Add script to generate gettext files from trad4all.inc.php files - refs #2323

pull/2744/head
Yannick Warnier 8 years ago
parent e9139b9a6c
commit bcb011c0fa
  1. 272
      tests/scripts/lang/generate_gettext_files.php

@ -4,89 +4,233 @@
* Generate Gettext-format language files from the existing language files
*
* @package chamilo.cron.lang
* @author Yannick Warnier <yannick.warnier@beeznest.com>
*/
/**
* Includes and declarations.
*/
die();
die(); //remove before execution
require_once __DIR__.'/../../../main/inc/global.inc.php';
$path = api_get_path(SYS_LANG_PATH).'english';
ini_set('memory_limit', '128M');
$partial = false;//if set to true, do not add empty strings to .po files
$destinationDir = '/tmp/gettext'; //where to put the generated files
/**
* Main code.
* Get list of languages
*/
$terms = [];
$list = SubLanguageManager::get_lang_folder_files_list($path);
foreach ($list as $entry) {
$file = $path.'/'.$entry;
if (is_file($file)) {
$terms = array_merge($terms, SubLanguageManager::get_all_language_variable_in_file($file, true));
$langPath = api_get_path(SYS_LANG_PATH);
$languagesListFull = scandir($langPath);
$languagesList = [];
foreach ($languagesListFull as $language) {
if (substr($language, 0, 1) === '.') {
continue;
} elseif ($language === 'index.html') {
continue;
} else {
$languagesList[] = $language;
}
}
/**
* Get English language terms (the main source of terms)
*/
$path = $langPath.'english';
$terms = [];
$file = $path.'/trad4all.inc.php';
if (is_file($file)) {
$terms = SubLanguageManager::get_all_language_variable_in_file($file, true);
}
foreach ($terms as $index => $translation) {
$terms[$index] = trim(rtrim($translation, ';'), '"');
}
// get only the array keys (the language variables defined in language files)
$defined_terms = array_flip(array_keys($terms));
echo count($defined_terms)." terms were found in language files".PHP_EOL;
//$definedTerms = array_flip(array_keys($terms));
echo count($terms)." terms were found in language files".PHP_EOL;
// now get all terms found in all PHP files of Chamilo (this takes some
// time and memory)
$usedTerms = [];
$l = strlen(api_get_path(SYS_PATH));
$files = getAllPhpFiles(api_get_path(SYS_PATH));
$rootLength = strlen(api_get_path(SYS_PATH));
$countFiles = 0;
$countReplaces = 0;
// Browse files
foreach ($files as $file) {
if (substr($file, $rootLength, 6) === 'vendor' || substr($file, $rootLength, 3) === 'web') {
continue;
// make sure we have an ISO 639-1 (or-2 if no -1) to give the gettext file
$langToIso639v1 = [
'arabic' => 'ar_AR',
'asturian' => 'ast_ES',
'basque' => 'eu_ES',
'bengali' => 'bn_BD',
'bosnian' => 'bs_BA',
'brazilian' => 'pt_BR',
'bulgarian' => 'bg_BG',
'catalan' => 'ca_ES',
'croatian' => 'hr_HR',
'czech' => 'cs_CZ',
'danish' => 'da_DK',
'dari' => 'prs_AF',
'dutch' => 'nl_NL',
'english' => 'en_US',
'esperanto' => 'eo',
'faroese' => 'fo_FO',
'finnish' => 'fi_FI',
'french' => 'fr_FR',
'friulian' => 'fur_IT',
'galician' => 'gl_ES',
'georgian' => 'ka_GE',
'german' => 'de_DE',
'greek' => 'el_GR',
'hebrew' => 'he_IL',
'hindi' => 'hi_IN',
'hungarian' => 'hu_HU',
'indonesian' => 'id_ID',
'italian' => 'it_IT',
'japanese' => 'ja_JP',
'korean' => 'ko_KR',
'latvian' => 'lv_LV',
'lithuanian' => 'lt_LT',
'macedonian' => 'mk_MK',
'malay' => 'ms_MY',
'norwegian' => 'nn_NO',
'occitan' => 'oc_FR',
'pashto' => 'ps_AF',
'persian' => 'fa_IR',
'polish' => 'pl_PL',
'portuguese' => 'pt_PT',
'quechua_cusco' => 'quz_PE',
'romanian' => 'ro_RO',
'russian' => 'ru_RU',
'serbian' => 'sr_RS',
'simpl_chinese' => 'zh_CN',
'slovak' => 'sk_SK',
'slovenian' => 'sl_SI',
'somali' => 'so_SO',
'spanish' => 'es_ES',
'spanish_latin' => 'es_MX',
'swahili' => 'sw_KE',
'swedish' => 'sv_SE',
'tagalog' => 'tl_PH',
'thai' => 'th_TH',
'tibetan' => 'bo_CN',
'trad_chinese' => 'zh_TW',
'turkish' => 'tr_TR',
'ukrainian' => 'uk_UA',
'vietnamese' => 'vi_VN',
'xhosa' => 'xh_ZA',
'yoruba' => 'yo_NG',
];
$langToPOFilename = [
'arabic' => 'ar',
'asturian' => 'ast_ES',
'basque' => 'eu_ES',
'bengali' => 'bn',
'bosnian' => 'bs',
'brazilian' => 'pt_BR',
'bulgarian' => 'bg',
'catalan' => 'ca_ES',
'croatian' => 'hr',
'czech' => 'cs',
'danish' => 'da',
'dari' => 'prs_AF',
'dutch' => 'nl',
'english' => 'en',
'esperanto' => 'eo',
'faroese' => 'fo_FO',
'finnish' => 'fi',
'french' => 'fr',
'friulian' => 'fur_IT',
'galician' => 'gl_ES',
'georgian' => 'ka_GE',
'german' => 'de',
'greek' => 'el',
'hebrew' => 'he',
'hindi' => 'hi_IN',
'hungarian' => 'hu',
'indonesian' => 'id',
'italian' => 'it',
'japanese' => 'ja',
'korean' => 'ko',
'latvian' => 'lv',
'lithuanian' => 'lt',
'macedonian' => 'mk',
'malay' => 'ms_MY',
'norwegian' => 'nn',
'occitan' => 'oc_FR',
'pashto' => 'ps_AF',
'persian' => 'fa_IR',
'polish' => 'pl',
'portuguese' => 'pt',
'quechua_cusco' => 'quz_PE',
'romanian' => 'ro',
'russian' => 'ru',
'serbian' => 'sr_RS',
'simpl_chinese' => 'zh_CN',
'slovak' => 'sk',
'slovenian' => 'sl',
'somali' => 'so',
'spanish' => 'es',
'spanish_latin' => 'es_MX',
'swahili' => 'sw_KE',
'swedish' => 'sv',
'tagalog' => 'tl_PH',
'thai' => 'th',
'tibetan' => 'bo_CN',
'trad_chinese' => 'zh_TW',
'turkish' => 'tr',
'ukrainian' => 'uk_UA',
'vietnamese' => 'vi',
'xhosa' => 'xh_ZA',
'yoruba' => 'yo_NG',
];
/**
* Generate .pot and .po files
* See https://webtranslateit.com/en/docs/file_formats/gettext_po/
*/
$baseFilename = 'main';
if (!is_dir($destinationDir)) {
mkdir($destinationDir);
}
if (!is_dir($destinationDir.'/'.$baseFilename)) {
mkdir($destinationDir.'/'.$baseFilename);
}
$destinationDir .= '/';
$destinationFilePot = $destinationDir.'/'.$baseFilename.'/'.$baseFilename.'.pot';
foreach ($languagesList as $language) {
$termsInLanguage = [];
$file = $langPath.$language.'/trad4all.inc.php';
$languageCode = $langToIso639v1[$language];
$languageFilename = $langToPOFilename[$language];
$destinationFile = $destinationDir.'/'.$baseFilename.'/'.$languageFilename.'.po';
$header = 'msgid ""'."\n".'msgstr ""'."\n".
'"Project-Id-Version: chamilo-lms\n"'."\n".
'"Language: '.$languageCode.'\n"'."\n".
'"Content-Type: text/plain; charset=UTF-8\n"'."\n".
'"Content-Transfer-Encoding: 8bit\n"'."\n\n";
file_put_contents($destinationFile, $header);
$termsInLanguage = SubLanguageManager::get_all_language_variable_in_file(
$file,
true
);
foreach ($termsInLanguage as $id => $content) {
$termsInLanguage[$id] = trim(rtrim($content, ';'), '"');
}
//echo 'Analyzing '.$file.PHP_EOL;
$shortFile = substr($file, $l);
//echo 'Analyzing '.$shortFile.PHP_EOL;
$lines = file($file);
// Browse lines inside file $file
foreach ($lines as $line) {
$myTerms = [];
$res = preg_match_all('/get_lang\(([\'"](\\w*)[\'"])\)/m', $line, $myTerms);
if ($res > 0) {
foreach ($myTerms[2] as $term) {
echo "Found term $term - ".print_r($myTerms, 1).PHP_EOL;
if (substr($term, 0, 4) == 'lang') {
$term = substr($term, 4);
}
if (!empty($terms[$term])) {
$translation = $terms[$term];
$quotedTerm = $myTerms[1][0];
//echo "Would do sed -i \"s#$quotedTerm#'$translation'#g\" $file here\n";
system("sed -i \"s#$term#'$translation'#g\" $file");
$countReplaces++;
}
}
$bigString = '';
$bigStringPot = '';
foreach ($terms as $term => $englishTranslation) {
$translatedTerm = '';
if (!empty($termsInLanguage[$term])) {
$translatedTerm = $termsInLanguage[$term];
} else {
$res = 0;
$res = preg_match_all('/\{\s*([\'"](\\w*)[\'"])\s*\|get_lang\}/m', $line, $myTerms);
if ($res > 0) {
foreach ($myTerms[2] as $term) {
echo "Found term $term".PHP_EOL;
if (substr($term, 0, 4) == 'lang') {
$term = substr($term, 4);
}
if (!empty($terms[$term])) {
$translation = $terms[$term];
$quotedTerm = $myTerms[1][0];
//echo "Would do sed -i \"s#$quotedTerm#'$translation'#g\" $file here\n";
system("sed -i \"s#$term#'$translation'#g\" $file");
$countReplaces++;
}
}
if ($partial) {
//do not include terms with empty translation
continue;
}
}
$bigString .= 'msgid "'.$englishTranslation.'"'."\n".'msgstr "'.$translatedTerm.'"'."\n\n";
if ($language === 'english') {
//$bigStringPot .= 'msgid "'.$term.'"'."\n".'msgstr ""'."\n\n";
}
}
file_put_contents($destinationFile, $bigString, FILE_APPEND);
if ($language === 'english') {
file_put_contents($destinationFilePot, $header.$bigString, FILE_APPEND);
}
$countFiles++;
flush();
}
echo "Done analyzing $countFiles files, with $countReplaces replacements!\n";
echo "Done generating gettext files in $destinationDir!\n";

Loading…
Cancel
Save