Beispiel #1
0
 /**
  *
  */
 public function initialize()
 {
     // Define locale (first from session, then from HTTP header)
     $this->_localeCode = \z\request()->session('culture/locale');
     if (empty($this->_localeCode) === true) {
         $this->_localeCode = locale_accept_from_http(\z\request()->header('Accept-Language'));
     }
     if (empty($this->_localeCode) === true) {
         $this->_localeCode = \z\pref('culture/locale');
     }
     // Define fallback
     $this->_fallbackCode = \z\pref('culture/fallback');
     // Canonicalize locales
     $this->_localeCode = locale_canonicalize($this->_localeCode);
     $this->_fallbackCode = locale_canonicalize($this->_fallbackCode);
     // List locales available
     $locales = \z\service('helper/file')->listFiles(PATH_APP . '/Config/Strings', null, false, true, false);
     // Is the locale available?
     if (in_array($this->_localeCode, $locales) === false) {
         // Grab the parent code (e.g. en for en_US)
         $primaryCode = locale_get_primary_language($this->_localeCode);
         // Find locales belonging to this parent
         $locales = array_filter($locales, function ($key) use($primaryCode) {
             return strpos($key, $primaryCode) === 0;
         });
         // Sort locales A-Z
         sort($locales);
         // Grab the very first locale
         $this->_localeCode = array_shift($locales);
     }
     // Load strings
     $this->loadStrings();
 }
 public function onNotMatched(AgaviExecutionContainer $container)
 {
     // the pattern didn't match, or onMatched() returned false.
     // that's sad. let's see if there's a locale set in a cookie from an earlier visit.
     $rd = $this->context->getRequest()->getRequestData();
     $cookie = $rd->getCookie('locale');
     if ($cookie !== null) {
         try {
             $this->translationManager->setLocale($cookie);
             return;
         } catch (AgaviException $e) {
             // bad cookie :<
             $this->context->getController()->getGlobalResponse()->unsetCookie('locale');
         }
     }
     if ($rd->hasHeader('Accept-Language')) {
         $hasIntl = function_exists('locale_accept_from_http');
         // try to find the best match for the locale
         $locales = self::parseAcceptLanguage($rd->getHeader('Accept-Language'));
         foreach ($locales as $locale) {
             try {
                 if ($hasIntl) {
                     // we don't use this directly on Accept-Language because we might not have the preferred locale, but another one
                     // in any case, it might help clean up the value a bit further
                     $locale = locale_accept_from_http($locale);
                 }
                 $this->translationManager->setLocale($locale);
                 return;
             } catch (AgaviException $e) {
             }
         }
     }
 }
 /**
  * Handle an incoming request.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  \Closure  $next
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
     if ($request->has('lang') && Languages::has($request->input('lang'))) {
         // 1. Check URL parameter
         $locale = $request->input('lang');
         App::setLocale($locale);
         // Logged-in users: save to database
         if (Auth::check()) {
             $user = Auth::user();
             $user->locale = $locale;
             $user->save();
         }
         Cookie::queue('locale', $locale, 24 * 60);
     } elseif (Auth::check() && Languages::has(Auth::user()->locale)) {
         // 2. Check database for logged in users
         App::setLocale(Auth::user()->locale);
     } elseif (Languages::has($request->cookie('locale'))) {
         // 3. Check cookies
         App::setLocale($request->cookie('locale'));
     } elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
         // 4. Check browser languages. Note that Googlebot do not have this.
         $accept_languages = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
         foreach ($accept_languages as $lang) {
             $lang = locale_accept_from_http($lang);
             $locale = locale_lookup(Languages::all(), $lang, true, 'en');
             if (!empty($locale)) {
                 App::setLocale($locale);
                 break;
             }
         }
     }
     setlocale(LC_TIME, Languages::withRegion(App::getLocale()) . '.utf8');
     return $next($request);
 }
Beispiel #4
0
 function get_client_locale()
 {
     if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
         return locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
     } else {
         return false;
     }
 }
/**
 * Attempts to get the client's locale
 *
 * @return mixed
 */
function getClientLocale()
{
    // can return 'en-GB', 'en-US', etc., or null if undetermined
    $locale = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
    if ($locale) {
        return $locale;
    }
    return null;
}
Beispiel #6
0
 public function detect()
 {
     if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && !empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
         $locale = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
         if ($locale !== null) {
             return collator_create($locale);
         }
     }
     return null;
 }
Beispiel #7
0
 /**
  * Check if we have local, if not get local from HTTP_ACCEPT_LANGUAGE
  */
 protected static function loadLocale()
 {
     if (self::$locale) {
         return;
     }
     if (isset($_SESSION['locale'])) {
         self::$locale = $_SESSION['locale'];
     } else {
         self::$locale = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
     }
 }
