Home  >  Article  >  Backend Development  >  YII Framework method to achieve internationalization

YII Framework method to achieve internationalization

不言
不言Original
2018-06-15 14:50:581580browse

This article mainly introduces the internationalization implementation method of the YII Framework framework tutorial. It analyzes the principles and related implementation techniques of the YII Framework framework internationalization in more detail. Friends in need can refer to the following

This article describes Learned the internationalization implementation method of 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

Local settings

By pairing Regional settings to determine 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)
{

$category source language
$mesage target language
$params is required in $mesage Array of matching translations.

The specific usage is as follows:

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

Of course, you can translate it through the command line command message provided by yiic. For details, please refer to the use of yiic command. Description

Date/time, money and number format

Date/time processing CDateFormatter class

The above is the entire content of this article, I hope It will be helpful for everyone’s learning. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Summary of introductory knowledge points of YiiFramework

About Yii Framework method to obtain all subclasses under the category

The above is the detailed content of YII Framework method to achieve internationalization. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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