Internationalization
Internationalization
If you are building a site for an international audience, you will likely want to provide localized versions of common strings on your website, including menu items, form labels, button labels, and more. Additionally, some websites require that route path segments be localized.
Zend Framework provides internationalization (i18n) tools via the zend-i18ncomponent, and integration with zend-mvc via the zend-mvc-i18n component.
Installation
Install zend-mvc-i18n via Composer:
$ composer require zendframework/zend-mvc-i18n
Assuming you are using zend-component-installer (which is installed by default with the skeleton application), this will prompt you to install the component as a module in your application; make sure you select eitherapplication.config.php
or modules.config.php
for the location.
Once installed, this component exposes several services, including:
MvcTranslator
, which implements the zend-i18nTranslatorInterface
, as well as the version specific to zend-validator, providing an instance that can be used for all application contexts.- A "translator aware" router.
By default, until you configure translations, installation has no practical effect. So the next step is creating translations to use in your application.
Creating translations
The zend-i18n Translation chapter covers the details of adding translations to your application. You can use PHP arrays, INI files, or the popular gettext package (which allows you to use industry standard tools such as poedit to edit translations).
Once you have some translation sources, you will need to put them somewhere your application can access them. Options include:
- In a subdirectory of the module that defines and/or consumes the translation strings. As an example,
module/Application/language/
. - In your application data directory; e.g.,
data/language/
.
Make sure you follow the guidelines from the zend-i18n documentation with regards to naming your files. Additionally, you may want to further segregate any such directory by text domain.
From here, you need to configure the translator to use your files. This requires adding configuration in either your module or application configuration files that provides:
- The default locale if none is provided.
- Translation file patterns, which include:
- the translation source type (e.g.,
gettext
,phparray
,ini
) - the base directory in which they are stored
- a file pattern for identifying the files to use
As examples:
// in a module's module.config.php:
'translator' => [
'locale' => 'en_US',
'translation_file_patterns' => [
[
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
],
],
],
// or in config/autoload/global.php:
'translator' => [
'locale' => 'en_US',
'translation_file_patterns' => [
[
'type' => 'gettext',
'base_dir' => getcwd() . '/data/language',
'pattern' => '%s.mo',
],
],
],
Once the above configuration is in place, the translator will be active in your application, allowing you to use it.
Translating strings in templates
Once you have defined some strings to translate, and configured the application to use them, you can translate them in your application. Thetranslate()
and translatePlural()
view helpers allow you to provide translations within your view scripts.
As an example, you might want to translate the string "All rights reserved" in your footer. You could do the following in your layout script:
<p>© 2016 by Examples Ltd. <?= $this->translate('All rights reserved') ?></p>
Translating route segments
In order to enable route translation, you need to do two things:
- Tell the router to use the translation-aware route class.
- Optionally, tell it which text domain to use (if not using the default text domain).
To tell the application to use the translation-aware route class, we can update our routing configuration. Underneath the top-level router
key, we'll add therouter_class
key:
// In a module.config.php file, or config/autoload/global.php:
'router' => [
'router_class' => Zend\Mvc\I18n\Router\TranslatorAwareTreeRouteStack::class,
'routes' => [
/* ... */
],
],
If you want to use an alternate text domain, you can do so via thetranslator_text_domain
key, also directly below the router
key:
// In a module.config.php file, or config/autoload/global.php:
'router' => [
'router_class' => Zend\Mvc\I18n\Router\TranslatorAwareTreeRouteStack::class,
'translator_text_domain' => 'router',
'routes' => [
/* ... */
],
],
Now that the router is aware of translations, we can use translatable strings in our routes. To do so, surround the string capable of translation with braces ({}
). As an example:
'route' => '/{login}',
specifies the word "login" as translatable.
Internationalization的更多相关文章
- 国际化(Internationalization)
1:什么是国际化? 国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式.它要求从产品中抽离所有的与语言,国家/地区和文化相关的元素.换言之,应用程序的功 ...
- Struts2 internationalization(国际化)
1:什么是国际化? 国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式.它要求从产品中抽离所有的与语言,国家/地区和文化相关的元素.换言之,应用程序的功 ...
- Struts2(十三)国际化-internationalization
一.国际化是什么--I18N 即internationalization 首字母i-结束字母n之间有18个字母 特征:在程序不做修改的情况下,可以根据不同的语言环境显示相应内容 二.Java内置国际化 ...
- Struts2学习笔记 国际化(Internationalization)
概述 国际化(Internationalization),通途的讲,就是让软件实现对多种语言的支持.可以通过简单的设置就可以从一种语言切换到另一种语言.用的最多的地方就是在应用程序的界面表示上.我们经 ...
- 使用 PySide2 开发 Maya 插件系列三:qt语言国际化(internationalization)
使用 PySide2 开发 Maya 插件系列三:qt语言国际化(internationalization) 前言: 这是 qt for python 的语言国际化,基于 UI 的,python 也有 ...
- Internationalization(i18n) support in SAP CRM,UI5 and Hybris
i18n(其来源是英文单词 internationalization的首末字符i和n,18为中间的字符数)是"国际化"的简称.对程序来说,在不修改内部代码的情况下,能根据不同语言及 ...
- current status of the installation and the internationalization of Samba 3.0
Only about 8 months from release of Samba 3.0.0, there is beginning to be the transition from 2.2.x. ...
- i18n 和 L10n (internationalization and localization) 国际化与本地化(具有全球战略眼光的公司企业的必由之路)
i18n 和 L10n (internationalization and localization) 国际化与本地化(具有全球战略眼光的公司企业的必由之路) 1 1 https://zh.wiki ...
- Internationalization API & ECMA-402
Internationalization API & ECMA-402 i18n https://caniuse.com/?search=Internationalization API In ...
随机推荐
- android.content.res.Resources$NotFoundException: String resource ID #0x1
之前忘了记录这个错误,今天又遇到了.唉,人不能纵容自己犯懒,遂记录之. 错误:android.content.res.Resources$NotFoundException: String resou ...
- ODAC访问oracle时,提示:由于以前的函数求值超时,函数求值被禁用,必须继续执行才能正常返回
这是因为调试时会自动对Local/Watch等窗口里面(或鼠标停留所在)的变量求值,为了防止用户写的程序错误(比如死循环),系统有一个超时限制,如果某个属性的get中做了很复杂的操作(而不是简单地返回 ...
- MessagePack介绍
在项目中,服务端的人需要我研究messagepcak 进行数据的传输,对messagePack的了解就是传输的数据格式都是二进制,可以节省用户的流量,就因为这点 数据格式小,服务端决定采用msgpac ...
- HDU 2121 Ice_cream’s world II 最小树形图
这个题就是需要求整个有向带权图的最小树形图,没有指定根,那就需要加一个虚根 这个虚根到每个点的权值是总权值+1,然后就可以求了,如果求出来的权值大于等于二倍的总权值,就无解 有解的情况,还需要输出最根 ...
- eclipse配置tomcat加大内存的方法
双击tomcat -Dcatalina.base="E:\work\whykt\.metadata\.plugins\org.eclipse.wst.server.core\tmp0&quo ...
- 《Genesis-3D开源游戏引擎完整实例教程-跑酷游戏篇04:如何实现触控操作》
4.如何实现触控操作 触控操作概述: 随着APPLE.Samsung.SONY等众多公司,将掌机.电脑和手机等产品在触控领域的不断探索,以触控为操作的机型越来越多的被投放到市场当中.触控游戏.触控软件 ...
- 拉格朗日对偶(Lagrange duality)
拉格朗日对偶(Lagrange duality) 存在等式约束的极值问题求法,比如下面的最优化问题: 目标函数是f(w),下面是等式约束.通常解法是引入拉格朗日算子,这里使用 ...
- Eclipse关联JavaDoc和源代码
1.Eclipse 关联 JavaDoc 1.在 Eclipse 菜单中点击 Window -> Preferences,在弹出框中左侧选择展开 Java 节点,点击 Installed JRE ...
- android sqlite 中存储 long 数据
在資料庫的技術中,一個資料庫(Database)表示應用程式儲存與管理資料的單位,應用程式可能需要儲存很多不同的資料,例如一個購物網站的資 料庫,就需要儲存與管理會員.商品和訂單資料.每一種在資料庫中 ...
- 爬取知乎百万信息之UrlTask
这个模块的作用是从nexturl队列获取用户的关注列表的url,获取关注列表.服务器返回的Json的数据 封装一个对象的序列化和反序列化的类 public class SerializeHelper ...