Yii Framework 开发教程多国语言示例
总体来说,要实现信息翻译,需要执行如下几步:
在合适的位置调用 Yii::t() ;
以 protected/messages/LocaleID/CategoryName.php 的格式创建 PHP 翻译文件。 每个文件简单的返回一个信息翻译数组。 注意,这是假设你使用默认的 CPhpMessageSource 存储翻译信息。
配置 CApplication::sourceLanguage 和 CApplication::language。
因此第一步为Application添加合适的配置:
[php]
// This is the main Web application configuration. Any writable
// CWebApplication properties can be configured here.
return array(
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
'sourceLanguage'=>'en',
'language'=>'zh_cn',
'name'=>'i18nDemo',
...
// application components
'components'=>array(
'coreMessages'=>array(
'basePath'=>'protected/messages',
),),
);
// This is the main Web application configuration. Any writable
// CWebApplication properties can be configured here.
return array(
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
'sourceLanguage'=>'en',
'language'=>'zh_cn',
'name'=>'i18nDemo',
...
// application components
'components'=>array(
'coreMessages'=>array(
'basePath'=>'protected/messages',
),),
);
源语言为en,目录语言为中文(zh_cn) ,翻译信息的目录为protected/messages.
然后再protected/messages 创建zh_cn 目录,如果还需要支持其它语言,可以创建相应的目录,然后在zh_cn创建一个yii.php ,其中yii 做为翻译时的分类名(你可以选择你自己喜欢的名字)。
检查代码中需要翻译的地方,然后在yii.php 文件中定义对应的翻译:
[php]
return array (
'Application Name' => '应用程序名称',
'Greetings from Santa'=>'来自圣诞老人的问候',
'firstName'=>'名',
'lastName'=>'姓',
'Choose your Christmas Gift'=>'选择你喜欢的圣诞礼物',
'iPad'=>'iPad',
'Remote control helicopter'=>'遥控直升飞机',
'60 inch 3D LED TV'=>'60寸3D LED电视',
'Holy Bible'=>'圣经',
'Choose your Christmas dinner'=>'选择你圣诞节晚餐',
'Egg'=>'鸡蛋',
'Ham'=>'火腿',
'Chicken'=>'鸡',
'Pork'=>'猪肉',
'Beer'=>'啤酒',
'Coke'=>'可乐',
'Wine'=>'白酒',
'Submit'=>'提交',
'Merry Christmas'=>'圣诞快乐',
'On Christmas Day,You will be given'=>'圣诞节那天你将获得',
'And you will have'=>'你可以有',
'for Christmas dinner'=>'作为圣诞晚餐',
'Start Again'=>'重新选择'
);
1
然后将原先使用英文字符串的地方换成yii::t('yii',xxx);比如DataModel.php
1
public function getGifts()
{
return array(
'1'=>Yii::t('yii','iPad'),
'2'=>Yii::t('yii','Remote control helicopter'),
'3'=>Yii::t('yii','60 inch 3D LED TV'),
'4'=>Yii::t('yii','Holy Bible'),
);
}
public function getMeals()
{
return array(
'1'=>Yii::t('yii','Egg'),
'2'=>Yii::t('yii','Ham'),
'3'=>Yii::t('yii','Chicken'),
'4'=>Yii::t('yii','Pork'),
'5'=>Yii::t('yii','Beer'),
'6'=>Yii::t('yii','Coke'),
'7'=>Yii::t('yii','Wine'),
);
}
return array (
'Application Name' => '应用程序名称',
'Greetings from Santa'=>'来自圣诞老人的问候',
'firstName'=>'名',
'lastName'=>'姓',
'Choose your Christmas Gift'=>'选择你喜欢的圣诞礼物',
'iPad'=>'iPad',
'Remote control helicopter'=>'遥控直升飞机',
'60 inch 3D LED TV'=>'60寸3D LED电视',
'Holy Bible'=>'圣经',
'Choose your Christmas dinner'=>'选择你圣诞节晚餐',
'Egg'=>'鸡蛋',
'Ham'=>'火腿',
'Chicken'=>'鸡',
'Pork'=>'猪肉',
'Beer'=>'啤酒',
'Coke'=>'可乐',
'Wine'=>'白酒',
'Submit'=>'提交',
'Merry Christmas'=>'圣诞快乐',
'On Christmas Day,You will be given'=>'圣诞节那天你将获得',
'And you will have'=>'你可以有',
'for Christmas dinner'=>'作为圣诞晚餐',
'Start Again'=>'重新选择'
);
1
然后将原先使用英文字符串的地方换成yii::t('yii',xxx);比如DataModel.php
1
public function getGifts()
{
return array(
'1'=>Yii::t('yii','iPad'),
'2'=>Yii::t('yii','Remote control helicopter'),
'3'=>Yii::t('yii','60 inch 3D LED TV'),
'4'=>Yii::t('yii','Holy Bible'),
);
}
public function getMeals()
{
return array(
'1'=>Yii::t('yii','Egg'),
'2'=>Yii::t('yii','Ham'),
'3'=>Yii::t('yii','Chicken'),
'4'=>Yii::t('yii','Pork'),
'5'=>Yii::t('yii','Beer'),
'6'=>Yii::t('yii','Coke'),
'7'=>Yii::t('yii','Wine'),
);
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



According to news on July 22, today, the official Weibo of Xiaomi ThePaper OS announced that Xiaoai Translation has been upgraded. Real-time subtitles have been added to Japanese and Korean translations, and subtitle-free videos and live conferences can be transcribed and translated in real time. Face-to-face simultaneous interpretation supports translation into 12 languages, including Chinese, English, Japanese, Korean, Russian, Portuguese, Spanish, Italian, French, German, Indonesian, and Hindi. The above functions currently only support the following three new phones: Xiaomi MIX Fold 4 Xiaomi MIX Flip Redmi K70 Extreme Edition It is reported that in 2021, Xiao Ai’s AI subtitles will be added to Japanese and Korean translations. AI subtitles use Xiaomi’s self-developed simultaneous interpretation technology to provide a faster, more stable and accurate subtitle reading experience. 1. According to the official statement, Xiaoai Translator can not only be used in audio and video venues

This AI-assisted programming tool has unearthed a large number of useful AI-assisted programming tools in this stage of rapid AI development. AI-assisted programming tools can improve development efficiency, improve code quality, and reduce bug rates. They are important assistants in the modern software development process. Today Dayao will share with you 4 AI-assisted programming tools (and all support C# language). I hope it will be helpful to everyone. https://github.com/YSGStudyHards/DotNetGuide1.GitHubCopilotGitHubCopilot is an AI coding assistant that helps you write code faster and with less effort, so you can focus more on problem solving and collaboration. Git

After rain in summer, you can often see a beautiful and magical special weather scene - rainbow. This is also a rare scene that can be encountered in photography, and it is very photogenic. There are several conditions for a rainbow to appear: first, there are enough water droplets in the air, and second, the sun shines at a low angle. Therefore, it is easiest to see a rainbow in the afternoon after the rain has cleared up. However, the formation of a rainbow is greatly affected by weather, light and other conditions, so it generally only lasts for a short period of time, and the best viewing and shooting time is even shorter. So when you encounter a rainbow, how can you properly record it and photograph it with quality? 1. Look for rainbows. In addition to the conditions mentioned above, rainbows usually appear in the direction of sunlight, that is, if the sun shines from west to east, rainbows are more likely to appear in the east.

1. First open WeChat. 2. Click [+] in the upper right corner. 3. Click the QR code to collect payment. 4. Click the three small dots in the upper right corner. 5. Click to close the voice reminder for payment arrival.

On March 3, 2022, less than a month after the birth of the world's first AI programmer Devin, the NLP team of Princeton University developed an open source AI programmer SWE-agent. It leverages the GPT-4 model to automatically resolve issues in GitHub repositories. SWE-agent's performance on the SWE-bench test set is similar to Devin, taking an average of 93 seconds and solving 12.29% of the problems. By interacting with a dedicated terminal, SWE-agent can open and search file contents, use automatic syntax checking, edit specific lines, and write and execute tests. (Note: The above content is a slight adjustment of the original content, but the key information in the original text is retained and does not exceed the specified word limit.) SWE-A

Go language development mobile application tutorial As the mobile application market continues to boom, more and more developers are beginning to explore how to use Go language to develop mobile applications. As a simple and efficient programming language, Go language has also shown strong potential in mobile application development. This article will introduce in detail how to use Go language to develop mobile applications, and attach specific code examples to help readers get started quickly and start developing their own mobile applications. 1. Preparation Before starting, we need to prepare the development environment and tools. head

PHP Tutorial: How to Convert Int Type to String In PHP, converting integer data to string is a common operation. This tutorial will introduce how to use PHP's built-in functions to convert the int type to a string, while providing specific code examples. Use cast: In PHP, you can use cast to convert integer data into a string. This method is very simple. You only need to add (string) before the integer data to convert it into a string. Below is a simple sample code

Based on the continuous optimization of large models, LLM agents - these powerful algorithmic entities have shown the potential to solve complex multi-step reasoning tasks. From natural language processing to deep learning, LLM agents are gradually becoming the focus of research and industry. They can not only understand and generate human language, but also formulate strategies, perform tasks in diverse environments, and even use API calls and coding to Build solutions. In this context, the introduction of the AgentQuest framework is a milestone. It not only provides a modular benchmarking platform for the evaluation and advancement of LLM agents, but also provides researchers with a Powerful tools to track and improve the performance of these agents at a more granular level
