How can I use gettext to help me here?

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 &quot;<a href="' . $scripturl . '?page=%1$s">%2$s</a>&quot;.'; ?> 

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!

+7
php internationalization gettext
source share
2 answers

When it was recommended that you use gettext, it was actually recommended that you use the gettext- like translation system. Your current code is complicated due to mnemonic text hints. And the problem you are having with regular expressions for editing is caused by confusion of variables. Let me suggest an alternative code for transitional purposes.

The beauty of gettext is the use of an unobtrusive API. The _() function call is simple enough to be fully usable without adding syntax or bloating code. He prefers things like getTextTrans('ABBR_TXT_ID') . The use of such mnemonic text identifiers is a common mistake; because in practice the words are not often repeated, and _("Raw english original text.") performs the same task. However, since you already have the mnemonic keys, keep them if it is too much to change. This is just a recommendation.

Your real problem is using PHP built-in expressions to create translation strings. That is why regular expressions for your translation editor have become opaque. Therefore, I highly recommend using static strings and providing placeholders. The translation function must be entrusted with its processing. (Donโ€™t worry about microoptimization here!) - I would use {$url_xy} PHP / Smarty style placeholders, for example:

 $txt['dp_who_forum'] = 'Viewing the forum index of <a href="{$scripturl}?action=forum">{$forum_name}</a>.'; 

And a translation function that looks for a global placeholder table or parameters ($ context) to replace:

 function __($text, $params=array()) { global $txt, $txt_placeholders; if (isset($txt[$text])) { $text = $txt[$text]; } if (strpos($text, '{$')) { $params = array_merge($params, $txt_placeholders); $text= preg_replace("/\{\$(\w+)\}/e", "$params['$1']", $text); } return $text; } 

optimizable. But in this way you can use a static mnemonic-> text or English-> text set of translation arrays. You use only static lines in a text editor. These static lines are displayed as-is, and your translators are editing English text, but not any of the {$placeholders} .

Therefore, your code for the translation function will not need any complex regular expressions (in this case they are not useful) for matching strings and PHP built-in variables. In fact, a much simpler combination of include() and var_export() can now take its place:

 function langSave($lang, $file) { $txt = array(); include($file); // assuming it contains a simple $txt = array(... foreach ($txt as $key=>$string) { $txt[$key] = $_REQUEST["txt$key"]; } file_put_contents($file, "<?php\n\$txt =".var_export($txt,1).';?>'); } 

File processing and more needs to be customized. But still this is a simpler approach.

And it will also allow you to switch to one of the gettext options as backends. Save your own __() wrapper function and use for example. Zend_Translate as a backend. This allows you to use the .php $ txt = array () translation files (or, I think so), or go to the .mo / .po gettext-style files. The advantage of this is that there is rich support for the instrument as opposed to homegrown solutions.

Anyway, this is how you use the material in your main application:

 print "<a href='...'>" . __("link_text") . "</a>"; print __("dp_forum_link"); // uses eg $txt_placeholder["scripturl"] print __("dp_param_link", array("scripturl"=>"http://override...")); 

At first, it is recommended to switch from short_txt keys to English-English> translational text translation arrays, but theoretically any of the backends from the dvbs answer is applicable. Now you already said that the native gettext is not an option for you (for PHP, not related to fastcgi, memory resistance is a drawback). But the PHP features for INTL's built-in functions can help you. In particular, http://www.php.net/manual/en/class.messageformatter.php may be more useful than the simple {$ var} replacement shell I gave you. But I never used it, and I think Zend_Translate is most likely more useful. In particular, any of these backends gives you character set independence. Personally, I would just stick with UTF-8, no matter what. But for gettext.mo/.po files, everyone can have their own encoding, for example.

+6
source share

Why reinvent the wheel? Perhaps I do not understand, but I am sure that what you are trying to do has been done before.

Just use one of the many existing solutions.

  • Visit each of them and filter out irrelevant projects
  • Find out the differences between the others and choose the one that suits you.
  • Continue to develop and customize your own needs based on your existing project.
  • If you find a mistake or if you think that the source project will be useful for your add-ons, contact the author and let him know.

Here's what to think about when making decisions:

  • The language in which the project is used
  • Ability to expand, add features and customize to suit your needs.
  • Is this a service? or open source, which you can download and use on your own server.
  • Does it support plural translation?
  • Community and resources available for this project
  • Does it also come with the web interface you requested?

Here are some (randomly ordered):

Good luck

Update: thanks, El Yobo. I marked this as a community wiki, anyone can edit or respond to other projects if they find something else.

+13
source share

All Articles