Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2: How to mass extract Yii::t() messages for localization?

I have lots of Yii::t() commands in my application. Now I need to extract them somehow but I don't want to do this manually. How can I extract all those commands easily? Do I have to do it with regular expressions?

I have read the documentation but it doesn't state anything about this.

like image 494
robsch Avatar asked Feb 22 '15 12:02

robsch


1 Answers

Yii2 provides already somthing to help you with this topic. You can use the console with two yii commands:

  • message/config: With the first command you create a configuration file
  • message/extract: and with the second you extract the Yii:t() messages based on the configuration file

By default (part of the configuration file) all messages will be saved into a @app/messages directory that you have to have created before. These files will contain the required php arrays with empty string values for the translations. The files will be named by category that are used in the Yii::t('category', 'message') commands and are placed into sub directories for the specified language codes.

All is documented in the yii command. Type ./yii help message into your console. The config file itself also includes documentation.

Hint: don't forget to exclude the vendor directory in the configuration, otherwise the Yii framework messages gets also extracted.

Btw: these commands don't need the i18n configuration that is typically stored in the web.php or console.php files. Though, for using the localized messages you have to define it of course.

Some information is now available here in the guide.

like image 63
robsch Avatar answered Dec 25 '22 08:12

robsch