Yii2 Chinese and English website

Source: Internet
Author: User
Keywords Php yii2
Tags i18n
Website to be made into a Chinese and English, the data also if the Chinese and English, how to do?

Here are some of my ideas:

To build a module, the site is the default Chinese, English is placed in the English module, English is the time to call the English data?
Chinese set of templates, English set of templates?

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

How do we save the data? Add a field to each piece of data, lang = 'en' / lang = 'zh' so?

Or a few more fields of data, such as the following, title save Chinese, in title_en English?

  CREATE TABLE ' courses ' (' ID ' int (one) unsigned not NULL auto_increment, ' Title ' varchar (255) NOT NULL default ', ' title_en ' varchar (255) is not null default ' ', ' cover ' varchar (255) is not NULL Default ' comment ' cover ', ' Text ' text, ' text_en ' text, ' start_date ' int (one) unsigned not NULL default 0 comment ' Open Start date ', ' end_date ' int (one) unsigned NOT null default 0 comment ' End Date ', ' Days ' int (one) unsigned NOT NULL default 0 comm Ent ' Effective days ', ' maxnum ' int (one) unsigned NOT null default 0 comment ' Maximum number of registrations ', ' has_num ' int (one) unsigned NOT null Defau Lt 0 Comment ' enrolled number ', ' Price ' decimal (10,2) Comment ' Course fee ', ' created_at ' int (one) unsigned default ' 0 ', ' updated_at ' Int (one) unsigned default ' 0 ', ' status ' varchar (+) Default ' on ' comment ' on off off expire expiry ', ' extra ' text commen T ' extra data can use serialized data serialize ', PRIMARY KEY (' id ')) engine=innodb DEFAULT Charset=utf8;  

What is the best way to solve the problem, like how to 苹果官网 do the multi-language, how to save the data?

Reply content:

Website to be made into a Chinese and English, the data also if the Chinese and English, how to do?

Here are some of my ideas:

To build a module, the site is the default Chinese, English is placed in the English module, English is the time to call the English data?
Chinese set of templates, English set of templates?

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

How do we save the data? Add a field to each piece of data, lang = 'en' / lang = 'zh' so?

Or a few more fields of data, such as the following, title save Chinese, in title_en English?

  CREATE TABLE ' courses ' (' ID ' int (one) unsigned not NULL auto_increment, ' Title ' varchar (255) NOT NULL default ', ' title_en ' varchar (255) is not null default ' ', ' cover ' varchar (255) is not NULL Default ' comment ' cover ', ' Text ' text, ' text_en ' text, ' start_date ' int (one) unsigned not NULL default 0 comment ' Open Start date ', ' end_date ' int (one) unsigned NOT null default 0 comment ' End Date ', ' Days ' int (one) unsigned NOT NULL default 0 comm Ent ' Effective days ', ' maxnum ' int (one) unsigned NOT null default 0 comment ' Maximum number of registrations ', ' has_num ' int (one) unsigned NOT null Defau Lt 0 Comment ' enrolled number ', ' Price ' decimal (10,2) Comment ' Course fee ', ' created_at ' int (one) unsigned default ' 0 ', ' updated_at ' Int (one) unsigned default ' 0 ', ' status ' varchar (+) Default ' on ' comment ' on off off expire expiry ', ' extra ' text commen T ' extra data can use serialized data serialize ', PRIMARY KEY (' id ')) engine=innodb DEFAULT Charset=utf8;  

What is the best way to solve the problem, like how to 苹果官网 do the multi-language, how to save the data?

1, English translation into other languages more convenient, yii configuration file can be configured sourcelanguage=> ' en-us ',lanaguae=> ' ZH-CN ', while Yii:: $app- Language is a readable and writable global variable.

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

Echo \yii::t (' app ', ' this was a string to translate! ');
The first parameter refers to the category name that stores the message source, and the second parameter refers to the message that needs to be translated.

This yii::t () method invokes the I18N application component to implement the translation effort. This component can be configured in the application according to the following code,

' Components ' = [

// ...'i18n' => [    'translations' => [        'app*' => [            'class' => 'yii\i18n\PhpMessageSource',            //'basePath' => '@app/messages',            //'sourceLanguage' => 'en-US',            'fileMap' => [                'app' => 'app.php',                'app/error' => 'error.php',            ],        ],    ],], ], 在上面的代码中,配置了由 yii\i18n\PhpMessageSource 所支持的消息来源。模式 app* 表示所有以 app 开头的消息类别名称都使用这个翻译的消息来源。该 yii\i18n\PhpMessageSource 类使用 PHP

File to store message translations. Each PHP file corresponds to a single category of messages. By default, the file name should be the same as the category name. However, you can configure
Yii\i18n\phpmessagesource::filemap to map a category to a different name PHP file. In the example above, the category
App/error is mapped to a PHP file @app/messages/ru-ru/error.php (assuming Ru-ru
To the target language). Without this configuration, the category will be mapped to the @app/messages/ru-ru/app/error.php.

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

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 was a string to translate! ', ' ZH-CN ') and this method can be passed to the target language parameter.
2, the contents of the database, it is recommended to consider more than one table to save the part of different languages. If a table in each language, the extensibility is poor, you can add the Language field in the table, according to Yii:: $app->language and foreign keys to configure the association is also very convenient.

Database considerations can create 2 tables with exactly the same structure: Article_en, ARTICLE_ZN
Using the same model, using URL parameters or other methods to determine the current language, use the following method in model to switch the corresponding data table:

public static function tableName(){    if (//如果是中文) {        $table_name = 'article_zn';    } else {        $table_name = 'article_en';    }    return $table_name;}

What if it supports the language of 10 countries? Using a database is too wasteful.

Can draw on the TP framework of multiple languages, it is to put different languages into the file. According to the cookie or session to distinguish the language, loading the corresponding language files. But the limitation is that it seems to be a static page only.

Do the current years of the year use the framework as a document before they are used?

Yii supports multi-lingual mechanisms.

Let's poke around here.

  • Related Article

    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.