• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

YII Framework框架实现国际化的方法

php 搞代码 4年前 (2022-01-22) 26次浏览 已收录 0个评论

这篇文章主要介绍了YII Framework框架教程之国际化实现方法,较为详细的分析了YII Framework框架国际化的原理与相关实现技巧,需要的朋友可以参考下

本文讲述了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 mess<strong>*本文来@源gao@daima#com搞(%代@#码@网2</strong><pre>搞代gaodaima码

age 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){


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:YII Framework框架实现国际化的方法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址