當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Translations::addFromJsCodeFile方法代碼示例

本文整理匯總了PHP中Gettext\Translations::addFromJsCodeFile方法的典型用法代碼示例。如果您正苦於以下問題:PHP Translations::addFromJsCodeFile方法的具體用法?PHP Translations::addFromJsCodeFile怎麽用?PHP Translations::addFromJsCodeFile使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Gettext\Translations的用法示例。


在下文中一共展示了Translations::addFromJsCodeFile方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: execute

 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $container = $this->getContainer();
     $filesystem = new Filesystem();
     $bundleAlias = $input->getArgument("bundle");
     $bundlePath = $container->get("agit.common.filecollector")->resolve($bundleAlias);
     $defaultLocale = $container->get("agit.intl.locale")->getDefaultLocale();
     $locales = $input->getArgument("locales") ? array_map("trim", explode(",", $input->getArgument("locales"))) : $container->getParameter("agit.intl.locales");
     $globalCatalogPath = $container->getParameter("kernel.root_dir") . "/{$this->catalogSubdir}";
     $this->cacheBasePath = sprintf("%s/agit.intl.temp/%s", sys_get_temp_dir(), $bundleAlias);
     $filesystem->mkdir($this->cacheBasePath);
     $finder = (new Finder())->in("{$bundlePath}")->name("*\\.php")->name("*\\.js")->notPath("/test.*/i")->notPath("public/js/ext");
     $files = [];
     foreach ($finder as $file) {
         $filePath = $file->getRealpath();
         $alias = str_replace($bundlePath, "@{$bundleAlias}/", $filePath);
         $files[$filePath] = $alias;
     }
     $this->getContainer()->get("event_dispatcher")->dispatch("agit.intl.bundle.files", new BundleTranslationFilesEvent($this, $bundleAlias, $this->cacheBasePath));
     $this->extraTranslations = new Translations();
     $this->getContainer()->get("event_dispatcher")->dispatch("agit.intl.bundle.translations", new BundleTranslationsEvent($this, $bundleAlias));
     $files += $this->extraSourceFiles;
     $frontendFiles = array_filter(array_keys($files), function ($file) {
         return preg_match("|\\.js\$|", $file);
     });
     $backendFiles = array_filter(array_keys($files), function ($file) {
         return !preg_match("|\\.js\$|", $file);
     });
     $frontendCatalogs = "";
     foreach ($locales as $locale) {
         if (!preg_match("|^[a-z]{2}_[A-Z]{2}|", $locale)) {
             throw new Exception("Invalid locale: {$locale}");
         }
         // we use the global catalog as source for already translated strings
         $globalCatalogFile = "{$globalCatalogPath}/{$locale}/LC_MESSAGES/agit.po";
         $globalCatalog = $filesystem->exists($globalCatalogFile) ? $this->deleteReferences(Translations::fromPoFile($globalCatalogFile)) : new Translations();
         $bundleCatalogFile = "{$bundlePath}/{$this->catalogSubdir}/bundle.{$locale}.po";
         $oldBundleCatalog = $filesystem->exists($bundleCatalogFile) ? $this->deleteReferences(Translations::fromPoFile($bundleCatalogFile)) : new Translations();
         // NOTE: we delete all headers and only set language, in order to avoid garbage commits
         $bundleCatalog = new Translations();
         $bundleCatalog->deleteHeaders();
         $bundleCatalog->setLanguage($locale);
         // first: only JS messages
         foreach ($frontendFiles as $file) {
             $bundleCatalog->addFromJsCodeFile($file, $this->extractorOptions);
         }
         $bundleCatalog->mergeWith($oldBundleCatalog, 0);
         $bundleCatalog->mergeWith($globalCatalog, 0);
         if ($bundleCatalog->count() && $locale !== $defaultLocale) {
             $transMap = [];
             foreach ($bundleCatalog as $entry) {
                 $msgid = ltrim($entry->getId(), "");
                 $msgstr = $entry->getTranslation();
                 $transMap[$msgid] = $entry->hasPlural() ? array_merge([$msgstr], $entry->getPluralTranslations()) : $msgstr;
             }
             $frontendCatalogs .= sprintf("ag.intl.register(\"%s\", %s);\n\n", $locale, json_encode($transMap, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
         }
         // now the same with all messages
         foreach ($backendFiles as $file) {
             $bundleCatalog->addFromPhpCodeFile($file, $this->extractorOptions);
         }
         $bundleCatalog->mergeWith($this->extraTranslations, Merge::ADD);
         $bundleCatalog->mergeWith($oldBundleCatalog, 0);
         $bundleCatalog->mergeWith($globalCatalog, 0);
         $catalog = $bundleCatalog->toPoString();
         $catalog = str_replace(array_keys($files), array_values($files), $catalog);
         if ($bundleCatalog->count()) {
             $filesystem->dumpFile("{$bundlePath}/{$this->catalogSubdir}/bundle.{$locale}.po", $catalog);
         }
     }
     if ($frontendCatalogs) {
         $filesystem->dumpFile("{$bundlePath}/{$this->frontendSubdir}/translations.js", $frontendCatalogs);
     }
     $filesystem->remove($this->cacheBasePath);
 }
開發者ID:agitation,項目名稱:intl-bundle,代碼行數:75,代碼來源:BundleCatalogCommand.php


注:本文中的Gettext\Translations::addFromJsCodeFile方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。