Home > php教程 > PHP开发 > body text

Internationalization implementation method of YII Framework framework tutorial

高洛峰
Release: 2016-12-27 13:57:41
Original
1365 people have browsed it

This article describes the internationalization implementation method of the YII Framework framework tutorial. Share it with everyone for your reference, the details are as follows:

A web application, published to the Internet, is for global users. Users from all corners of the world can access your web application. Of course, it depends on your website and disharmony. In a harmonious society, you will not be allowed to access disharmonious web applications.

YII provides international support, allowing the applications we create to be suitable for people in different languages.

Internationalization is a very fancy thing, and no large website can truly achieve internationalization. Most of them are designed for languages ​​​​that are not understood, and different websites are designed in different regions. If your application is relatively small and doesn't deal with many things, then internationalizing things is quite acceptable.

Internationalization starts with the following aspects:

Locale settings

Translation of information text and file resources

Date/time, currency symbols and numbers Format

The classes involved in internationalization in YII are under the /yii_dev/yii/framework/i18n directory:

/yii_dev/yii/framework/i18n# tree
.
├── CChoiceFormat.php
├── CDateFormatter.php
├── CDbMessageSource.php
├── CGettextMessageSource.php
├── CLocale.php
├── CMessageSource. php
├── CNumberFormatter.php
├── CPhpMessageSource.php
├── data
│ ├── en_us.php
│ ├── ....... .............
│ ├── zh_hk.php
│ ├── zh_mo.php
│ ├── zh.php
│ ├── zh_sg.php
│ ├── zh_tw.php
│ ├── zu.php
│ └── zu_za.php
└── gettext
├── CGettextFile.php
├── CGettextMoFile.php
└── CGettextPoFile.php

2 directories, 616 files

Regional settings

Judged by setting the region The user's country and language.

YII defines common area identifiers, which can be considered as unique IDs representing areas.

YII stores regional data (including currency, date, number format, etc.) through the CLocale class.

Pass a region's unique ID, and then you can obtain the corresponding CLocale instance through CLocale::getInstance($localeID) or CApplication::getLocale($localeID). Through the CLocale instance, you can determine the user's country and language. Then corresponding translation can be performed based on the CLocale data to make the web application more suitable for current users to use and read. The most fundamental thing is to perform specific translations for users.

Translation of information text and file resources

Translation is simply changing one language into another. In computers, 26 letters are used, which is e-text. Therefore, e-text can be regarded as the original language, the source of all languages. All other languages ​​are translated through e-text. For the time being, e-text is called the source language. The language into which it is translated is called the target language.

Specific class description

/**
* Translates a message to the specified language.
* Starting from version 1.0.2, this method supports choice format (see {@link CChoiceFormat}),
* i.e., the message returned will be chosen from a few candidates according to the given
* number value. This feature is mainly used to solve plural format issue in case
* a message has different plural forms in some languages.
* @param string $category message category. Please use only word letters. Note, category 'yii' is
* reserved for Yii framework core code use. See {@link CPhpMessageSource} for
* more interpretation about message category.
* @param string $message the original message
* @param array $params parameters to be applied to the message using <code>strtr</code>.
* Starting from version 1.0.2, the first parameter can be a number without key.
* And in this case, the method will call {@link CChoiceFormat::format} to choose
* an appropriate message translation.
* Starting from version 1.1.6 you can pass parameter for {@link CChoiceFormat::format}
* or plural forms format without wrapping it with array.
* @param string $source which message source application component to use.
* Defaults to null, meaning using &#39;coreMessages&#39; for messages belonging to
* the &#39;yii&#39; category and using &#39;messages&#39; for the rest messages.
* @param string $language the target language. If null (default), the {@link CApplication::getLanguage application language} will be used.
* This parameter has been available since version 1.0.3.
* @return string the translated message
* @see CMessageSource
*/
public static function t($category,$message,$params=array(),$source=null,$language=null)
{
Copy after login

$category source language
$mesage target language
$params is the array in $mesage to be matched for translation.

The specific usage is as follows:

Yii::t(&#39;app&#39;, &#39;Path alias "{alias}" is redefined.&#39;,
  array(&#39;{alias}&#39;=>$alias))
Copy after login

Of course, you can translate it through the command line command message provided by yiic. For details, please refer to the instructions for using the yiic command.

Date/time, money And number format

Date/time processing CDateFormatter class
Specific reference (/yii_dev/yii/framework/i18n/CDateFormatter.php) class file

Number processing
Specific reference ( /yii_dev/yii/framework/i18n/CNumberFormatter.php) class file

I hope this article will be helpful to everyone’s PHP program design based on the Yii framework.

For more articles related to the internationalization implementation method of the YII Framework framework tutorial, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!