Yii2 is a Chinese and English website

Source: Internet
Author: User
Tags i18n
What should I do if my website is to be made in both Chinese and English, and the data is also made in both Chinese and English? The following are some of my ideas: When a module is created, the website uses Chinese characters by default, and the English language is placed in the module of English, the data in English is called when it is English? One set of Chinese template and one set of English template? {Code... the website should be made in Chinese and English, and data should also be made in Chinese and English. How can this problem be solved?

Below are some of my ideas:

Create a module. The website is in Chinese by default, and the module in English is put. When it is in English, the data in English is called?
One set of Chinese template and one set of English template?

 'modules' => [        'en' => ['class' => '\app\modules\english\Module'],  ]

How can we store data? Add a field to each data entry,lang = 'en' / lang = 'zh'So?

Add more fields to the data, as shown below,titleSave Chinese characters,title_enSave English?

Create table 'Course' ('id' int (11) unsigned not null auto_increment, 'title' varchar (255) not null default '', 'title _ en' varchar (255) not null default '', 'cover' varchar (255) not null default ''comment' cover ', 'text' text, 'text _ en' text, 'Start _ date' int (11) unsigned not null default 0 comment 'start date', 'end _ date' int (11) unsigned not null default 0 comment 'end date ', 'Days 'int (11) unsigned not null default 0 comment' valid days ', 'maxnum' int (11) unsigned not null default 0 comment' maximum registrants ', 'Has _ num' int (11) unsigned not null default 0 comment 'number of registration', 'price' decimal () comment 'course Bill ', 'created _ at' int (11) unsigned default '0', 'updated _ at' int (11) unsigned default '0', 'status' varchar (16) default 'on 'comment' on off expire expiring ', 'extra 'text comment' additional data can be serialized using serialize, primary key ('id ')) ENGINE = InnoDB default charset = utf8;

Find a better solution, suchOfficial Apple websiteHow does one store data in multiple languages?

Reply content:

What should I do if my website is to be made in both Chinese and English, and the data is also made in both Chinese and English?

Below are some of my ideas:

Create a module. The website is in Chinese by default, and the module in English is put. When it is in English, the data in English is called?
One set of Chinese template and one set of English template?

 'modules' => [        'en' => ['class' => '\app\modules\english\Module'],  ]

How can we store data? Add a field to each data entry,lang = 'en' / lang = 'zh'So?

Add more fields to the data, as shown below,titleSave Chinese characters,title_enSave English?

Create table 'Course' ('id' int (11) unsigned not null auto_increment, 'title' varchar (255) not null default '', 'title _ en' varchar (255) not null default '', 'cover' varchar (255) not null default ''comment' cover ', 'text' text, 'text _ en' text, 'Start _ date' int (11) unsigned not null default 0 comment 'start date', 'end _ date' int (11) unsigned not null default 0 comment 'end date ', 'Days 'int (11) unsigned not null default 0 comment' valid days ', 'maxnum' int (11) unsigned not null default 0 comment' maximum registrants ', 'Has _ num' int (11) unsigned not null default 0 comment 'number of registration', 'price' decimal () comment 'course Bill ', 'created _ at' int (11) unsigned default '0', 'updated _ at' int (11) unsigned default '0', 'status' varchar (16) default 'on 'comment' on off expire expiring ', 'extra 'text comment' additional data can be serialized using serialize, primary key ('id ')) ENGINE = InnoDB default charset = utf8;

Find a better solution, suchOfficial Apple websiteHow does one store data in multiple languages?

1. It is easier to translate English into other languages. You can configure sourceLanguage => 'en-us', lanaguae => 'zh-cn' in the Yii configuration file, and Yii :: $ app-> language is a readable and writable global variable.

Yii: the usage of the t () method is as follows,

Echo \ Yii: t ('app', 'This is a string to translate! ');
The first parameter indicates the type name of the message source, and the second parameter indicates the message to be translated.

The Yii: t () method calls the i18n application component for translation. This component can be configured in the application according to the following code,

'Components' => [

//... 'I18n' => ['translation' => ['app * '=> ['class' => 'yii \ i18n \ phpmessagesource ', // 'basepath' => '@ app/messages', // 'sourcelanguage' => 'en-us ', 'filemap' => ['app' => 'app. php', 'app/error' => 'error. php',], in the code above, a message source supported by yii \ i18n \ PhpMessageSource is configured. Mode app * indicates that all the message category names starting with the app use the translated message source. This yii \ i18n \ PhpMessageSource class uses PHP

File to store message translation. Each PHP file corresponds to a single type of message. By default, the file name should be the same as the category name. However, you can configure
Yii \ i18n \ PhpMessageSource: fileMap to map a class to a PHP file with different names. In the preceding example
App/error is mapped to the php file @ app/messages/ru-RU/error. php (assuming ru-RU
Target Language ). If this configuration is not available, this category will be mapped to @ app/messages/ru-RU/app/error. php.

In addition to storing message sources in the PHP file, you can also use the following message sources to store translated messages in different storage:

Yii \ i18n \ GettextMessageSource uses the GNU Gettext MO or PO file to save the translated message.
Yii \ i18n \ DbMessageSource uses a database table to store translated messages.

Add Yii: t ('app', 'This is a string to translate! ', "Zh-CN.
2. For the database content, we recommend that you store multiple tables in different languages. If each language is a table, the scalability is poor. You can add the language field to the table, and configure the association according to Yii: $ app-> language and foreign key.

For the database, you can create two tables with the same structure: article_en and article_zn.
Use the same model to determine the current language using url parameters or other methods. Use the following method in the model to switch the corresponding data table:

Public static function tableName () {if (// in Chinese) {$ table_name = 'Article _ zn ';} else {$ table_name = 'Article _ en ';} return $ table_name ;}

What if it supports languages in 10 countries? It is too wasteful to use databases.

You can refer to the tp framework for multiple languages. It puts different languages into files. The cookie or session is used to differentiate languages and load corresponding language files. However, the limitation is that it seems that only static pages can be created.

Can I document the document before I use the framework?

Yii supports multi-language mechanisms.

Click here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.