Yii 1.1: jstrans

Yii translations in javascript


JsTrans is a Yii extension aiming to port the native Yii translations module to javascript. Language files can be published and used within javascript in almost the same way as Yii does server side. Currently supported are placeholders and plural forms (including expressions).


  • Download/clone JsTrans in extension folder (folder should be 'JsTrans')
  • Import the extension in main config: ('ext.JsTrans.*')


We need to publish the translations to javascript for them to work. You can publish all your translations in a single location, or publish only parts in specific places. (e.g. controller or view)

 new JsTrans('app','nl');

The first parameter is the category, the second the language. Both accept single items as string or multiple as array. There is an optional third parameter to specify the default language. If nothing is passed, it will use the App default language.

If everything went well, the translations are published as a javascript file containing all the translations in JSON format. A hash is included in the filename, to distinguish between different languages/categories. By default the file is cached, you may need to empty the assets folder in order to force a refresh.

At this point, translations can be used in javascript:

Yii.t('app','My non-translated text');

Make sure there is a translation available to see the results.


(for demo purposes the default language is English, it could be any language)

Simple translation:

Yii.t('app','Hello world');  // Hello World

Simple translation with custom language:

Yii.t('app','Hello world','','fr');  // Bonjour tout le monde
Yii.t('app','Hello world','','de');  // Hallo Welt


Yii.t('app','Hello {name}',{name:'Michael'}); // Hello Michael

Multiple placeholders:

Yii.t('app','Hello {firstname} {lastname}', {firstname:'Michael', lastname:'Jackson'}); // Hello Michael Jackson

Plural forms:

Yii.t('app','Apple|Apples',0); // Apples
Yii.t('app','Apple|Apples',1); // Apple
Yii.t('app','Apple|Apples',2); // Apples

Plural forms with placeholders:

Yii.t('app','{n} Apple|{n} Apples',0); // 0 Apples
Yii.t('app','{n} Apple|{n} Apples',1); // 1 Apple
Yii.t('app','{n} Apple|{n} Apples',2); // 2 Apples

Plural forms with expressions:

Yii.t('app','0#No comments, be the first!|1#One comment|n>1#{n} comments',0); // No comments, be the first!
Yii.t('app','0#No comments, be the first!|1#One comment|n>1#{n} comments',1); // One comment
Yii.t('app','0#No comments, be the first!|1#One comment|n>1#{n} comments',2); // 2 comments

Plural forms with expressions and placeholders:

Yii.t('app','0#{name} has no mail|1#{name} has one mail|n>1#{name} has {n} mails',{n:0, name:'Pete'}); // Pete has no mail
Yii.t('app','0#{name} has no mail|1#{name} has one mail|n>1#{name} has {n} mails',{n:1, name:'Pete'}); // Pete has one mail
Yii.t('app','0#{name} has no mail|1#{name} has one mail|n>1#{name} has {n} mails',{n:2, name:'Pete'}); // Pete has 2 mails

(optional) Generate translations using Yiic

Yii can be configured to automatically scan javascript files for translations, for more info see http://www.yiiframework.com/doc/api/1.1/CPhpMessageSource

An example configuration could look like this:

return array(
    'sourcePath' => dirname(__FILE__) . '/../..',
    'messagePath' => dirname(__FILE__) . '/../messages',
    'translator' => 'Yii.t',
    'languages' => array('nl','de'),
    'fileTypes' => array('js'),
    'overwrite' => true,
    'exclude' => array(

Best practise is to keep your javascript translations in separate categories, otherwise they might override each other. This is a known limitation of the framework.


  • extension page: http://www.yiiframework.com/extension/jstrans/
  • github project: https://github.com/Arne-S/JsTrans

Total 7 comments

#17112 report it
Fire at 2014/05/05 05:14am
dont forget to make the extensions/JTrans/assets folder chmod 777

In order for this extension to work dont forget to chmod 777 the extensions/JTrans/assets folder

#11792 report it
wisp at 2013/02/04 07:39am

thanks hakd, I've added your suggestion

#11318 report it
hakd at 2013/01/05 12:09pm

Excellent extension, but it seems that it will be better to use application level messages instead of coreMessages from framework distributive, or make it configurable.

$messagesFolder = rtrim(Yii::app()->messages->basePath, '\/');
#10097 report it
wisp at 2012/10/04 08:35am

The configuration is optional, it's only required when you want to automatically generate message files using YIIC. To make this work, you should put the config in a file called 'config.php' in the 'messages' folder.

In the command line you could type something like

 framework/yiic message protected/messages/config.php

..and then it will scan your whole project and generate translations.

You can find more info about translations here: http://www.yiiframework.com/doc/guide/1.1/it/topics.i18n

#10095 report it
David T at 2012/10/04 05:26am

Where can I put your configuration ?

return array(
    'sourcePath' => dirname(__FILE__) . '/../..',
    'messagePath' => dirname(__FILE__) '/../messages',
    'translator' => 'Yii.t',
    'languages' => array('nl','de'),
    'fileTypes' => array('js'),
    'overwrite' => true,
    'exclude' => array(
#10094 report it
wisp at 2012/10/04 05:17am

Hi David, what's the problem? Do you get an error message? Setup should be pretty straight forward.

#10093 report it
David T at 2012/10/04 05:09am
How to use this extension?

Please help me to use the configure for this extension. I can't get it to work.

Leave a comment

Please to leave your comment.

Create extension