| 8 | | // Bootstrap gettext |
|---|
| 9 | | $textdomainpath = T_bindtextdomain ('elgg', path . 'languages'); |
|---|
| 10 | | // Bind the 'elgg' text domain to the languages directory |
|---|
| 11 | | T_bindtextdomain ('elgg', path . 'languages'); |
|---|
| 12 | | // Set our current text domain to 'elgg' |
|---|
| 13 | | T_textdomain('elgg'); |
|---|
| 14 | | // This is a UTF-8 application |
|---|
| 15 | | T_bind_textdomain_codeset('elgg', 'UTF-8'); |
|---|
| | 13 | // Return the plural form. |
|---|
| | 14 | function __ngettext($single, $plural, $number, $domain = 'elgg') { |
|---|
| | 15 | global $l10n; |
|---|
| 21 | | if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) || $_SERVER['HTTP_ACCEPT_LANGUAGE'] == "") { |
|---|
| 22 | | $locale = strtolower(locale); |
|---|
| 23 | | $list[] = $locale; |
|---|
| 24 | | } else { |
|---|
| 25 | | $locale = strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']); |
|---|
| 26 | | $list = explode(",", $locale); |
|---|
| | 27 | function load_textdomain($domain, $mofile) { |
|---|
| | 28 | global $l10n; |
|---|
| | 29 | |
|---|
| | 30 | if (isset($l10n[$domain])) |
|---|
| | 31 | return; |
|---|
| | 32 | |
|---|
| | 33 | if ( is_readable($mofile)) |
|---|
| | 34 | $input = new CachedFileReader($mofile); |
|---|
| | 35 | else |
|---|
| | 36 | return; |
|---|
| | 37 | |
|---|
| | 38 | $l10n[$domain] = new gettext_reader($input); |
|---|
| 28 | | |
|---|
| 29 | | // If the locale isn't the default, set a new default language |
|---|
| 30 | | // If the first choice is the right non-country-specific language, don't bother |
|---|
| 31 | | // (kludge to allow a browser to default to "en", basically) |
|---|
| 32 | | if (substr($list[0], 0, 2) != substr(locale, 0, 2)) { |
|---|
| 33 | | // Set locale to language_COUNTRY in preference order |
|---|
| 34 | | if (sizeof($list) > 0) { |
|---|
| 35 | | $newlist = array(); |
|---|
| 36 | | $extralist = array(); |
|---|
| 37 | | foreach ($list as $language) { |
|---|
| 38 | | $language = explode(";", $language); |
|---|
| 39 | | $language = $language[0]; |
|---|
| 40 | | $languageparts = explode("-", $language); |
|---|
| 41 | | if ($languageparts[1]) { |
|---|
| 42 | | // If the browser has given a 2-part language code, use it... |
|---|
| 43 | | $newlist[] = $languageparts[0] . "_" . strtoupper($languageparts[1]); |
|---|
| 44 | | // but also add the munged code to the end of the list, just in case. |
|---|
| 45 | | // E.g. if user passes "de_AT", this will add "de_DE" to end of the list as a fallback. |
|---|
| 46 | | $extralist[] = $languageparts[0] . "_" . strtoupper($languageparts[0]); |
|---|
| 47 | | } else { |
|---|
| 48 | | // Otherwise munge one from a 1-part code. |
|---|
| 49 | | // NB. This is a flawed assumption, because not all languages have the same language |
|---|
| 50 | | // and country codes: e.g. en_EN is not valid for English, and Danish is da_DK. |
|---|
| 51 | | $newlist[] = $languageparts[0] . "_" . strtoupper($languageparts[0]); |
|---|
| 52 | | } |
|---|
| | 40 | |
|---|
| | 41 | function parse_http_accept_language($str = null) { |
|---|
| | 42 | // getting http instruction if not provided |
|---|
| | 43 | $str = $str ? $str : $_SERVER['HTTP_ACCEPT_LANGUAGE']; |
|---|
| | 44 | |
|---|
| | 45 | // exploding accepted languages |
|---|
| | 46 | $langs = explode(',',$str); |
|---|
| | 47 | |
|---|
| | 48 | // creating output list |
|---|
| | 49 | $accepted = array(); |
|---|
| | 50 | |
|---|
| | 51 | foreach ($langs as $lang) { |
|---|
| | 52 | // parsing language preference instructions |
|---|
| | 53 | // 2_digit_code[-longer_code][;q=coefficient] |
|---|
| | 54 | ereg('([a-z]{1,2})(-([a-z0-9]+))?(;q=([0-9\.]+))?', $lang, $found); |
|---|
| | 55 | |
|---|
| | 56 | // 2 digit lang code |
|---|
| | 57 | $code = $found[1]; |
|---|
| | 58 | |
|---|
| | 59 | // lang code complement |
|---|
| | 60 | $morecode = $found[3]; |
|---|
| | 61 | |
|---|
| | 62 | if (empty($morecode)) { |
|---|
| | 63 | $fullcode = $code . "_" . strtoupper($code); |
|---|
| | 64 | } else { |
|---|
| | 65 | $fullcode = $code . "_" . strtoupper($morecode); |
|---|
| 54 | | $list = array_merge($newlist, $extralist); |
|---|
| 55 | | |
|---|
| 56 | | // Add user locales to global configuration settings. |
|---|
| 57 | | $CFG->userlocale = $list; |
|---|
| 58 | | |
|---|
| 59 | | foreach($list as $languagecode) { |
|---|
| 60 | | // Presumably we don't want to set locale to a language we don't have the .mo file for? |
|---|
| 61 | | if (file_exists($textdomainpath . '/' . $languagecode . '/LC_MESSAGES/elgg.mo')) { |
|---|
| 62 | | // Try UTF-8 version of the locale first. |
|---|
| 63 | | if (T_setlocale(LC_MESSAGES, $languagecode.'.UTF-8', $languagecode)) { |
|---|
| 64 | | T_setlocale(LC_TIME, $languagecode.'.UTF-8', $languagecode); |
|---|
| 65 | | break; |
|---|
| 66 | | } |
|---|
| 67 | | } |
|---|
| 68 | | } |
|---|
| | 67 | |
|---|
| | 68 | // coefficient |
|---|
| | 69 | $coef = sprintf('%3.1f', $found[5] ? $found[5] : '1'); |
|---|
| | 70 | |
|---|
| | 71 | // for sorting by coefficient |
|---|
| | 72 | $key = $coef.'-'.$code; |
|---|
| | 73 | |
|---|
| | 74 | // Only add it if a translation file is present or default locale |
|---|
| | 75 | if (file_exists(textdomain_file($fullcode, $domain = 'elgg')) || $fullcode == $CFG->defaultlocale) { |
|---|
| | 76 | // adding |
|---|
| | 77 | $accepted[$key] = array('code' => $code, |
|---|
| | 78 | 'coef' => $coef, |
|---|
| | 79 | 'morecode' => $morecode, |
|---|
| | 80 | 'fullcode' => $fullcode); |
|---|
| | 81 | } |
|---|
| | 82 | } |
|---|
| | 83 | |
|---|
| | 84 | // sorting the list by coefficient desc |
|---|
| | 85 | krsort($accepted); |
|---|
| | 86 | |
|---|
| | 87 | return $accepted; |
|---|
| | 88 | } |
|---|
| | 89 | |
|---|
| | 90 | function init_i18n($domain, $native = false) { |
|---|
| | 91 | global $CFG; |
|---|
| | 92 | |
|---|
| | 93 | // To prevent config file error |
|---|
| | 94 | if (!defined($CFG->defaultlocale) || empty($CFG->defaultlocale)) { |
|---|
| | 95 | $CFG->locale = "en_GB"; |
|---|
| | 96 | } |
|---|
| | 97 | |
|---|
| | 98 | $languages = parse_http_accept_language(); |
|---|
| | 99 | |
|---|
| | 100 | // Get the language array key |
|---|
| | 101 | $lang_key = array_shift(array_keys($languages)); |
|---|
| | 102 | |
|---|
| | 103 | if (!empty($languages[$lang_key]['fullcode'])) { |
|---|
| | 104 | //Set initial user locale |
|---|
| | 105 | $CFG->userlocale = $languages[$lang_key]['fullcode']; |
|---|
| | 106 | } else { |
|---|
| | 107 | $CFG->userlocale = $CFG->defaultlocale; |
|---|
| | 108 | } |
|---|
| | 109 | |
|---|
| | 110 | if ($native == false) { |
|---|
| | 111 | $textdomainfile = textdomain_file($CFG->defaultlocale); |
|---|
| | 112 | load_textdomain($domain, $textdomainfile); |
|---|
| | 113 | } |
|---|
| | 114 | |
|---|
| | 115 | return; |
|---|
| | 116 | } |
|---|
| | 117 | |
|---|
| | 118 | function textdomain_file($locale, $domain = 'elgg') { |
|---|
| | 119 | global $CFG; |
|---|
| | 120 | |
|---|
| | 121 | return $CFG->dirroot.'languages/'.$locale.'/LC_MESSAGES/'.$domain.'.mo'; |
|---|
| | 122 | } |
|---|
| | 123 | |
|---|
| | 124 | if (!function_exists('gettext')) { |
|---|
| | 125 | // No native gettext support |
|---|
| | 126 | function gettext($text, $domain = 'elgg') { |
|---|
| | 127 | _e($text, $domain); |
|---|
| 71 | | // User language is same as default, easy... |
|---|
| 72 | | T_textdomain('elgg'); |
|---|
| 73 | | T_setlocale(LC_ALL, $CFG->defaultlocale.'.UTF-8', $CFG->defaultlocale); |
|---|
| | 130 | // Have gettext |
|---|
| | 131 | $domain = "elgg"; |
|---|
| | 132 | init_i18n($domain, true); |
|---|
| | 133 | |
|---|
| | 134 | $encoding = 'UTF-8'; |
|---|
| | 135 | $locale = $CFG->userlocale; |
|---|
| | 136 | |
|---|
| | 137 | putenv("LANGUAGE = $locale"); |
|---|
| | 138 | putenv("LANG = $locale"); |
|---|
| | 139 | |
|---|
| | 140 | T_setlocale(LC_ALL, $locale); |
|---|
| | 141 | |
|---|
| | 142 | // This will stop Windows from whining |
|---|
| | 143 | if (!defined('LC_MESSAGES')) |
|---|
| | 144 | define('LC_MESSAGES', 6); |
|---|
| | 145 | |
|---|
| | 146 | T_setlocale(LC_MESSAGES, $locale); |
|---|
| | 147 | |
|---|
| | 148 | bindtextdomain($domain, $CFG->dirroot."languages"); |
|---|
| | 149 | |
|---|
| | 150 | // bind_textdomain_codeset is supported only in PHP 4.2.0+ |
|---|
| | 151 | if (function_exists('bind_textdomain_codeset')) |
|---|
| | 152 | bind_textdomain_codeset($domain, $encoding); |
|---|
| | 153 | |
|---|
| | 154 | textdomain($domain); |
|---|