translatephpmessage

Manage translations with CPhpMessageSource
15 followers

Manage translations with CPhpMessageSource

This module creates a user interface to translate your message files.
Your application must be setup to use messages CPhpMessageSource.
Check Resources for links with information about this.

Requirements

Tested with Yii 1.1.10.

Configuration

Edit your config file and add or change these lines:

.........
'modules'=>array(
    .........
    'TranslatePhpMessage' => array(
        'encoding' => 'UTF-8', //encoding used to save messages
        'excludedirs' => array(), //directories to exclude
        'excludefiles' => array(), //files to exclude
        ),
    .........
    ),
.........
// default Yii language, CHANGE to your language
'language' => 'en_us',

Every directory under /protected/messages/ should have 777 permissions (Unix only).

How to use

Extract the file to protected/modules.
Create a link to '/TranslatePhpMessage') or navigate to index.php?r=TranslatePhpMessage

This module assumes that the developer mantains the default language files, all operations are based of that files, so keep them in order.

The module displays an error if it cant find the directories where message files are stored.
Ideally you will have at least two directories (default language, other language).
If not you will get a warning, until you create them manually, there isn't any functionalty for that.

Choose the language you want to translate. You can choose the same language as the default to edit a source file.
The next screen shows two lists of files.
On the left the files you have. If you need a new file you must create it manually
On the right shows what files are missing (you can create a new blank file).

Again, this lists refer to the default language, so no missing files will show if you are editing the default language.
If you need a file not found on either list you must create it manualy in the default language directory.
Just make sure the file contains:

<?php
return array()
?>

The translate view compares between your default language and the language you choose.
To translate just write on the translation column and save.
You can also insert new key=>value pairs in the default language message file

Options Usage

  • 'encoding' => 'UTF-8'
    change to the encoding you want to use, defaults to UTF-8
  • 'excludedirs' => array()
    directories to exclude, list like this: array('.dir1', 'otherdir')
  • 'excludefiles' => array()
    files to exclude, use like this: array('firstfile.php', 'otherfile.php')

Resources

Change log

0.4 02/04/2012
- add option to exclude files / directories from the listing
- some settings can now be changed from config file
- solved bug under linux where filepath was wrong

0.3 02/03/2012
- Should work under Linux with no errors (Windows is ok).

0.2 29/02/2012
- no longer gives errors with PHP error reporting other than server production settings
- more helpful error messages
- UI tweaks

0.1 27/02/2012
- initial release

Total 3 comments

#11438 report it
godhimself at 2013/01/14 05:26am
pagination and sortable

.... would be nice features because the translation files could have a lot of entries. nice tool that works for me, thanks

#7600 report it
Kajo at 2012/04/02 11:30am
Thank you for reviewing

Hi Befi,

Thank you for the time spent reviewing this module.

Your suggestion for the hidden directories makes a lot of sense, I've implemented it slightly different but now you can choose wich file/directory to exclude from list. You can still exclude .svn but still include (for whatever reason) other hidden directories. It's also possible to exclude any directory you want.

Did the same for files which I actually needed.

You anticipated my reply with your edit, but if you don't want the new version (0.4), then instead of changing languagesNames() change findLanguages() instead with the same edit you used for findFiles().

As for the addslashes is not as simple, did you tried to enter:
e\'
it will save as e\\' which will broke the array.

#7595 report it
befi at 2012/04/01 11:58pm
hidden directories

I'm working with SVN so I immediately noticed that the ".svn" directory is included as well. It makes sense to exclude hidden directories. I did so by using

if ($entry != "." && $entry != ".." && !preg_match("/^\./", $entry)) {

instead of

if ($entry != "." && $entry != "..") {

in the findFiles function in TranslatePhpMessageModule.php and

using

if (!preg_match("/^\./", $value))

in front of

$languagesnames[$value] = Yii::App()->locale->getLocaleDisplayName($value);

in languagesNames() in the same file.

Hope this helps!

Otherwise great module!

[EDIT]

I did some more work with it and it's actually not a good idea to use "addslashes" since this function escapes quotes, double quotes and the backslash. It's to escape strings for database insertion if you don't have the DBMS specific escape function at hand. That's not what you want in this case. Since we're writing a PHP array with strings to a file and we're enclosing each string (key and value respectively) with quotes ('this is a string'), escaping double quotes and backslashes is unnecessary.

Therefore I would suggest an array2text function like this:

private function array2text($array) {
 
        $output = ''; 
        if (is_array($array)) {
            foreach ($array as $key => $value) {
                $key = str_replace("'", "\'", $key);
                $value = str_replace("'", "\'", $value);
                $output.="\t'{$key}'=>'{$value}',\n";
            }   
        }   
        return $output;
    }

With this, you can savely use ', " and \ in your strings without side effects.

Leave a comment

Please to leave your comment.

Create extension