I am trying to set up a way to allow members to translate strings into other languages. You can see an example here: TRANSLATIONS TEST
Someone recommended that I use php's native gettext() function for this, instead of what I am already using to load the language files, which is this:
function loadLanguageFile($language, $file) {
$temp = array();
$data = file_get_contents('./'.$language.'/'.$file.'.'.$language.'.php');
$codes = array (
'/(\'\s*\.\s*\$)(.+?)(\s*\.\s*\')/',
'/(=\s*\$)(.+?)(\s*\.\s*\')/',
'/(\'\s*\.\s*\$)(.+?)(;)/',
'/(\[\')(.+?)(\'\])/',
'/<\?php/s', '/\?>/s', '/<\?/s'
);
$html = array (
'{$2}',
'= \'{$2}',
'{$2}\';',
'[$2]',
'',
);
// Since we don't have the values for the vars.
$data = preg_replace($codes, $html, $data);
// We must change this because they are global.
$data = str_replace('$txt', '$langEditor_txt', $data);
$data = str_replace('$helptxt', '$langEditor_helptxt', $data);
eval($data);
if (isset($langEditor_txt)) {
$temp['txt'] = $langEditor_txt;
unset($GLOBALS['langEditor_txt']);
}
if (isset($langEditor_helptxt)) {
$temp['helptxt'] = $langEditor_helptxt;
unset($GLOBALS['langEditor_helptxt']);
}
return $temp;
}
The strings are contained within a file that is named like so: ManageDPModules.english.php DreamPortal.english.php etc.
These files can look like the following, when opened in any php editor, and can have many of these $txt variables:
<?php
// Dream Portal (c) 2009-2010 Dream Portal Team
// DreamPortal.english.php; @1.1
global $scripturl, $context;
// General Strings
$txt['forum'] = 'Forum';
$txt['dream_portal'] = 'Dream Portal';
$txt['dp_core_modules'] = 'Collapse or Expand this Module';
$txt['dp_who_forum'] = 'Viewing the forum index of <a href="' . $scripturl . '?action=forum">' . $context['forum_name'] . '</a>.';
$txt['dp_who_portal'] = 'Viewing the portal index of <a href="' . $scripturl . '">' . $context['forum_name'] . '</a>.';
$txt['dp_who_page'] = 'Viewing the page "<a href="' . $scripturl . '?page=%1$s">%2$s</a>".';
?>
I am using the following function to save the translations:
function langSave($lang, $file) {
// We just don't get values from the form, they have to exist in the english files to be taken seriously.
$default = loadLanguageFile('english', $file);
if ($default['txt']) {
foreach ($default['txt'] as $key=>$string) {
if (isset($_REQUEST['txt'.$key]) && str_replace(' ', '', $_REQUEST['txt'.$key]) != '') {
$data.='$txt[\''.$key.'\'] = \''.str_replace("'", "\'", $_REQUEST['txt'.$key]).'\';'."
";
}
}
}
if ($default['helptxt']) {
foreach ($default['helptxt'] as $key=>$string) {
if (isset($_REQUEST['helptxt'.$key]) && str_replace(' ', '', $_REQUEST['helptxt'.$key]) != '') {
$data.='$helptxt[\''.$key.'\'] = \''.str_replace("'", "\'", $_REQUEST['helptxt'.$key]).'\';'."
";
}
}
}
if (isset($data)) {
$codes = array (// '' . $test . '
'/(\{)(.+?)(\})/',
'/(\'\' \. \$)(.+?)( \. \')/',
'/(\' \. \$)(.+?)( \. \'\')/',
'/(\[\')(.+?)(\'\])/',
'/(\[)(.+?)(\])/',
);
$html = array (
'\' . \$$2 . \'',
'\$$2 . \'',
'\' . \$$2',
'[$2]',
'[\'$2\']',
);
// Convert the data back to normal.
$data = preg_replace($codes, $html, $data);
$data = '<?php'."
".$data.'?>';
file_put_contents('./'.$lang.'/'.$file.'.'.$lang.'.php', $data);
}
languageHome();
}
Language function:
function languageHome() {
$languages = loadLanguageList();
echo '
Language List
<table>';
foreach ($languages as $language) {
echo '
<tr>
<td>
'.$language.'
</td>
<td>
<a href="index.php?op=langView&lang='.$language.'">View</a>
</td>
</tr>';
}
echo '
</table>';
}
I fail to see how gettext will help out. There is no way to update the text catalog without rebooting the server every time. Maybe if someone can create a demo of this for me?
Also, would like it to support UTF-8. The data should be consistent.
So what is wrong with this implementation?? Why use gettext?? How can it be used to improve translations to work for both UTF-8 and non UTF-8 language strings so that it can be translated.?
EDIT:
Please note, the files will eventually need to be renamed to: ManageDPModules.[language].php
, DreamPortal.[language].php
, etc., etc. in order for the translations to work. So, how would catalogs help me in this regard? If you want to see possible END-RESULT Translations, you can download a language package located here and open up the .german.php language files to see what it should look like after the member submits a language on a file by file basis. Noted that some of these packages have UTF-8 strings, and some do not. The filename of the packages let you know this. Would be nice if I can also make it support UTF-8, but it is not a requirement. Please note, I'm not trying to create complete packages here. I just want to create the languagefile.[language].php with all of the translated strings inside of them (which my code already does).
OK, I will provide the ENTIRE index.php file for this so you can see what it does exactly when you do translations. Here is the index.php file for this and you'll need some english language files: DreamPortal.english.php, ManageDPModules.english.php, and DreamHelp.english-utf8.php. Now, in order to see this, you need to upload to a server, index.php, create a few folders where index.php is, call 1 english, and create a folder in there for each additional language you want (I did 2 folders, spanish and french), than upload the 3 language files into the english folder. Run index.php in your browser and you will see it working.
Now, how could I use gettext for catalogs with this SAME approach. I need to enable online translation of files. I need to create PHP files of the translations in the SAME style that the .english.php files are with the same PREFIX that is before .english.php
, and I need to change the language within the filename to the same language defined for the folder name. Online translations is the ONLY method available. Translator's need to focus ONLY on translating the strings. They shouldn't be focusing on installing programs, packaging it up, renaming the files, etc. etc.. This makes this process as painless as possible allowing it to be done online. And I know there is a way to do this, and even for UTF-8 support. But I'm using the best method that I know how at the moment. But so many of you are MUCH smarter at this sort of thing than I am, so I ask for help from you guys.
Is there anyone that can show me a better way? An example like the one that I have given you in this question?
I need to allow translations to be done ONLINE from translators, and would also like it to support UTF-8 files, as well as non UTF-8 files. I like the way I have it setup for the link provided above (TRANSLATIONS TEST) so that translator's can just do the translations online and not have to worry about anything else, and it would automatically create the files needed. Which would be the same as the english filename of the language with the folder name (representing the language) after the first . (dot) and it needs to have the extension .php (like it does in the code I am currently using). So basically, I need an adaption of the current index.php to support UTF-8 and non UTF-8 for all or most languages, and was told that using gettext() and catalog files would help with this.
Looking for a modified version of my current index.php to use gettext() in a way that it will support most, if not all, languages and translations. The REGEX I got going on for preg_replace isn't completely satisfactory because it seems to place a forward slash in front of double quotes when saving/submitting the translations. So perhaps an improvement on the preg_replace would be needed also.
I provided a complete example with ACTUAL CODE bytheway. I'd like for someone to alter this example, with the CODE that I provided to USE GETTEXT instead and support UTF-8. Or actually provide a ACTUAL METHOD for me to do this myself with. Not looking for a bunch of links that I can find on my own!
Thank You!