Mcamara\LaravelLocalization\LaravelLocalization::setLocale PHP 메소드

setLocale() 공개 메소드

Set and return current locale
public setLocale ( string $locale = null ) : string
$locale string Locale to set the App to (optional)
리턴 string Returns locale (if route has any) or null (if route does not have a locale)
    public function setLocale($locale = null)
    {
        if (empty($locale) || !is_string($locale)) {
            // If the locale has not been passed through the function
            // it tries to get it from the first segment of the url
            $locale = $this->request->segment(1);
        }
        if (!empty($this->supportedLocales[$locale])) {
            $this->currentLocale = $locale;
        } else {
            // if the first segment/locale passed is not valid
            // the system would ask which locale have to take
            // it could be taken by the browser
            // depending on your configuration
            $locale = null;
            // if we reached this point and hideDefaultLocaleInURL is true
            // we have to assume we are routing to a defaultLocale route.
            if ($this->hideDefaultLocaleInURL()) {
                $this->currentLocale = $this->defaultLocale;
            } else {
                $this->currentLocale = $this->getCurrentLocale();
            }
        }
        $this->app->setLocale($this->currentLocale);
        // Regional locale such as de_DE, so formatLocalized works in Carbon
        $regional = $this->getCurrentLocaleRegional();
        if ($regional) {
            setlocale(LC_TIME, $regional . '.UTF-8');
            setlocale(LC_MONETARY, $regional . '.UTF-8');
        }
        return $locale;
    }

Usage Example

예제 #1
0
 /**
  * Set and return current locale
  *
  * @param string $locale Locale to set the App to (optional)
  * @return string Returns locale (if route has any) or null (if route does not have a locale)
  * @static 
  */
 public static function setLocale($locale = null)
 {
     return \Mcamara\LaravelLocalization\LaravelLocalization::setLocale($locale);
 }