/** * Load language pack resources from the given directory. * * @param string $directory */ public function loadLanguagePackFrom($directory) { $name = $title = basename($directory); if (file_exists($manifest = $directory . '/composer.json')) { $json = json_decode(file_get_contents($manifest), true); if (empty($json)) { throw new RuntimeException("Error parsing composer.json in {$name}: " . json_last_error_msg()); } $locale = array_get($json, 'extra.flarum-locale.code'); $title = array_get($json, 'extra.flarum-locale.title', $title); } if (!isset($locale)) { throw new RuntimeException("Language pack {$name} must define \"extra.flarum-locale.code\" in composer.json."); } $this->locales->addLocale($locale, $title); if (!is_dir($localeDir = $directory . '/locale')) { throw new RuntimeException("Language pack {$name} must have a \"locale\" subdirectory."); } if (file_exists($file = $localeDir . '/config.js')) { $this->locales->addJsFile($locale, $file); } if (file_exists($file = $localeDir . '/config.css')) { $this->locales->addCssFile($locale, $file); } foreach (new DirectoryIterator($localeDir) as $file) { if ($file->isFile() && in_array($file->getExtension(), ['yml', 'yaml'])) { $this->locales->addTranslations($locale, $file->getPathname()); } } }
/** * @param Request $request * @return Request */ protected function logIn(Request $request) { $header = $request->getHeaderLine('authorization'); $parts = explode(';', $header); $actor = new Guest(); if (isset($parts[0]) && starts_with($parts[0], $this->prefix)) { $token = substr($parts[0], strlen($this->prefix)); if (($accessToken = AccessToken::find($token)) && $accessToken->isValid()) { $actor = $accessToken->user; $actor->updateLastSeen()->save(); } elseif (isset($parts[1]) && ($apiKey = ApiKey::valid($token))) { $userParts = explode('=', trim($parts[1])); if (isset($userParts[0]) && $userParts[0] === 'userId') { $actor = User::find($userParts[1]); } } } if ($actor->exists) { $locale = $actor->getPreference('locale'); } else { $locale = array_get($request->getCookieParams(), 'locale'); } if ($locale && $this->locales->hasLocale($locale)) { $this->locales->setLocale($locale); } return $request->withAttribute('actor', $actor ?: new Guest()); }
public function registerLocale(LocaleManager $manager, $locale, $title) { $path = __DIR__ . '/../../locale/' . $locale; $manager->addLocale($locale, $title); $manager->addTranslations($locale, $path . '.yml'); $manager->addConfig($locale, $path . '.php'); $manager->addJsFile($locale, $path . '.js'); }
/** * {@inheritdoc} */ public function __invoke(Request $request, Response $response, callable $out = null) { $actor = $request->getAttribute('actor'); if ($actor->exists) { $locale = $actor->getPreference('locale'); } else { $locale = array_get($request->getCookieParams(), 'locale'); } if ($locale && $this->locales->hasLocale($locale)) { $this->locales->setLocale($locale); } return $out ? $out($request, $response) : $response; }
/** * Set up the locale compiler for the given locale. * * @param string $locale * @return LocaleJsCompiler */ protected function getLocaleCompiler($locale) { $compiler = new LocaleJsCompiler($this->getAssetDirectory(), "{$this->clientName}-{$locale}.js", $this->app->config('debug'), $this->cache); foreach ($this->locales->getJsFiles($locale) as $file) { $compiler->addFile($file); } return $compiler; }
/** * Set the application's actor instance according to the request token. * * @param Request $request * @return Request */ protected function logIn(Request $request) { $actor = new Guest(); if ($token = $this->getToken($request)) { if (!$token->isValid()) { // TODO: https://github.com/flarum/core/issues/253 } elseif ($actor = $token->user) { $actor->updateLastSeen()->save(); } } if ($actor->exists) { $locale = $actor->getPreference('locale'); } else { $locale = array_get($request->getCookieParams(), 'locale'); } if ($locale && $this->locales->hasLocale($locale)) { $this->locales->setLocale($locale); } return $request->withAttribute('actor', $actor); }
/** * Get the name of the locale to use. * * @param User $actor * @param Request $request * @return string */ protected function getLocale(User $actor, Request $request) { if ($actor->exists) { $locale = $actor->getPreference('locale'); } else { $locale = array_get($request->getCookieParams(), 'locale'); } if (!$locale || !$this->locales->hasLocale($locale)) { return $this->settings->get('default_locale', 'en'); } return $locale; }
public function flushLocaleCss() { foreach ($this->locales->getLocales() as $locale => $info) { $this->getLocaleCss($locale)->flush(); } }
protected function buildPayload(Request $request, $forum) { $data = $this->getDataFromDocument($forum); if ($request->getAttribute('actor')->exists) { $user = $this->getUserDocument($request); $data = array_merge($data, $this->getDataFromDocument($user)); } $payload = ['resources' => $data, 'session' => $this->buildSession($request), 'document' => $this->document, 'locales' => $this->locales->getLocales(), 'locale' => $this->locales->getLocale()]; return array_merge($payload, $this->variables); }
/** * @param WebAppView $view */ private function addTranslations(WebAppView $view) { $translations = array_get($this->locales->getTranslator()->getMessages(), 'messages', []); $translations = $this->filterTranslations($translations); $view->getLocaleJs()->setTranslations($translations); }
public function addConfig($locale, $file) { $this->manager->addConfig($locale, $file); }
public function addTranslations($locale, $file) { $this->manager->addTranslations($locale, $file); }