当前位置: 移动技术网 > IT编程>开发语言>PHP > YII Framework框架教程之国际化实现方法

YII Framework框架教程之国际化实现方法

2017年12月12日  | 移动技术网IT编程  | 我要评论

本文讲述了yii framework框架教程之国际化实现方法。分享给大家供大家参考,具体如下:

一个web应用,发布到互联网,就是面向全球用户。用户在世界的各个角落都可以访问到你的web应用,当然要看你的网站和不和谐,不和谐的web应用在和谐社会是不让你访问的。

yii提供了国际化的支持,可以让我们创建的应用适合不同语言的人群。

国际化是一个很花哨的东西,没有哪个大型的网站真正能做到国际化。大多都是针对不懂的语言,不同地区设计不同的网站。如果你的应用相对较小,处理的东西不多,那么国际化起来的东西还是蛮可以的。

国际化从以下几个方面入手:

区域设置

信息文本和文件资源的翻译

日期/时间、货币符号和数字格式

yii中国际化涉及到的类在/yii_dev/yii/framework/i18n目录下面:

/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

区域设置

通过对区域的设置,来判断用户所在的国际和使用的语言。

yii定义了常见的区域标识,可以认为是表示区域的唯一id。

yii中通过clocale类存放区域数据(包括货币,日期,数字格式等等)。

通过一个区域唯一id,然后就可以通过 clocale::getinstance($localeid) 或者capplication::getlocale($localeid) 获取相应的 clocale 实例。通过clocale实例,就能够判断用户所在的国家,使用的语言。然后可以根据clocale的数据进行相应的翻译,让web应用更适于当前用户使用和阅读。最根本的就是为了用户进行特定的翻译。

信息文本和文件资源的翻译

翻译很简单就是把一种语言变成另一种语言。在计算机中用的是26字母,就是e文。所以可以把e文当成是原始语言,万语之源,所有其他的语言都是通过e文翻译而成的,暂且e文叫做源语言。翻译成的语言叫做目标语言。

具体的类说明

/**
* 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 'coremessages' for messages belonging to
* the 'yii' category and using 'messages' 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源语言
$mesage目标语言
$params是$mesage中要匹配翻译的数组。

具体使用方法如:

yii::t('app', 'path alias "{alias}" is redefined.',
  array('{alias}'=>$alias))

当然可以通过yiic提供的命令行命令message进行翻译,具体的参考yiic命令的使用说明

日期/时间、金钱和数字格式

日期/时间处理cdateformatter类
具体参考(/yii_dev/yii/framework/i18n/cdateformatter.php)类文件

数字处理
具体参考(/yii_dev/yii/framework/i18n/cnumberformatter.php)类文件

更多关于yii相关内容感兴趣的读者可查看本站专题:《yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家基于yii框架的php程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网