/** * Changes the locale in the application and optionally stores it in the session. * @param string $locale Locale to use * @param boolean $remember Set to false to not store in the session. * @return boolean Returns true if the locale exists and is set. */ public function setLocale($locale, $remember = true) { if (!Locale::isValid($locale)) { return false; } App::setLocale($locale); $this->activeLocale = $locale; if ($remember) { $this->setSessionLocale($locale); } return true; }
/** * Returns the path prefixed with language code. * * @param string $path Path to rewrite * @param string $locale optional language code, default to the system default language * @return string */ public function getPathInLocale($path, $locale = null) { $segments = explode('/', $path); $segments = array_values(array_filter($segments, function ($v) { return $v != ''; })); if (is_null($locale) || !Locale::isValid($locale)) { $locale = $this->defaultLocale; } if (count($segments) == 0 || Locale::isValid($segments[0])) { $segments[0] = $locale; } else { array_unshift($segments, $locale); } return implode('/', $segments); }
/** * Returns the current path prefixed with language code. * * @param string $locale optional language code, default to the system default language * @return string */ public function getCurrentPathInLocale($locale = null) { if (is_null($locale) || !Locale::isValid($locale)) { $locale = $this->defaultLocale; } $segments = Request::segments(); if (count($segments) == 0 || Locale::isValid($segments[0])) { $segments[0] = $locale; } else { array_unshift($segments, $locale); } return implode('/', $segments); }
<?php use RainLab\Translate\Models\Locale; use RainLab\Translate\Models\Message; use RainLab\Translate\Classes\Translator; /* * Adds a custom route to check for the locale prefix. */ App::before(function ($request) { $translator = Translator::instance(); if (!$translator->isConfigured()) { return; } $locale = Request::segment(1); if (!Locale::isValid($locale)) { return; } $translator->setLocale($locale); /* * Register routes */ Route::group(['prefix' => $locale], function () { Route::any('{slug}', 'Cms\\Classes\\CmsController@run')->where('slug', '(.*)?'); }); Route::any($locale, 'Cms\\Classes\\CmsController@run'); /* * Ensure Url::action() retains the localized URL * by re-registering the route after the CMS. */ Event::listen('cms.route', function () use($locale) { Route::group(['prefix' => $locale], function () {