Example #1
0
 /**
  * TODO: Limit catalogue if maintenance mode is enabled?
  * @Route("/{locale}", requirements={"locale"="[a-zA-Z0-9_-]+"}, defaults={"_maintenance" = true})
  * @Request({"locale"})
  */
 public function indexAction($locale = null)
 {
     $intl = App::module('system/intl');
     $intl->loadLocale($locale);
     $messages = $intl->getFormats($locale) ?: [];
     $messages['locale'] = $locale;
     $messages['translations'] = [$locale => App::translator()->getCatalogue($locale)->all()];
     $messages = json_encode($messages);
     $request = App::request();
     $json = $request->isXmlHttpRequest();
     $response = $json ? App::response()->json() : App::response('', 200, ['Content-Type' => 'application/javascript']);
     $response->setETag(md5($json . $messages))->setPublic();
     if ($response->isNotModified($request)) {
         return $response;
     }
     return $response->setContent($json ? $messages : sprintf('var $locale = %s;', $messages));
 }
Example #2
0
 /**
  * Loads language files.
  *
  * @param string              $locale
  * @param TranslatorInterface $translator
  */
 public function loadLocale($locale, TranslatorInterface $translator = null)
 {
     $translator = $translator ?: App::translator();
     foreach (App::module() as $module) {
         $domains = [];
         $path = $module->get('path') . ($module->get('languages') ?: '/languages');
         $files = glob("{$path}/{$locale}/*.php") ?: [];
         foreach ($files as $file) {
             $format = substr(strrchr($file, '.'), 1);
             $domain = basename($file, '.' . $format);
             if (in_array($domain, $domains)) {
                 continue;
             }
             $domains[] = $domain;
             $translator->addResource($format, $file, $locale, $domain);
         }
     }
 }
Example #3
0
 /**
  * Translates the given choice message by choosing a translation according to a number, alias for method transChoice()
  */
 function _c($id, $number, array $parameters = [], $domain = null, $locale = null)
 {
     return App::translator()->transChoice($id, $number, $parameters, $domain, $locale);
 }