diff options
author | Filip Komar <filip@mageia.org> | 2014-03-01 18:15:17 +0000 |
---|---|---|
committer | Filip Komar <filip@mageia.org> | 2014-03-01 18:15:17 +0000 |
commit | fea2eca9cb50ba0dd1058358bc0feb58304a3d80 (patch) | |
tree | 14473e0a8ed3499e559ff4e957a5ecdbf42dfa3a /tools | |
parent | 1f3db960405e08e0f707a5de5810c3c12040709d (diff) | |
download | www-fea2eca9cb50ba0dd1058358bc0feb58304a3d80.tar www-fea2eca9cb50ba0dd1058358bc0feb58304a3d80.tar.gz www-fea2eca9cb50ba0dd1058358bc0feb58304a3d80.tar.bz2 www-fea2eca9cb50ba0dd1058358bc0feb58304a3d80.tar.xz www-fea2eca9cb50ba0dd1058358bc0feb58304a3d80.zip |
first step towards transition from lang files to gettext translation system
Diffstat (limited to 'tools')
-rw-r--r-- | tools/README | 11 | ||||
-rw-r--r-- | tools/extract2gettext.php | 126 | ||||
-rw-r--r-- | tools/lookup_for_second_argument.php | 61 | ||||
-rwxr-xr-x | tools/lookup_for_second_argument.sh | 18 | ||||
-rw-r--r-- | tools/web_projects.dat | 24 |
5 files changed, 240 insertions, 0 deletions
diff --git a/tools/README b/tools/README new file mode 100644 index 000000000..453913ddb --- /dev/null +++ b/tools/README @@ -0,0 +1,11 @@ +REBUILDING A GETTEXT DICTIONARIES + +To rebuild a gettext dictionaries you need to run rebuild_gettext_catalogs.sh script in the root of main web server: + +It cals extract2gettext.php for every translatable resource. It picks the database for that from web_projects.dat file. + + +LEGACY UTILITY +There is also useful script lookup_for_second_argument.sh used as a preparation for transition. + +It cals lookup_for_second_argument.php for every translatable resource. It picks the database for that from web_projects.dat file. diff --git a/tools/extract2gettext.php b/tools/extract2gettext.php new file mode 100644 index 000000000..559285573 --- /dev/null +++ b/tools/extract2gettext.php @@ -0,0 +1,126 @@ +<?php +/** + * Lookup for _g() and _r() in php source code and build a gettext catalog (pot file) + * + * PHP 5.3 + * + * @license MIT + * @author Filip (transformation to build a gettext catalog) + * @copyright 2014/02 + * based on extract2lang.php by author Romain d'Alverny, rdalverny, rda +*/ + +if (php_sapi_name() !== 'cli') { + echo "CLI only.\n"; + exit(1); +} + +if (version_compare(PHP_VERSION, '5.3.0') < 0) { + echo "PHP 5.3 needed.\n"; + exit(1); +} + +define('APP_ROOT', realpath(__DIR__ . '/..')); + +$php_source = isset($argv[1]) ? $argv[1] : null; +$domain = isset($argv[2]) ? $argv[2] : null; + +if (is_null($php_source) || is_null($domain)) { + echo <<<U +Usage: + php tools/extract2gettext.php path/to/source.php domain_name + + Example: + php tools/extract2gettext.php en/index.php index + + You can join multiple sources with apostrophe - single quote (') like this: + 'en/3/download_index.php en/for-pc/index.php en/for-server/index.php en/3/index.php en/3/nav.php' + +U; + exit(1); +} + +date_default_timezone_set(@date_default_timezone_get()); + +echo "ohay!\n"; + +$cmd = sprintf('grep -HrnEi "_(g|r)\((.*)" %s', $php_source); +//echo $cmd, "\n"; +exec($cmd, $out); + +$strings = array(); +$f = array(); +$error = 0; + +foreach ($out as $str) { + $arr = explode(':', $str); + $file = array_shift($arr); + $file = str_replace(APP_ROOT, '', $file); + $files[] = $file; + $line = array_shift($arr); + $arr = implode(':', $arr); + $arr = str_replace('\\\'', '_apostrophe_', $arr); + + if (preg_match_all('/\_(g|r)\(\'(.+)\'/imU', $arr, $reg)) { // i=PCRE_CASELESS, m=PCRE_MULTILINE, U=PCRE_UNGREEDY + foreach ($reg[2] as $found) { + $found = str_replace('_apostrophe_', '\'', $found); + $strings[$domain][$found][] = $file . ' +' . $line; + } + } + else { + echo "\n\n!!!! Could not find string in $file, line $line: $str\n"; + echo "!!!! Please fix $file before using $domain.pot file!!!\n\n"; + $error = 2; + } +} + +foreach ($strings as $domain => $strs) { + $f[] = '# gettext catalog for ' . $domain . ' web page(s)'; + $f[] = '# Copyright (C) 2014 - ' . date('Y') . ' Mageia'; + $f[] = '# This file is distributed under the same license as'; + $f[] = '# the content of the corresponding web page(s).'; + $f[] = '#'; + $f[] = sprintf('# Generated by extract2gettext.php on %s', $cur_date = date('Y m d H:i:sO')); + $f[] = sprintf('# Domain: %s', $domain); + $f[] = '#'; + $f[] = '# include translation strings from:'; + $files = array_unique($files); + foreach ($files as $source) { + $f[] = sprintf('# %s', $source); + } + $f[] = '#'; + $f[] = 'msgid ""'; + $f[] = 'msgstr ""'; + $f[] = '"Project-Id-Version: ' . $domain . '\n"'; + $f[] = '"Report-Msgid-Bugs-To: mageia-i18n@mageia.org\n"'; + $f[] = '"POT-Creation-Date: ' . $cur_date . '\n"'; + $f[] = '"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"'; + $f[] = '"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"'; + $f[] = '"Language-Team: LANGUAGE <LL@li.org>\n"'; + $f[] = '"Language: \n"'; + $f[] = '"MIME-Version: 1.0\n"'; + $f[] = '"Content-Type: text/plain; charset=CHARSET\n"'; + $f[] = '"Content-Transfer-Encoding: 8bit\n"'; + $f[] = ''; + + foreach ($strs as $str => $info) { + $str = str_replace(array("\'", '"'), array("'", '\"'), $str); + $f[] = sprintf('msgctxt "%s"', $info[0]); + $f[] = 'msgid "' . $str . '"'; + $f[] = 'msgstr ""'; + $f[] = ''; + } + $f = implode("\n", $f); + $dest = sprintf('%s/langs/en/%s.pot', APP_ROOT, $domain); + $dir = dirname($dest); + + if (!is_dir($dir)) { + echo "making $dir\n"; + mkdir($dir, 0755, true); + } + echo sprintf("saved %d strings in %s\n", count($strs), $dest); + if (FALSE === file_put_contents($dest, $f)) + echo "Failed to write.\n"; +} +echo "Done. thxbye.\n"; +exit($error); diff --git a/tools/lookup_for_second_argument.php b/tools/lookup_for_second_argument.php new file mode 100644 index 000000000..97ce33728 --- /dev/null +++ b/tools/lookup_for_second_argument.php @@ -0,0 +1,61 @@ +<?php +/** + * Lookup for second argument in functions _t() and _d() in source code + * + * PHP 5.3 + * + * @license MIT + * @author Filip (transformation to build a gettext dictionary) + * @copyright 2014/02 + * based on extract2lang.php by author Romain d'Alverny, rdalverny, rda +*/ + +if (php_sapi_name() !== 'cli') { + echo "CLI only.\n"; + exit(1); +} + +if (version_compare(PHP_VERSION, '5.3.0') < 0) { + echo "PHP 5.3 needed.\n"; + exit(1); +} + +$php_source = isset($argv[1]) ? $argv[1] : null; +$domain = isset($argv[2]) ? $argv[2] : null; + +if (is_null($php_source) || is_null($domain)) { + echo <<<U +Usage: + php tools/lookup_for_second_argument.php path/to/source.php domain_name + + Example: + php tools/lookup_for_second_argument.php en/index.php index + + You can join multiple sources with apostrophe - single quote (') like this: + 'en/3/download_index.php en/for-pc/index.php en/for-server/index.php en/3/index.php en/3/nav.php' + +U; + exit(1); +} + +date_default_timezone_set(@date_default_timezone_get()); + +$cmd = sprintf('grep -HrnEi "_(e|t|h|d)\((.*)" %s', $php_source); +exec($cmd, $out); + +foreach ($out as $str) { + $arr = explode(':', $str); + $file = array_shift($arr); + $line = array_shift($arr); + $arr = implode(':', $arr); + $arr = str_replace('\\\'', '_apostrophe_', $arr); + if (preg_match_all('/\_(t|d)\(\'(.+)\'(,.+)$/im', $arr, $test)) { // qq i=PCRE_CASELESS, m=PCRE_MULTILINE, U=PCRE_UNGREEDY + foreach ($test[3] as $more_arg) { // qq + $att = $php_source . ' (line ' . $line . '): '. trim($arr) . PHP_EOL; // qq + $attention[] = $att; + echo $att . PHP_EOL; + } + } +} + +exit(0); diff --git a/tools/lookup_for_second_argument.sh b/tools/lookup_for_second_argument.sh new file mode 100755 index 000000000..30bcc1034 --- /dev/null +++ b/tools/lookup_for_second_argument.sh @@ -0,0 +1,18 @@ +#!/bin/bash +# Filip Komar, 2014 +# @license GPL v2 +# @author Filip (prepare for transformation to gettext translation system) +# @copyright 2014/03 +# inspired by check_for_translation_work.sh + +declare -A resources + +source ./tools/web_projects.dat + +for resource in "${!resources[@]}" +do + php_source=${resources[$resource]} + php tools/lookup_for_second_argument.php $php_source $resource +done +echo '' +echo "Done lookup for second argument in functions _t() and _d() in the source code" diff --git a/tools/web_projects.dat b/tools/web_projects.dat new file mode 100644 index 000000000..b00d2b339 --- /dev/null +++ b/tools/web_projects.dat @@ -0,0 +1,24 @@ +resources[about/code-of-conduct]="en/about/code-of-conduct/index.php" +resources[about/constitution]="en/about/constitution/index.php" +resources[about/license]="en/about/license/index.php" +resources[about/media]="en/about/media/index.php" +resources[about/reports]="en/about/reports/index.php" +resources[about/values]="en/about/values/index.php" +resources[downloads/get]="en/downloads/get/index.php" +resources[2]="en/2/download_index.php en/2/for-pc/index.php en/2/for-server/index.php en/2/index.php en/2/nav.php" +resources[3]="en/3/download_index.php en/for-pc/index.php en/for-server/index.php en/3/index.php en/3/nav.php" +resources[4]="en/4/download_index.php en/4/nav.php en/4/index.php" +resources[404]="404.php" +resources[about]="en/about/index.php" +resources[calendar]="en/calendar/index.php" +resources[community]="en/community/index.php" +resources[contact]="en/contact/index.php" +resources[contribute]="en/contribute/index.php" +resources[documentation]="en/doc/index.php en/doc/archive.php en/doc/doc.php" +resources[donate]="en/donate/index.php" +resources[index]="en/index.php" +resources[map]="en/map/index.php" +resources[support]="en/support/index.php" +resources[thank-you]="en/thank-you/index.php" +resources[timeline]="en/timeline/index.php" +resources[mognase]="_nav/lib.php" |