I am trying to create a way to allow members to translate strings into other languages. Here you can see an example: TESTING TRANSLATIONS
Someone recommended me to use the php native gettext () function instead of what I already use to load language files, here's what:
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; }
Strings are contained in a file called: ManageDPModules.english.php DreamPortal.english.php and others.
These files may look like this: when opened in any php editor, and may 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 use the following function to save 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]).'\';'."\n"; } } } 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]).'\';'."\n"; } } } 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'."\n".$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 do not see how gettext will help. It is not possible to update the text directory without restarting the server each time. Maybe if someone can create a demo for me?
In addition, I would like him to support UTF-8. Data must be consistent.
So what is wrong with this implementation? Why use gettext? How can it be used to improve translation for working with both UTF-8 language strings and UTF-8 so that it can be translated.?
EDIT: Note that the files must ultimately be renamed to: ManageDPModules.[language].php , DreamPortal.[language].php , etc. Etc. That translations work. So how do directories help me in this regard? If you want to see possible END-RESULT Translations, you can download the language pack located here and open the .german.php language files to see how it should look after the participant sends the language to the file by file. It is noted that some of these packages have UTF-8 lines, and some do not. The package file name tells you this. It would be nice if I could also support it with UTF-8, but this is not a requirement. Please note: I am not going to create complete packages here. I just want to create a language file. [Language] .php with all translated strings inside them (which my code already does).
OK, I provided the ENTIRE index.php file for this, so you can see what exactly it does when you translate. Here is the index.php file, and you will need some English files: DreamPortal.english.php , ManageDPModules.english.php and DreamHelp.english-utf8.php . Now, to see this, you need to upload to the server, index.php, create several folders where index.php is specified, call 1 English and create a folder there for each additional language (I made 2 folders, Spanish and French) than upload 3 language files in the English folder. Run index.php in your browser and you will see that it works.
Now, how could I use gettext for directories with this SAME. I need to enable online file translation. I need to create SAME-style PHP translation files that have .english.php files with the same PREFIX as before .english.php , and I need to change the language in the file name to the same language defined for the folder name. Online translations are the only way. The translator needs to focus ONLY when translating lines. They should not focus on installing programs, packing them, renaming files, etc. Etc. This makes this process as painless as possible, allowing you to do it online. And I know that there is a way to do this, and even to support UTF-8. But I use the best method that I know how at the moment. But so many of you are much smarter about this than me, so I ask you guys for help.
Is there anyone who can show me the best way? An example similar to the one I gave you in this question?
I need translations to be done ONLINE by translators, and also want it to support UTF-8 files as well as files without UTF-8. I like the way I do it ( TRANSLATIONS TEST ) so that the translator can simply perform translations online and not worry about anything else, and this will automatically create the necessary files. Which will match the English language file name with the folder name (representing the language) after the first . (period), and it should have the extension .php (as in the code that I am currently using). So basically I need an adaptation of the current index.php to support UTF-8 and not UTF-8 for all or most languages, and I was told that using gettext () and directory files would help.
Looking for a modified version of my current index.php to use gettext () so that it supports most, if not all, languages โโand translations. The REGEX I got for preg_replace is not completely satisfactory because it seems to put a front slash in front of double quotes when saving / sending translations. Therefore, you may also need an improvement on preg_replace.
I presented a complete example with ACTUAL CODE bytheway. I would like someone to modify this example, with the CODE that I provided instead of USE GETTEXT, and support UTF-8. Or actually provide the ACTUAL METHOD so that I do it myself. Do not look for a bunch of links that I can find on my own!
Thanks!