aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFilip Komar <filip@mageia.org>2014-03-01 18:15:17 +0000
committerFilip Komar <filip@mageia.org>2014-03-01 18:15:17 +0000
commitfea2eca9cb50ba0dd1058358bc0feb58304a3d80 (patch)
tree14473e0a8ed3499e559ff4e957a5ecdbf42dfa3a
parent1f3db960405e08e0f707a5de5810c3c12040709d (diff)
downloadwww-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
-rw-r--r--en/about/license/license.php2
-rw-r--r--langs.inc.php91
-rw-r--r--langs/php-mo.php (renamed from en/about/license/php-mo.php)0
-rw-r--r--tools/README11
-rw-r--r--tools/extract2gettext.php126
-rw-r--r--tools/lookup_for_second_argument.php61
-rwxr-xr-xtools/lookup_for_second_argument.sh18
-rw-r--r--tools/web_projects.dat24
8 files changed, 331 insertions, 2 deletions
diff --git a/en/about/license/license.php b/en/about/license/license.php
index a28c8adc2..5745e0826 100644
--- a/en/about/license/license.php
+++ b/en/about/license/license.php
@@ -17,8 +17,6 @@ function read_license_from_vcs($locale) {
array('Warning: Free Software may not necessarily be patent free, and some Free\nSoftware included may be covered by patents in your country. For example, the\nMP3 decoders included may require a license for further usage (see\nhttp://www.mp3licensing.com for more details). If you are unsure if a patent\nmay be applicable to you, check your local laws.'),
);
- require_once('php-mo.php');
-
if($locale == 'en') {
$po_locale = 'libDrakX.pot';
} else {
diff --git a/langs.inc.php b/langs.inc.php
index 59f89b910..a30e44a24 100644
--- a/langs.inc.php
+++ b/langs.inc.php
@@ -49,6 +49,8 @@ $domains_lang = array(
'mageia.ro' => 'ro',
);
+require_once('langs/php-mo.php');
+
/**
* Redirect to a localized path, depending on incoming TLD.
* Only manages redirections to main home path.
@@ -434,3 +436,92 @@ function _e($s = null, $args = null) { return i18n::_e($s, $args); }
function _h($s = null, $args = null, $tag = 'p') { return i18n::_h($s, $args, $tag); }
function _lang_load($locale, $domain) { return i18n::_lang_load($locale, $domain); }
+
+/**
+ * Create dictionary from gettext file
+ * Return array.
+ * Do not exit the process.
+ *
+ * @param string $locale from which we want to create dictionary
+ * @param string $name_of_translation gettext filename
+ *
+ * @return array
+*/
+function read_translation_file($locale, $name_of_translation)
+{
+ return phpmo_parse_po_file(G_APP_ROOT . '/langs/' . $locale . '/' . $name_of_translation .'.po');
+}
+
+/**
+ * Returns a translated string from global $dictionary
+ * it can append space if needed
+ *
+ * Note that it trims {ok} for translations equal to original too.
+ *
+ * Use it when you need to capture the string to output.
+ *
+ * Examples:
+ * echo _r("Hello!", ' ') . _r("How are you?")
+ * which should return translated: Hello! How are you?
+ *
+ * @param string $string_for_translation which we want to translate
+ * @param string $sufix append (usually space)
+ *
+ * @return string translated to current locale
+*/
+function _r($string_for_translation, $sufix = '')
+{
+ global $dictionary;
+ $escapeded_string = str_replace(array('"'), array('\\"'), $string_for_translation);
+ if(!empty($dictionary[$escapeded_string]["msgstr"][0])) {
+ $find = array('\\"', '\n', ' ', '{ok}', '{OK}', '{Ok}', '{oK}');
+ $replace = array('"','<br>', ' ');
+ $prepared_string = trim(str_replace($find, $replace, $dictionary[$escapeded_string]["msgstr"][0]));
+ }
+ if(empty($prepared_string)) {
+ $prepared_string = $string_for_translation;
+ }
+ if(!empty($sufix)) {
+ $prepared_string .= $sufix;
+ }
+ return $prepared_string;
+}
+
+/**
+ * Higher level function for _r() to echo a translated string from global $dictionary
+ * used also to wrap the translation with HTML tags
+ * it can also append space if needed
+ *
+ * Examples:
+ *_g("How are you?")
+ * will just echo translation
+ *
+ * _g('Download Mageia %d!', array(5), 'a href="" style="color: blue;"')
+ * will echo blue link
+ *
+ * _g("Hey there.", null, ' '); _g("How are you?")
+ * will just echo translation: Hey there. How are you?
+ *
+ * Return boolean.
+ * Do not exit the process.
+ *
+ * @param string $string_for_translation which we want to translate
+ * @param array $args for vsprintf
+ * @param string $tag_or_space HTML tag or space to append
+ *
+ * @return null
+*/
+function _g($string_for_translation, $args = null, $tag_or_space = '')
+{
+ $translated_string = _r($string_for_translation);
+ if(is_array($args)) {
+ $translated_string = vsprintf($translated_string, $args);
+ }
+ if(!empty($tag_or_space) && $tag_or_space != ' ') {
+ $tag_or_space_w_args = explode(' ', $tag_or_space);
+ $close_tag = array_shift($tag_or_space_w_args);
+ echo sprintf('<%s>%s</%s>', $tag_or_space, $translated_string, $close_tag);
+ } else {
+ echo $translated_string . $tag_or_space;
+ }
+}
diff --git a/en/about/license/php-mo.php b/langs/php-mo.php
index 470ce80e8..470ce80e8 100644
--- a/en/about/license/php-mo.php
+++ b/langs/php-mo.php
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"