示例#1
0
<?php

if (strcmp(PHP_SAPI, 'cli') !== 0) {
    exit("This script must be run from the command line\n");
}
define('BASEDIR', __DIR__);
define('VERSION', '3.0.0 RC4');
define('MAILADDR', 'translator at xtrafile.com');
update_core(BASEDIR, 'xtraupload');
merge_translations(BASEDIR);
function update_core($dir, $domain)
{
    $potfile = BASEDIR . "/xtraupload.pot";
    $version = VERSION;
    $mailaddr = MAILADDR;
    $old = getcwd();
    chdir($dir);
    $application = realpath(BASEDIR . "/../../../application");
    unlink($potfile);
    passthru(<<<END
xgettext \\
\t--from-code=UTF-8 \\
\t--default-domain={$domain} \\
\t--output={$potfile} \\
\t--language=PHP \\
\t--copyright-holder="XtraUpload" \\
\t--package-name="XtraUpload" \\
\t--package-version="{$version}" \\
\t--msgid-bugs-address="{$mailaddr}" \\
\t--add-comments=HINT: \\
\t--keyword="nlang:1,1t" \\
    /**
     * Execute the console command.
     *
     * @return void
     */
    public function fire()
    {
        $this->generator->setOptions($this->option());
        $langPath = $this->getPath();
        $scaffold = $this->option('lang');
        if (!$scaffold) {
            $template = $this->option('template');
            $locales = getDirs($langPath, true);
            foreach ($locales as $locale) {
                if ($locale === 'en/') {
                    $path = $this->getPath($locale);
                    $this->printResult($this->generator->make($path, $template, $finalPath), $path, $finalPath);
                }
            }
        } else {
            // add whatever else is specified in the template/lang to appropriate groups
            $langTemplatePath = $scaffold;
            if ($langTemplatePath) {
                $files = getFiles($langTemplatePath, "*.txt", false);
                $config = \Config::get(GeneratorsServiceProvider::PACKAGE, null);
                foreach ($files as $file) {
                    // here we have group.php to map to lang/en/group.php
                    // and create all the lines in the group
                    $group = basename($file, ".txt");
                    $langTemplate = file_get_contents($langTemplatePath . "/" . $file);
                    $locales = getDirs($langPath, true);
                    foreach ($locales as $locale) {
                        if ($locale === 'en/') {
                            // get the file we are to add to
                            $path = $this->getLangFile($group, $locale);
                            $configRewriter = new TranslationFileRewriter();
                            $exportOptions = array_key_exists('export_format', $config) ? TranslationFileRewriter::optionFlags($config['export_format']) : null;
                            $translationModsText = $this->generator->getLangTemplate($langTemplate);
                            $translationMods = <<<PHP
return array(
{$translationModsText}
);
PHP;
                            try {
                                $newTranslations = eval($translationMods);
                                if ($newTranslations) {
                                    $translations = file_exists($path) ? require $path : [];
                                    $overwrite = $this->option('overwrite');
                                    $needUpdate = merge_translations($translations, $newTranslations, $overwrite);
                                    if ($needUpdate) {
                                        $configRewriter->parseSource(file_exists($path) ? file_get_contents($path) : '');
                                        $output = $configRewriter->formatForExport($translations, $exportOptions);
                                        if (file_put_contents($path, $output) !== false) {
                                            $this->info("Updated translations from template/lang/{$file} in {$path}");
                                        } else {
                                            $this->error("Failed to update translations from template/lang/{$file} in {$path}");
                                        }
                                    } else {
                                        $this->info("Did not need to update changes from template/lang/{$file} in {$path}");
                                    }
                                } else {
                                    $this->error("failed to evaluate changes from template/lang/{$file} for {$path}");
                                }
                            } catch (FatalErrorException $e) {
                                $this->error("failed to evaluate changes from template/lang/{$file}, exception " . $e->getMessage());
                            }
                        }
                    }
                }
            }
        }
    }
示例#3
0
 function merge_translations(&$array1, $array2, $overwrite)
 {
     $hadChanges = false;
     foreach ($array2 as $key => $value) {
         if ($overwrite || !array_key_exists($key, $array1)) {
             $array1[$key] = $value;
             $hadChanges = true;
         } else {
             if (is_array($value) && is_array($array1[$key])) {
                 if (merge_translations($array1[$key], $value, $overwrite)) {
                     $hadChanges = true;
                 }
             }
         }
     }
     return $hadChanges;
 }