Beispiel #8
0
 /**
  * Handle an incoming request.
  *
  * @param \Illuminate\Http\Request $request
  * @param \Closure                 $next
  *
  * @return mixed
  */
 public function handle(Request $request, Closure $next)
 {
     $locale = presence($request->input('locale')) ?? presence($request->cookie('locale')) ?? locale_accept_from_http($request->server('HTTP_ACCEPT_LANGUAGE'));
     $locale = get_valid_locale($locale);
     App::setLocale($locale);
     $response = $next($request);
     if (method_exists($response, 'withCookie')) {
         return $response->withCookie(cookie()->forever('locale', $locale));
     } else {
         return $response;
     }
 }
Beispiel #9
0
 /**
 	Initialize the locale.
 
 	The parameters array can contain:
 	* auto:		Whether to automatically try to select the best locale based on the HTTP_ACCEPT_LANGUAGE header.
 	* default:	The default locale to use when others aren't available.
 
 	@param $aParams The parameters listed above.
 */
 public function __construct($aParams = array())
 {
     function_exists('locale_set_default') or burn('ConfigurationException', _WT('The intl PHP extension is required by the weeLocale application driver.'));
     if (!empty($aParams['default'])) {
         locale_set_default($aParams['default']) or burn('InvalidArgumentException', _WT('Setting the default locale failed.'));
     }
     if (!empty($aParams['auto']) && !empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
         try {
             $this->set(locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']));
         } catch (UnexpectedValueException $e) {
             //TODO:maybe try the default locale here and otherwise go back to 'C'?
         }
     }
 }
Beispiel #10
0
 public function __construct(Container $app)
 {
     $this->app = $app;
     $language = 'pt_BR';
     if (isset($_REQUEST['lang']) && $_REQUEST['lang']) {
         if (preg_match("/^\\w{2}_\\w{2}\$/", $_REQUEST['lang'])) {
             $language = $_REQUEST['lang'];
         }
     }
     if (!$language && isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) {
         if (function_exists('locale_accept_from_http')) {
             $language = locale_accept_from_http($_SERVER["HTTP_ACCEPT_LANGUAGE"]);
         }
     }
     $this->lang = $language;
 }
Beispiel #11
0
function lang_init()
{
    $available_languages = lang_get_available(false);
    if (isset($_SESSION['LANGUAGE']) && in_array($_SESSION['LANGUAGE'], $available_languages)) {
        $language = $_SESSION['LANGUAGE'];
    } else {
        if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
            $language = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
        }
    }
    if (!isset($language)) {
        $language = forum_get_setting('default_language', 'strlen', 'en_GB');
    }
    $languages = array($language . '.utf8', $language . '.UTF8', $language . '.utf-8', $language . '.UTF-8', $language);
    setlocale(LC_ALL, $languages);
    putenv('LC_ALL=' . $language);
    putenv('LANG=' . $language);
    putenv('LANGUAGE=' . $language);
    bindtextdomain('messages', realpath(BH_INCLUDE_PATH . 'locale'));
    bind_textdomain_codeset('messages', 'UTF-8');
    textdomain('messages');
}
Beispiel #12
0
 /**
  * Returns the prevered locale from HTTP_ACCEPT_LANGUAGE.
  *
  * @since   0.0.1
  *
  * @access  protected
  * @static
  * @return  string|bool Locale from SERVER, FALSE on failure.
  */
 protected static function getLocaleFromBrowser()
 {
     $accepted_languages = Filter::server('HTTP_ACCEPT_LANGUAGE');
     if ($accepted_languages) {
         if (function_exists('locale_accept_from_http')) {
             return locale_accept_from_http($accepted_languages);
         } else {
             $parsed_languages = array();
             if (preg_match_all('#([a-z]{1,8})(-[a-z]{1,8})*\\s*(;\\s*q\\s*=\\s*((1(\\.0{0,3}))|(0(\\.[0-9]{0,3}))))?#i', $accepted_languages, $parsed_languages)) {
                 $languages = isset($parsed_languages[1]) ? $parsed_languages[1] : array();
                 $countries = isset($parsed_languages[2]) ? $parsed_languages[2] : array();
                 $qualities = isset($parsed_languages[4]) ? $parsed_languages[4] : array();
                 $temp = array();
                 if ($languages) {
                     for ($i = 0, $len = count($languages); $i < $len; $i++) {
                         if (isset($countries[$i]) && !empty($countries[$i])) {
                             $language = strtolower($languages[$i]);
                             $country = strtoupper($countries[$i]);
                             $quality = isset($qualities[$i]) ? empty($qualities[$i]) ? 1.0 : floatval($qualities[$i]) : 0.0;
                             $locale = str_replace('-', '_', $language . $country);
                             $temp[$locale] = isset($temp[$locale]) ? max($temp[$locale], $quality) : $quality;
                         }
                     }
                 }
                 arsort($temp, SORT_NUMERIC);
                 $keys = array_keys($temp);
                 return array_shift($keys);
             }
         }
     }
     return false;
 }
