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.

186 lines
3.6 KiB

10 years ago
10 years ago
10 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. <?php
  2. require_once("include/dba.php");
  3. /**
  4. * translation support
  5. */
  6. /**
  7. *
  8. * Get the language setting directly from system variables, bypassing get_config()
  9. * as database may not yet be configured.
  10. *
  11. * If possible, we use the value from the browser.
  12. *
  13. */
  14. if(! function_exists('get_browser_language')) {
  15. function get_browser_language() {
  16. if (x($_SERVER,'HTTP_ACCEPT_LANGUAGE')) {
  17. // break up string into pieces (languages and q factors)
  18. preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i',
  19. $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);
  20. if (count($lang_parse[1])) {
  21. // create a list like "en" => 0.8
  22. $langs = array_combine($lang_parse[1], $lang_parse[4]);
  23. // set default to 1 for any without q factor
  24. foreach ($langs as $lang => $val) {
  25. if ($val === '') $langs[$lang] = 1;
  26. }
  27. // sort list based on value
  28. arsort($langs, SORT_NUMERIC);
  29. }
  30. }
  31. if(isset($langs) && count($langs)) {
  32. foreach ($langs as $lang => $v) {
  33. if(file_exists("view/$lang") && is_dir("view/$lang")) {
  34. $preferred = $lang;
  35. break;
  36. }
  37. }
  38. }
  39. if(isset($preferred))
  40. return $preferred;
  41. $a = get_app();
  42. return ((isset($a->config['system']['language'])) ? $a->config['system']['language'] : 'en');
  43. }}
  44. function push_lang($language) {
  45. global $lang, $a;
  46. $a->langsave = $lang;
  47. if($language === $lang)
  48. return;
  49. if(isset($a->strings) && count($a->strings)) {
  50. $a->stringsave = $a->strings;
  51. }
  52. $a->strings = array();
  53. load_translation_table($language);
  54. $lang = $language;
  55. }
  56. function pop_lang() {
  57. global $lang, $a;
  58. if($lang === $a->langsave)
  59. return;
  60. if(isset($a->stringsave))
  61. $a->strings = $a->stringsave;
  62. else
  63. $a->strings = array();
  64. $lang = $a->langsave;
  65. }
  66. // l
  67. if(! function_exists('load_translation_table')) {
  68. /**
  69. * load string translation table for alternate language
  70. *
  71. * first plugin strings are loaded, then globals
  72. *
  73. * @param string $lang language code to load
  74. */
  75. function load_translation_table($lang) {
  76. global $a;
  77. $a->strings = array();
  78. // load enabled plugins strings
  79. $plugins = q("SELECT name FROM addon WHERE installed=1;");
  80. if ($plugins!==false) {
  81. foreach($plugins as $p) {
  82. $name = $p['name'];
  83. if(file_exists("addon/$name/lang/$lang/strings.php")) {
  84. include("addon/$name/lang/$lang/strings.php");
  85. }
  86. }
  87. }
  88. if(file_exists("view/$lang/strings.php")) {
  89. include("view/$lang/strings.php");
  90. }
  91. }}
  92. // translate string if translation exists
  93. if(! function_exists('t')) {
  94. function t($s) {
  95. $a = get_app();
  96. if(x($a->strings,$s)) {
  97. $t = $a->strings[$s];
  98. return is_array($t)?$t[0]:$t;
  99. }
  100. return $s;
  101. }}
  102. if(! function_exists('tt')){
  103. function tt($singular, $plural, $count){
  104. global $lang;
  105. $a = get_app();
  106. if(x($a->strings,$singular)) {
  107. $t = $a->strings[$singular];
  108. $f = 'string_plural_select_' . str_replace('-','_',$lang);
  109. if(! function_exists($f))
  110. $f = 'string_plural_select_default';
  111. $k = $f($count);
  112. return is_array($t)?$t[$k]:$t;
  113. }
  114. if ($count!=1){
  115. return $plural;
  116. } else {
  117. return $singular;
  118. }
  119. }}
  120. // provide a fallback which will not collide with
  121. // a function defined in any language file
  122. if(! function_exists('string_plural_select_default')) {
  123. function string_plural_select_default($n) {
  124. return ($n != 1);
  125. }}
  126. /**
  127. * Return installed languages as associative array
  128. * [
  129. * lang => lang,
  130. * ...
  131. * ]
  132. */
  133. function get_avaiable_languages() {
  134. $lang_choices = array();
  135. $langs = glob('view/*/strings.php'); /**/
  136. if(is_array($langs) && count($langs)) {
  137. if(! in_array('view/en/strings.php',$langs))
  138. $langs[] = 'view/en/';
  139. asort($langs);
  140. foreach($langs as $l) {
  141. $t = explode("/",$l);
  142. $lang_choices[$t[1]] = $t[1];
  143. }
  144. }
  145. return $lang_choices;
  146. }