Yii 1.1: translatephpmessage

Manage translations with CPhpMessageSource

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.


Tested with Yii 1.1.13 and PHP 5.4.


Edit your config file and add or change these lines:

    '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:

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, scroll to the bottom until you find a table labeled 'Insert new line for default language'.

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('.git', 'otherdir')
  • 'excludefiles' => array() files to exclude, use like this: array('.gitignore', 'otherfile.php')

About escaping

TranslatePhpMessage uses PHP var_export to save the file.
var_export does all the escaping in order to not break the array.
Usage of HTML character entity will work (&copy; &#169; copyright sign).
HTML tags (<div></div>) will not.


Change log

0.5 09/07/2013
- changed string escape method, now using PHP var_export
- fixed missing file creation
- should now present valid error message when no directories are found

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 9 comments

#18596 report it
le_top at 2014/11/25 04:39pm
Translations of extensions, themes

Hi It would be nice to include the translation of extensions and themes ;-).

#15500 report it
sl1p at 2013/11/15 02:56pm

Thank you, Kajo! You saved my hours of working.

#13974 report it
johniem at 2013/07/10 03:33am
Works like a charm

Last version works like a charm. Thanks a lot Kajo!

#13957 report it
Kajo at 2013/07/09 10:33am
translatephpmessage version 0.5

should solve the two comments below this one.

if not fell free to post here or pm me.

#13953 report it
johniem at 2013/07/09 07:15am
error running

I'm getting

ranslatePhpMessageModule and its behaviors do not have a method or closure named "countWhere".

on protected/modules/TranslatePhpMessage/views/translate/index.php(21)

16         echo '<pre> ' . $module->messagepath . '</pre>';
17         echo '</div>';
18         break;
19     case 1:
20         echo '<div id="modulewarning">';
21         if ($module->countWhere($module->languages, '==', $module->language) == 1) {
22             echo 'No other languages found, create some directories for the other languages in:';
23             echo '<pre>' . $module->messagepath . '</pre>';
24         } else {
25             echo 'Directory for defined language not found, create the directory "' . $module->language . '" in:';
26             echo '<pre>' . $module->messagepath . '</pre>';

I have the latest yii version 1.1.13

#13778 report it
pvk at 2013/06/25 07:55am
Problem with quote

If the message has a quote, every time that you save it a slashbar is added

Ex: '100100001'=>'O campo "%(field)s" não é válido.'

First Save result '100100001'=>'O campo \"%(field)s\" não é válido.',

Save again result '100100001'=>'O campo \"%(field)s\" não é válido.',

#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:
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


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!


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);
        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