예제 #1
0
파일: I18nTest.php 프로젝트: openclerk/i18n
 /**
  * A default locale is included.
  */
 function testAllLocalesIncludesDefault()
 {
     foreach (I18n::getAvailableLocales() as $localeInstance) {
         if ($localeInstance instanceof Openclerk\DefaultLocale) {
             return;
         }
     }
     $this->fail("Could not find DefaultLocale");
 }
예제 #2
0
 /**
  * Tests that all locales defined by {@link get_all_locales()} exist.
  */
 function testAllLocales()
 {
     foreach (I18n::getAvailableLocales() as $locale => $instance) {
         if ($locale == 'en') {
             continue;
         }
         $f = __DIR__ . "/../locale/" . $locale . ".php";
         $this->assertTrue(file_exists($f), "Locale file " . $f . " should exist");
     }
 }
예제 #3
0
    {
        return $this->title;
    }
    function load()
    {
        require __DIR__ . "/../locale/" . $this->key . ".php";
        return $result;
    }
}
$locales = array('de' => 'German', 'fr' => 'French', 'jp' => 'Japanese', 'ru' => 'Russian', 'zh' => 'Chinese');
foreach ($locales as $locale => $title) {
    I18n::addAvailableLocale(new GenericLocale($locale, $title));
}
I18n::addDefaultKeys(array(':site_name' => get_site_config('site_name')));
// set locale as necessary
if (isset($_COOKIE["locale"]) && in_array($_COOKIE["locale"], array_keys(I18n::getAvailableLocales()))) {
    I18n::setLocale($_COOKIE["locale"]);
}
\Openclerk\Events::on('i18n_missing_string', function ($data) {
    $locale = $data['locale'];
    $key = $data['key'];
    log_uncaught_exception(new LocaleException("Locale '{$locale}': Missing key '{$key}'"));
});
/**
 * Helper function to mark strings that need to be translated on the client-side.
 */
function ct($s)
{
    // do not do any translation here - we have to do it on the client side!
    return $s;
}
예제 #4
0
<?php

use Openclerk\I18n;
?>

<form action="<?php 
echo htmlspecialchars(url_for('set_locale'));
?>
" method="post" id="locale_selector">
  <select class="language-list locale locale-<?php 
echo htmlspecialchars(I18n::getCurrentLocale());
?>
" name="locale">
  <?php 
foreach (I18n::getAvailableLocales() as $locale) {
    $selected = I18n::getCurrentLocale() == $locale->getKey();
    echo "<option value=\"" . htmlspecialchars($locale->getKey()) . "\" class=\"locale locale-" . htmlspecialchars($locale->getKey()) . "\"" . ($selected ? " selected" : "") . ">" . htmlspecialchars($locale->getTitle()) . "</option>\n";
}
?>
  </select>
  <input type="hidden" name="redirect" value="<?php 
echo htmlspecialchars(url_for(request_url_relative(), $_GET));
?>
">
</form>
예제 #5
0
<?php

/**
 * Set the current session, cookie or user language.
 */
use Openclerk\I18n;
$locale = require_post("locale");
$redirect = require_post("redirect");
$available = I18n::getAvailableLocales();
if (!isset($available[$locale])) {
    throw new LocaleException("Locale '{$locale}' does not exist for user selection");
}
I18n::setLocale($locale);
// update cookies
setcookie('locale', $locale, time() + 60 * 60 * 24 * 365 * 10);
// update users
if (user_logged_in()) {
    $user = get_user(user_id());
    $q = db()->prepare("UPDATE user_properties SET locale=? WHERE id=?");
    $q->execute(array($locale, user_id()));
}
// go back to their previous page
redirect($redirect);
예제 #6
0
/**
 * Allows us to load client-specific locale strings.
 */
define('FORCE_NO_RELATIVE', true);
// url_for() references need to be relative to the base path, not the js/ directory that this script is within
require __DIR__ . "/../../inc/content_type/js.php";
// to allow for appropriate headers etc
require __DIR__ . "/../../inc/global.php";
use Openclerk\I18n;
use Openclerk\Locale;
// note that the contents of this file will change based on user, selected currencies etc;
// these parameters need to be encoded into a ?hash parameter, so that while this file can
// be cached, it is correctly reloaded when necessary.
allow_cache();
$locale = require_get("locale");
if (!in_array($locale, array_keys(I18n::getAvailableLocales()))) {
    throw new Exception("Locale '{$locale}' is not a valid locale");
}
I18n::setLocale($locale);
$strings = json_decode(file_get_contents(__DIR__ . "/../../locale/client.json"));
$result = array();
foreach ($strings as $key) {
    $result[$key] = t($key);
}
?>
window.LocaleStrings = <?php 
echo json_encode($result);
?>
;