choose first if (!$language_no_q) { if (file_exists(LANG_DIR . $lang_code[0]) && ($lang_code[1] != 'q' || $q < $lang_code[2])) { $locale = $lang_code[0]; if( $lang_code[1] != 'q') { $q = 1.0; $language_no_q = TRUE; } else { $q = $lang_code[1]; } } } } } } else { $locale = "en"; } return $locale; } /** * Write language to environment */ function i18n_settext_language($i18n, $language_dir = LANG_DIR) { global $_PAGE_INFO; if (strlen($i18n)) { putenv("LANGUAGE=" . $i18n); putenv("LANG=" . $i18n); putenv("LC_ALL=" . $i18n); // Restore time and numeric, this to prevent javascript/mysql errors setlocale(LC_ALL, $i18n); setlocale(LC_TIME, 'C'); setlocale(LC_NUMERIC, 'C'); // Define charset header("Content-Type: text/html; charset=" . $_PAGE_INFO['charset']); } if (strlen($language_dir)) { $textdomain = DOMAIN; if( preg_match("|^.*/release/([^/]+)/.+$|", $language_dir, $release) || preg_match("|^/home/([^/]+)/.*$|", $language_dir, $release) ) { $textdomain .= "-" . $release[1]; } bindtextdomain($textdomain, $language_dir); textdomain($textdomain); } } /** * Double underscore: Interface to 'ngettext()', using 'sprintf()' */ function __($msgid1, $msgid2, $n) { $args = func_get_args(); // delete 'msgid1' and 'msgid2' array_splice($args, 0, 2); // format the string return vsprintf(ngettext($msgid1, $msgid2, $n), $args); } /** * Triple underscore: Format a string in the style of Microsoft's String::Format * * NB: Using the Microsoft style is somewhat more convenient than 'sprintf' * as PHP does not implement positional parameters. */ function ___($fmt) { // Build argument list $args = array_slice(func_get_args(), 1); // if( PHP_VERSION_ID >= 50300 ) { // return preg_replace_callback( // "/({[0-9]+})/", // function($match) use ($args) { return $args[$match[0]]; }, // _($fmt) // ); // } // else { return preg_replace( "/\{([0-9]+)\}/e", '$args[\1]', _($fmt) ); // } } /** * Read translation from the database table 'vertaling' * * Inputs: * - id Database id _or_ array of strings (all translations) * _or_ string to translate via 'gettext()' * when 'field' is not set (for symmetry with, erm, well * the function name :-) * - field Table and field in the table 'vertaling' * - i18n Target language (auto-detect if not set) * * Returns: * - Translation from the table 'vertaling' or via 'gettext()' when 'field' is * not set. * - FALSE on error */ function i18n_translate($id, $field = NULL, $i18n = NULL) { // Determine language to use if not passed as a parameter if( !$i18n ) { $i18n = $_SESSION[$_PAGE_INFO['id']]['i18n']; if( !$i18n ) $i18n = i18n_browser_preferred_language(); } // default return value to indicate that we cannot translate 'id' $str = FALSE; if( is_array($id) ) { // prefetched array with all translations // try the specified target language if( $i18n ) $str = $id[$i18n]; // search other languages if( !$str && function_exists("db_fetch_system_lang") ) { // Get all languages, in the user's preference $i18n_languages = db_fetch_system_lang(); foreach ($i18n_languages as $i18n => $lang) { $str = $id[$i18n]; if( $str ) return $str; } } else if( $str ) return $str; } else if( !$field ) { // database field is not set: simply translate via 'gettext()' $str = _($id); } else if( $i18n && function_exists("db_fetch_item") ) { // try the specified target language $str = db_fetch_item("SELECT tekst FROM vertaling WHERE id=" . $id . " AND field='" . $field . "' AND i18n='" . $i18n . "'"); } // search the database for other languages when not found above if( !$str && is_numeric($id) && $field && function_exists("db_fetch_system_lang") && function_exists("db_fetch_item") ) { // Get all languages, in the user's preference $i18n_languages = db_fetch_system_lang(); foreach ($i18n_languages as $i18n => $lang) { $str = db_fetch_item("SELECT tekst FROM vertaling WHERE id=" . $id . " AND field='" . $field . "' AND i18n='" . $i18n . "'"); if( $str ) return $str; } } // return translated string, or FALSE on error return $str; } ?>