Ejemplo n.º 1
0
 /**
  * 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());
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * @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());
 }
Ejemplo n.º 3
0
 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');
 }
Ejemplo n.º 4
0
 /**
  * {@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;
 }
Ejemplo n.º 5
0
 /**
  * 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;
 }
Ejemplo n.º 6
0
 /**
  * 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);
 }
Ejemplo n.º 7
0
 /**
  * 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;
 }
Ejemplo n.º 8
0
 public function flushLocaleCss()
 {
     foreach ($this->locales->getLocales() as $locale => $info) {
         $this->getLocaleCss($locale)->flush();
     }
 }
Ejemplo n.º 9
0
 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);
 }
Ejemplo n.º 10
0
 /**
  * @param WebAppView $view
  */
 private function addTranslations(WebAppView $view)
 {
     $translations = array_get($this->locales->getTranslator()->getMessages(), 'messages', []);
     $translations = $this->filterTranslations($translations);
     $view->getLocaleJs()->setTranslations($translations);
 }
Ejemplo n.º 11
0
 public function addConfig($locale, $file)
 {
     $this->manager->addConfig($locale, $file);
 }
Ejemplo n.º 12
0
 public function addTranslations($locale, $file)
 {
     $this->manager->addTranslations($locale, $file);
 }