Beispiel #13
0
 /**
  * Returns the user's preferred language from the browser
  *
  * @return string|null the preferred language from the browser or NULL
  */
 public static function getBrowserLanguage()
 {
     if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
         return locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
     } else {
         return NULL;
     }
 }
Beispiel #14
0
 protected function getDefaultLocale()
 {
     $defaultLocale = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
     return isset($this->locales[$defaultLocale]) ? $defaultLocale : (substr($defaultLocale, 0, 2) === 'fr' ? 'fr_FR' : 'en_US');
 }
Beispiel #15
0
 private function getClientLocaleDefault()
 {
     $acceptLanguageHeader = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : 'en-US';
     $localeDefault = \extension_loaded('intl') ? \locale_accept_from_http($acceptLanguageHeader) : 'en-US';
     if (!in_array(substr($localeDefault, 0, 2), $this->dc['l10n.available_languages'])) {
         $localeDefault = 'en-US';
     }
     return str_replace('_', '-', $localeDefault);
 }
Beispiel #16
0
 /**
  * Return how munch memory usage
  * @name run
  * @return string
  */
 public function run() {
     $language = (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) ? locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']) : 'None';
     echo json_encode(array(
         array("id" => "#server-language", "value" => $language)
     ));
 }
 public function _setLanguage()
 {
     $fieldLanguage = null;
     if ($this->Cookie->read('language')) {
         $lang = $this->Cookie->read('language');
     } else {
         $lang = $this->request->session()->read('Config.language');
         if (!$lang) {
             // Automatic detect locale for user
             $lang = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
             switch ($lang) {
                 case 'ja':
                     $lang = 'ja_JP';
                     break;
                 case 'vi':
                     $lang = 'vi_VN';
                     break;
                 case 'en':
                     $lang = 'en_US';
                     break;
             }
             $this->request->session()->write('Config.language', $lang);
         }
     }
     switch ($lang) {
         case 'ja_JP':
             $fieldLanguage = 'jpn';
             break;
         case 'vi_VN':
             $fieldLanguage = 'vie';
             break;
         case 'en_US':
             $fieldLanguage = 'eng';
             break;
     }
     $this->fieldLanguage = $fieldLanguage;
     $this->loadModel('JcApiModel');
     $this->JcApiModel->fieldLanguage = $this->fieldLanguage;
     $this->set(compact('lang', 'fieldLanguage'));
     I18n::locale($lang);
     $this->Cookie->write('language', $lang);
     Configure::load('constant', 'default', false);
 }
Beispiel #18
0
<?php

/**
 * Appliacation Init
 * @author vitex
 * @copyright Vitex@hippy.cz (G) 2010
 */
namespace SkeliCZ;

require_once 'includes/Configure.php';
require_once '../vendor/autoload.php';
//Initialise Gettext
$langs = ['en_US' => ['en', 'English (International)'], 'cs_CZ' => ['cs', 'Česky (Čeština)']];
$locale = "en_US";
if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) {
    $locale = locale_accept_from_http($_SERVER["HTTP_ACCEPT_LANGUAGE"]);
}
if (isset($_GET["locale"])) {
    $locale = preg_replace("/[^a-zA-Z_]/", "", substr($_GET["locale"], 0, 10));
}
foreach ($langs as $code => $lang) {
    if ($locale == $lang[0]) {
        $locale = $code;
    }
}
setlocale(LC_ALL, $locale);
bind_textdomain_codeset("skelicz", "UTF-8");
putenv("LC_ALL={$locale}");
bindtextdomain('skelicz', './i18n');
textdomain('skelicz');
session_start();
Beispiel #19
0
 /**
  * Returns the request language tag
  *
  * Should return a properly formatted IETF language tag, eg xx-XX
  * @link https://en.wikipedia.org/wiki/IETF_language_tag
  * @link https://tools.ietf.org/html/rfc5646
  *
  * @return string
  */
 public function getLanguage()
 {
     if (!($language = $this->getUser()->getLanguage())) {
         if ($this->_headers->has('Accept-Language')) {
             $language = locale_accept_from_http($this->_headers->get('Accept-Language'));
         } else {
             $language = $this->getConfig()->language;
         }
     }
     return $language;
 }
<?php

$lang = "";
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
    $lang = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
}
if (strpos($lang, 'fr') !== 0) {
    header("location: http://localhost/Portfolio/app/en/index.html");
    exit;
} else {
    header("location: http://localhost/Portfolio/app/index.html");
    exit;
}
Beispiel #21
0
 public function _setLanguage()
 {
     $lang = $this->request->session()->read('Config.language');
     if (!$lang) {
         // Automatic detect locale for user
         $lang = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
         switch ($lang) {
             case 'ja':
                 $lang = 'ja_JP';
                 break;
             case 'vi':
                 $lang = 'vi_VN';
                 break;
             case 'en':
                 $lang = 'en_US';
                 break;
         }
         $this->request->session()->write('Config.language', $lang);
     }
     $this->set('lang', $lang);
     I18n::locale($lang);
     \Cake\Core\Configure::load('constant', 'default', false);
 }