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

@ -58,10 +58,11 @@ class i18n
if (!isset(static::$translations[$language]))
{
$path = ROOT_DIR . '/data/i18n/' . $language . '.yaml';
static::$translations[$language] = file_exists($path) ? Spyc::YAMLLoadString(file_get_contents($path)) : [];
}
$scope = static::$translations[$language];
foreach(explode('.', $token) as $level)
foreach (explode('.', $token) as $level)
{
if (isset($scope[$level]))
{
@ -96,15 +97,15 @@ class i18n
// then headers
// 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)
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
$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
foreach ($langs as $lang => $val)
@ -121,7 +122,7 @@ class i18n
}
}
foreach($candidates as $candidate)
foreach ($candidates as $candidate)
{
foreach (static::getAllCultures() as $culture)
{

View file

@ -26,7 +26,9 @@
<form action="/set-culture" method="POST">
<select id="language-dropdown" name="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 ?>
</select>
</form>