Table of Contents
YII Framework框架教程之国际化实现方法,yiiframework
您可能感兴趣的文章:
Home php教程 php手册 YII Framework框架教程之国际化实现方法,yiiframework

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

Jun 13, 2016 am 08:44 AM
framework yii globalization

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

本文讲述了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)
{

Copy after login

$category源语言
$mesage目标语言
$params是$mesage中要匹配翻译的数组。

具体使用方法如:

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

Copy after login

当然可以通过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程序设计有所帮助。

您可能感兴趣的文章:

  • PHP YII框架开发小技巧之模型(models)中rules自定义验证规则
  • Nginx配置PHP的Yii与CakePHP框架的rewrite规则示例
  • 使用Composer安装Yii框架的方法
  • Yii使用migrate命令执行sql语句的方法
  • YII Framework框架使用YIIC快速创建YII应用之migrate用法实例详解
  • YII Framework框架教程之使用YIIC快速创建YII应用详解
  • YII Framework框架教程之缓存用法详解
  • YII Framework框架教程之安全方案详解
  • YII Framework框架教程之日志用法详解
  • YII Framework教程之异常处理详解
  • Yii rules常用规则示例
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Use the Gin framework to implement internationalization and multi-language support functions Use the Gin framework to implement internationalization and multi-language support functions Jun 23, 2023 am 11:07 AM

With the development of globalization and the popularity of the Internet, more and more websites and applications have begun to strive to achieve internationalization and multi-language support functions to meet the needs of different groups of people. In order to realize these functions, developers need to use some advanced technologies and frameworks. In this article, we will introduce how to use the Gin framework to implement internationalization and multi-language support capabilities. The Gin framework is a lightweight web framework written in Go language. It is efficient, easy to use and flexible, and has become the preferred framework for many developers. besides,

Build international web applications using the FastAPI framework Build international web applications using the FastAPI framework Sep 29, 2023 pm 03:53 PM

Use the FastAPI framework to build international Web applications. FastAPI is a high-performance Python Web framework that combines Python type annotations and high-performance asynchronous support to make developing Web applications simpler, faster, and more reliable. When building an international Web application, FastAPI provides convenient tools and concepts that can make the application easily support multiple languages. Below I will give a specific code example to introduce how to use the FastAPI framework to build

Building Multilingual Websites with PHP: Eliminating Language Barriers Building Multilingual Websites with PHP: Eliminating Language Barriers Feb 19, 2024 pm 07:10 PM

1. Prepare the database to create a new table for multilingual data, including the following fields: CREATETABLEtranslations(idINTNOTNULLAUTO_INCREMENT,localeVARCHAR(255)NOTNULL,keyVARCHAR(255)NOTNULL,valueTEXTNOTNULL,PRIMARYKEY(id)); 2. Set the language switching mechanism on the website Add a language switcher to the top or sidebar to allow users to select their preferred language. //Get the current language $current_locale=isset($_GET["locale"])?$_

How to use the Webman framework to achieve internationalization and multi-language support? How to use the Webman framework to achieve internationalization and multi-language support? Jul 09, 2023 pm 03:51 PM

Nowadays, with the continuous development of Internet technology, more and more websites and applications need to support multi-language and internationalization. In web development, using frameworks can greatly simplify the development process. This article will introduce how to use the Webman framework to achieve internationalization and multi-language support, and provide some code examples. 1. What is the Webman framework? Webman is a lightweight PHP-based framework that provides rich functionality and easy-to-use tools for developing web applications. One of them is internationalization and multi-

How to deal with multi-language and internationalization issues in PHP development How to deal with multi-language and internationalization issues in PHP development Oct 09, 2023 pm 04:24 PM

How to deal with multi-language and internationalization issues in PHP development requires specific code examples. With the development of the Internet, people's demand for multi-language and internationalization is getting higher and higher. In PHP development, how to effectively handle multi-language and internationalization issues has become an important task that developers need to solve. Handling of character encoding In PHP development, we must first ensure that character encoding is handled correctly. In multi-language environments, using UTF-8 encoding is the most common choice. You can add the following code to the head of the PHP file: header('C

How to use routing to implement international multi-language switching in Vue? How to use routing to implement international multi-language switching in Vue? Jul 22, 2023 pm 12:17 PM

How to use routing to implement international multi-language switching in Vue? When developing a multilingual website, one of our important needs is to be able to switch website content according to the language selected by the user. Vue.js is a popular JavaScript framework. By using the VueRouter plug-in, we can easily implement routing functions. In this article, I will introduce how to use routing to implement international multi-language switching in Vue. First, we need to install the VueRouter plugin. Can pass np

How to use PHP framework Yii to develop a highly available cloud backup system How to use PHP framework Yii to develop a highly available cloud backup system Jun 27, 2023 am 09:04 AM

With the continuous development of cloud computing technology, data backup has become something that every enterprise must do. In this context, it is particularly important to develop a highly available cloud backup system. The PHP framework Yii is a powerful framework that can help developers quickly build high-performance web applications. The following will introduce how to use the Yii framework to develop a highly available cloud backup system. Designing the database model In the Yii framework, the database model is a very important part. Because the data backup system requires a lot of tables and relationships

How to use the Hyperf framework for internationalization support How to use the Hyperf framework for internationalization support Oct 22, 2023 am 08:14 AM

How to use the Hyperf framework for international support With the rapid development of globalization, many applications need to have multi-language support functions to meet the needs of users in different countries and regions. As a lightweight, high-performance framework, the Hyperf framework provides international support functions and can help developers quickly develop multi-language applications. This article will introduce how to use internationalization functions in the Hyperf framework and provide corresponding code examples. 1. Configure multi-language support. First, you need to configure the Hyperf configuration file.

See all articles