Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

192 lines
4.5 KiB

  1. <?php
  2. /**
  3. * @brief translation support
  4. *
  5. * Get the language setting directly from system variables, bypassing get_config()
  6. * as database may not yet be configured.
  7. *
  8. * If possible, we use the value from the browser.
  9. *
  10. */
  11. require_once("include/dba.php");
  12. if(! function_exists('get_browser_language')) {
  13. /**
  14. * @brief get the prefered language from the HTTP_ACCEPT_LANGUAGE header
  15. */
  16. function get_browser_language() {
  17. if (x($_SERVER,'HTTP_ACCEPT_LANGUAGE')) {
  18. // break up string into pieces (languages and q factors)
  19. preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i',
  20. $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);
  21. $lang_list = [];
  22. if (count($lang_parse[1])) {
  23. // go through the list of prefered languages and add a generic language
  24. // for sub-linguas (e.g. de-ch will add de) if not already in array
  25. for ($i=0; $i<count($lang_parse[1]); $i++) {
  26. $lang_list[] = strtolower($lang_parse[1][$i]);
  27. if ( strlen($lang_parse[1][$i])>3 ) {
  28. $dashpos = strpos($lang_parse[1][$i], '-');
  29. if (! in_array(substr($lang_parse[1][$i], 0, $dashpos), $lang_list ) ) {
  30. $lang_list[] = strtolower(substr($lang_parse[1][$i], 0, $dashpos));
  31. }
  32. }
  33. }
  34. }
  35. }
  36. // check if we have translations for the preferred languages and pick the 1st that has
  37. for ($i=0; $i<count($lang_list); $i++) {
  38. $lang = $lang_list[$i];
  39. if(file_exists("view/lang/$lang") && is_dir("view/lang/$lang")) {
  40. $preferred = $lang;
  41. break;
  42. }
  43. }
  44. if(isset($preferred))
  45. return $preferred;
  46. // in case none matches, get the system wide configured language, or fall back to English
  47. $a = get_app();
  48. return ((isset($a->config['system']['language'])) ? $a->config['system']['language'] : 'en');
  49. }}
  50. function push_lang($language) {
  51. global $lang, $a;
  52. $a->langsave = $lang;
  53. if($language === $lang)
  54. return;
  55. if(isset($a->strings) && count($a->strings)) {
  56. $a->stringsave = $a->strings;
  57. }
  58. $a->strings = array();
  59. load_translation_table($language);
  60. $lang = $language;
  61. }
  62. function pop_lang() {
  63. global $lang, $a;
  64. if($lang === $a->langsave)
  65. return;
  66. if(isset($a->stringsave))
  67. $a->strings = $a->stringsave;
  68. else
  69. $a->strings = array();
  70. $lang = $a->langsave;
  71. }
  72. // l
  73. if(! function_exists('load_translation_table')) {
  74. /**
  75. * load string translation table for alternate language
  76. *
  77. * first plugin strings are loaded, then globals
  78. *
  79. * @param string $lang language code to load
  80. */
  81. function load_translation_table($lang) {
  82. global $a;
  83. $a->strings = array();
  84. // load enabled plugins strings
  85. $plugins = q("SELECT name FROM addon WHERE installed=1;");
  86. if ($plugins!==false) {
  87. foreach($plugins as $p) {
  88. $name = $p['name'];
  89. if(file_exists("addon/$name/lang/$lang/strings.php")) {
  90. include("addon/$name/lang/$lang/strings.php");
  91. }
  92. }
  93. }
  94. if(file_exists("view/lang/$lang/strings.php")) {
  95. include("view/lang/$lang/strings.php");
  96. }
  97. }}
  98. // translate string if translation exists
  99. if(! function_exists('t')) {
  100. function t($s) {
  101. $a = get_app();
  102. if(x($a->strings,$s)) {
  103. $t = $a->strings[$s];
  104. return is_array($t)?$t[0]:$t;
  105. }
  106. return $s;
  107. }}
  108. if(! function_exists('tt')){
  109. function tt($singular, $plural, $count){
  110. global $lang;
  111. $a = get_app();
  112. if(x($a->strings,$singular)) {
  113. $t = $a->strings[$singular];
  114. $f = 'string_plural_select_' . str_replace('-','_',$lang);
  115. if(! function_exists($f))
  116. $f = 'string_plural_select_default';
  117. $k = $f($count);
  118. return is_array($t)?$t[$k]:$t;
  119. }
  120. if ($count!=1){
  121. return $plural;
  122. } else {
  123. return $singular;
  124. }
  125. }}
  126. // provide a fallback which will not collide with
  127. // a function defined in any language file
  128. if(! function_exists('string_plural_select_default')) {
  129. function string_plural_select_default($n) {
  130. return ($n != 1);
  131. }}
  132. /**
  133. * @brief Return installed languages codes as associative array
  134. *
  135. * Scans the view/lang directory for the existence of "strings.php" files, and
  136. * returns an alphabetical list of their folder names (@-char language codes).
  137. * Adds the english language if it's missing from the list.
  138. *
  139. * Ex: array('de' => 'de', 'en' => 'en', 'fr' => 'fr', ...)
  140. *
  141. * @return array
  142. */
  143. function get_available_languages() {
  144. $langs = array();
  145. $strings_file_paths = glob('view/lang/*/strings.php');
  146. if (is_array($strings_file_paths) && count($strings_file_paths)) {
  147. if (!in_array('view/lang/en/strings.php', $strings_file_paths)) {
  148. $strings_file_paths[] = 'view/lang/en/strings.php';
  149. }
  150. asort($strings_file_paths);
  151. foreach($strings_file_paths as $strings_file_path) {
  152. $path_array = explode('/', $strings_file_path);
  153. $langs[$path_array[2]] = $path_array[2];
  154. }
  155. }
  156. return $langs;
  157. }