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-i18n TranslatorInterface, 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., gettextphparrayini)
  • 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>&copy; 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.

时间: 2024-10-12 23:09:46

Internationalization的相关文章

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. Here again, I will explain the Notes on new features and migration of Samba 3.0 series. (Editorial office) The Samba Team as an Samba of developer, e

Struts2(十三)国际化-internationalization

一.国际化是什么--I18N 即internationalization 首字母i-结束字母n之间有18个字母 特征:在程序不做修改的情况下,可以根据不同的语言环境显示相应内容 二.Java内置国际化 message_en_US.properties demo = hello world! userName = user name password = password welcome = welcome! message_zh_CN.properties demo = \u60A8\u597D

使用 PySide2 开发 Maya 插件系列三:qt语言国际化(internationalization)

使用 PySide2 开发 Maya 插件系列三:qt语言国际化(internationalization) 前言: 这是 qt for python 的语言国际化,基于 UI 的,python 也有自身的语言国际化,两者是不同的. 先来看最终效果: 前期准备: 这次创建一个 main window 在 menu bar 加一个 language 的 menu: 我们还要对 action 进行一些设置,如下: 生成 .py 文件: 生成代码: 1 # -*- coding: utf-8 -*-

SAP Fiori internationalization(国际化)实现的一些例子

下图这个Opportunity的金额,在右边的明细页面里显示的是1880 USD,但为何在左边,显示的是2K这个近似值? 在Chrome开发者工具的调试器里,我们能看到从后台返回的金额确实是1880: 所以这个2k一定是前台UI5代码里进行的格式化: 答案就在上图sap.ca.ui.model.format.AmoutFormat.FormatAmoutShort这个方法里. 这里涉及到一个CLDR的概念: CLDR - Unicode Common Locale Data Repository

[Angular] Introduction to Angular Internationalization (i18n)

To add translation to the application: <button (click)="onEditCourse()" i18n>Edit button</button> 'i18n' is for Angular CLI to extract all the string which need to be translated. Run: ng xi18n It will generate messages.xif, this file

centos 7.2 Apache+mysql+php step by step备忘

1. 如何允许laravel程序执行sudo shell脚本? chmod u+w /etc/sudoers ; echo "apache ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers 再注释掉Defaults requiretty这行,否则会要求有tty才能运行!(TODO: 写shell实现自动化) chmod u-w /etc/sudoers2. OS: CentOS 7.2 x643. 安装zend server ce: (由于ze

AngularJS 国际化——Angular-translate

对于一个用户群面向全球的的应用来说,不得不考虑国际化的问题.当然,即便是刚刚起步的小应用,如果有心搞大,也应该提前设计国际化的方案. 本篇讲述使用AngularJS构建的应用的简单国际化方案,准确的说,是国际化服务.... i18n与l10n i18n是Internationalization得缩写,取第一个字母和最后一个字母,以及中间省略的字母数目,即i18n,类似的l10n是Localization得意思. 通常i18n是国际化的意思,就是在不改变源码的情况下,通过某些简单的配置就能适应不同

Spring MVC @RequestMapping Annotation Example with Controller, Methods, Headers, Params, @RequestPar

Spring MVC @RequestMapping Annotation Example with Controller, Methods, Headers, Params, @RequestParam, @PathVariable Pankaj July 4, 2014 Spring @RequestMapping is one of the most widely used Spring MVC annotation.org.springframework.web.bind.annotat

Spring 3 MVC: Themes In Spring-Tutorial With Example---reference

Welcome to Part 6 of Spring 3.0 MVC Series. In previous article we saw how to add Internationalization i18n and Localization L10n support to Spring 3.0 based web application. We usedLocaleChangeInterceptor to intercept the change in locale andReloada