Pelzini

This is the code documentation for the Pelzini project

source of /viewer/i18n.php

This is a set of functions for internationalisation.

The main functions are:
  loadLanguage for loading a language file
  str for returning a language string
  setParam for manually setting a replacement parameter
  clearParams for clearing all replacement parameters
  1. <?php
  2. require_once 'i18n/strings.php';
  3.  
  4. /**
  5.  * This is a set of functions for internationalisation.
  6.  *
  7.  * The main functions are:
  8.  * {@link loadLanguage} for loading a language file
  9.  * {@link str} for returning a language string
  10.  * {@link setParam} for manually setting a replacement parameter
  11.  * {@link clearParams} for clearing all replacement parameters
  12.  *
  13.  * @author Josh Heidenreich, 2009-05-05
  14.  **/
  15.  
  16.  
  17. /**
  18.  * Loads a language file
  19.  *
  20.  * If you are using a language which may not be complete, it is advisable
  21.  * to load another language which you know is complete first (e.g. English)
  22.  * This will prevent empty strings being returned.
  23.  *
  24.  * @author Josh Heidenreich, 2009-05-05
  25.  *
  26.  * @param string The name of the language. Should correspond to a language file (without the extension)
  27.  * @return boolean True on success, false on failure
  28.  **/
  29. function loadLanguage($language)
  30. {
  31. global $strings;
  32.  
  33. $file_lines = @file(dirname(__FILE__) . "/i18n/{$language}.txt");
  34. if ($file_lines == null) return false;
  35.  
  36. foreach ($file_lines as $line) {
  37. $line = preg_replace('/;(.*)$/', '', $line);
  38. $line = trim($line);
  39. if ($line == '') continue;
  40.  
  41. $parts = preg_split('/\s+/', $line, 2);
  42.  
  43. $string_id = @constant($parts[0]);
  44. if ($string_id == null) continue;
  45. $strings[$string_id] = $parts[1];
  46. }
  47.  
  48. unset ($file_lines);
  49.  
  50. return true;
  51. }
  52.  
  53.  
  54. /**
  55.  * Returns the original string from the string table, without any parameter replacement
  56.  **/
  57. function getOriginalString($string_constant)
  58. {
  59. global $strings;
  60.  
  61. return $strings[$string_constant];
  62. }
  63.  
  64.  
  65. /**
  66.  * Sets a named param for use by the {@link str} function
  67.  *
  68.  * @author Josh Heidenreich, 2009-05-05
  69.  *
  70.  * @param string $name The name of the parameter
  71.  * @param mixed $value The value of the parameter
  72.  **/
  73. function setParam($name, $value)
  74. {
  75. global $string_params;
  76.  
  77. $name = strtoupper($name);
  78. if ($value == '') $value = null;
  79. $string_params[$name] = $value;
  80. }
  81.  
  82.  
  83. /**
  84.  * Clears all named parameters that are used by the {@link str} function
  85.  *
  86.  * @author Josh Heidenreich, 2009-05-05
  87.  **/
  88. function clearParams()
  89. {
  90. global $string_params;
  91.  
  92. $string_params = array();
  93. }
  94.  
  95.  
  96. /**
  97.  * Outputs a string, and sets arguments
  98.  *
  99.  * This function also allows additional functions after the string constant, printf style.
  100.  * Because the language system uses named arguments, arguments should be specified in pairs
  101.  * The first argument should be the param name, the second argument should be the param
  102.  *
  103.  * @author Josh Heidenreich, 2009-05-05
  104.  *
  105.  * @param int $string_constant The constant representing the string to return
  106.  * @return string The string, with replacements made
  107.  **/
  108. function str($string_constant)
  109. {
  110. global $strings, $string_params;
  111.  
  112. $string_constant = (int) $string_constant;
  113. if ($string_constant == 0) return "!! UNKNOWN STRING CONSTANT !!";
  114.  
  115. $num_args = func_num_args();
  116. if ($num_args % 2 == 0) return "!! ERROR INCORECT ARGS !!";
  117.  
  118. $index = 1;
  119. $name = '';
  120. for ($index = 1; $index < func_num_args(); $index++) {
  121. $arg = func_get_arg($index);
  122.  
  123. if ($name == '') {
  124. $name = $arg;
  125. } else {
  126. setParam ($name, $arg);
  127. $name = '';
  128. }
  129. }
  130.  
  131. $str = param_replace($strings[$string_constant]);
  132. return $str;
  133. }
  134.  
  135.  
  136. /**
  137.  * Replaces params in strings
  138.  *
  139.  * Replacements can be in the following forms:
  140.  * {PARAM}
  141.  * Does a replacement with a named parameter, PARAM, specified in any case
  142.  *
  143.  * {#PLURAL|PARAM|SINGLE|MULTIPLE}
  144.  * Does a replacement with SINGLE if PARAM (any case) is 1, and with MULTIPLE if PARAM is anything else
  145.  *
  146.  * {#NL}
  147.  * Adds a new line to the output
  148.  *
  149.  * {#URLENC|PARAM}
  150.  * Returns the specified parameter, urlencode()ed.
  151.  *
  152.  * {#HTMLENC|PARAM}
  153.  * Returns the specified parameter, htmlspecialchars()ed.
  154.  *
  155.  * @author Josh Heidenreich, 2009-05-05
  156.  *
  157.  * @param string $str The string to do replacements to
  158.  * @return string The replaced string
  159.  **/
  160. function param_replace($str)
  161. {
  162. return preg_replace_callback('/\{([^}]+?)\}/', 'param_replace_inner', $str);
  163. }
  164.  
  165.  
  166. /**
  167.  * Does the actual legwork for param_replace
  168.  *
  169.  * @author Josh Heidenreich, 2009-05-05
  170.  *
  171.  * @param array $matches The matches array returned by preg_replace_callback
  172.  **/
  173. function param_replace_inner($matches)
  174. {
  175. global $string_params;
  176.  
  177. $replace_code = $matches[1];
  178.  
  179. if (strncasecmp($replace_code, '#PLURAL', 7) == 0) {
  180. $parts = explode('|', $replace_code);
  181. $var_name = strtoupper($parts[1]);
  182.  
  183. if ($string_params[$var_name] == 1) {
  184. return $parts[2];
  185. } else {
  186. return $parts[3];
  187. }
  188.  
  189. } else if (strncasecmp($replace_code, '#NL', 3) == 0) {
  190. return "<br>";
  191.  
  192. } else if (strncasecmp($replace_code, '#URLENC', 7) == 0) {
  193. $parts = explode('|', $replace_code);
  194. $replace_code = strtoupper($parts[1]);
  195.  
  196. $replace_code = strtoupper($replace_code);
  197. return urlencode($string_params[$replace_code]);
  198.  
  199. } else if (strncasecmp($replace_code, '#HTMLENC', 8) == 0) {
  200. $parts = explode('|', $replace_code);
  201. $replace_code = strtoupper($parts[1]);
  202.  
  203. $replace_code = strtoupper($replace_code);
  204. return htmlspecialchars($string_params[$replace_code]);
  205.  
  206. } else {
  207. $replace_code = strtoupper($replace_code);
  208. return $string_params[$replace_code];
  209. }
  210.  
  211. return "!! ERROR REPLACING '{$replace_code}' !!";
  212. }
  213.  
  214.  
  215. ?>
  216.