This commit is contained in:
Alex Grintsvayg 2016-09-20 10:07:20 -04:00
parent e50e729019
commit 23e4e3c960
2 changed files with 14 additions and 11 deletions

View file

@ -22,7 +22,7 @@ class i18n
list($language, $country) = explode('_', $culture); list($language, $country) = explode('_', $culture);
static::$language = $language; static::$language = $language;
static::$country = $country; static::$country = $country;
setlocale(LC_MONETARY, $culture . '.UTF-8'); setlocale(LC_MONETARY, $culture . '.UTF-8');
} }
@ -39,7 +39,7 @@ class i18n
public static function getAllCultures() public static function getAllCultures()
{ {
return static::$cultures; return static::$cultures;
} }
public static function formatCurrency($amount, $currency = 'USD') public static function formatCurrency($amount, $currency = 'USD')
@ -49,7 +49,7 @@ class i18n
public static function formatCredits($amount) public static function formatCredits($amount)
{ {
return '<span class="formatted-credits">' . (is_numeric($amount) ? number_format($amount, 1) : $amount) . ' LBC</span>'; return '<span class="formatted-credits">' . (is_numeric($amount) ? number_format($amount, 1) : $amount) . ' LBC</span>';
} }
public static function translate($token, $language = null) public static function translate($token, $language = null)
@ -58,10 +58,11 @@ class i18n
if (!isset(static::$translations[$language])) if (!isset(static::$translations[$language]))
{ {
$path = ROOT_DIR . '/data/i18n/' . $language . '.yaml'; $path = ROOT_DIR . '/data/i18n/' . $language . '.yaml';
static::$translations[$language] = file_exists($path) ? Spyc::YAMLLoadString(file_get_contents($path)) : []; static::$translations[$language] = file_exists($path) ? Spyc::YAMLLoadString(file_get_contents($path)) : [];
} }
$scope = static::$translations[$language]; $scope = static::$translations[$language];
foreach(explode('.', $token) as $level) foreach (explode('.', $token) as $level)
{ {
if (isset($scope[$level])) if (isset($scope[$level]))
{ {
@ -85,7 +86,7 @@ class i18n
//url trumps everything //url trumps everything
$urlTokens = Request::getHost() ? explode('.', Request::getHost()) : []; $urlTokens = Request::getHost() ? explode('.', Request::getHost()) : [];
$code = $urlTokens ? reset($urlTokens) : null; $code = $urlTokens ? reset($urlTokens) : null;
if ($code !== 'www') if ($code !== 'www')
{ {
$candidates[] = $code; $candidates[] = $code;
@ -96,15 +97,15 @@ class i18n
// then headers // then headers
// http://www.thefutureoftheweb.com/blog/use-accept-language-header // http://www.thefutureoftheweb.com/blog/use-accept-language-header
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) if (Request::getHttpHeader('Accept-Language'))
{ {
// break up string into pieces (languages and q factors) // break up string into pieces (languages and q factors)
preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse); preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', Request::getHttpHeader('Accept-Language'), $languages);
if (count($lang_parse[1])) if (isset($languages[1]) && count($languages[1]))
{ {
// create a list like "en" => 0.8 // create a list like "en" => 0.8
$langs = array_combine($lang_parse[1], $lang_parse[4]); $langs = array_combine($languages[1], $languages[4]);
// set default to 1 for any without q factor // set default to 1 for any without q factor
foreach ($langs as $lang => $val) foreach ($langs as $lang => $val)
@ -121,7 +122,7 @@ class i18n
} }
} }
foreach($candidates as $candidate) foreach ($candidates as $candidate)
{ {
foreach (static::getAllCultures() as $culture) foreach (static::getAllCultures() as $culture)
{ {

View file

@ -26,7 +26,9 @@
<form action="/set-culture" method="POST"> <form action="/set-culture" method="POST">
<select id="language-dropdown" name="culture"> <select id="language-dropdown" name="culture">
<?php foreach ($cultures as $culture): ?> <?php foreach ($cultures as $culture): ?>
<option <?php echo $culture == $selectedCulture ? 'selected="selected"' : '' ?>><?php echo $culture ?></option> <option value="<?php echo $culture ?>" <?php echo $culture == $selectedCulture ? 'selected="selected"' : '' ?>>
<?php echo $culture ?>
</option>
<?php endforeach ?> <?php endforeach ?>
</select> </select>
</form> </form>