websafe/zf-mod-language

一个ZF2模块,负责检测您应用程序翻译器的最佳和/或请求的本地化。本地化是通过以下值计算/检测的:Accept-Language头部、cookie、session、查询参数和路由参数。每种检测方法都可以禁用/启用

v0.0.8 2013-12-09 23:26 UTC

This package is not auto-updated.

Last update: 2024-09-24 01:46:12 UTC


README

一个ZF2模块,负责检测 最佳 和/或 请求的 本地化,用于您的应用程序翻译器。本地化是基于以下值计算/检测的:Accept-Language头部、cookie、session、查询参数和路由参数。每种检测方法都可以通过配置禁用/启用。

提供

在您的应用程序中使用WebsafeZfModLanguage

在项目的根目录下运行

vendor/bin/composer.phar require "websafe/zf-mod-language:*"

config/application.config.php 中添加 WebsafeZfModLanguage

    // ...
    'modules' => array(
        // ...
        'Application',
        'WebsafeZfModLanguage',
        // ...
    ),

这就完了。模块现在应该可以透明地工作了 - 在官方 ZendSkeletonApplication 上进行测试,并尝试修改浏览器的Accept-Language头部,或在URL中添加 ?language=ja_JP(或任何在 supported_locales 中可用的其他本地化)。

您应该会注意到语言发生了变化,因为默认配置告诉语言服务在查询中检测本地化,请参阅选项 detect_in_queryquery_param

已经包含了一个基本的视图助手,因此,在 config/application.config.php 中启用模块后,您可以在 layout.phtml 或其他视图脚本中的某个地方尝试添加以下代码。

<?php echo $this->languageSelect();?>

视图助手是目前控制器存在的唯一原因。

配置

如何处理本地化/语言检测。

  1. 模块将 DetectLanguagesListener附加到事件管理器。

  2. DetectLanguagesListener现在正在等待一个分发事件(MvcEvent::EVENT_DISPATCH)...

  3. 当分发事件发生时,DetectLanguagesListener将事件转发到语言服务 WebsafeZfModLanguageService

1. The language service collects data for locale detection:

   + Retrieve languages requested by the client/browser via 
     [Accept-Language] headers. Add all results ordered by priority
     to the `detectedLanguages` array.

   + Retrieve locale stored in cookie (name of cookie is configurable).
     Prepend the retrieved locale to the front of `detectedLanguages`.

   + Retrieve locale stored in session (container name and session 
     variable name are configurable). 
     Prepend the retrieved locale to the front of `detectedLanguages`.

   + Retrieve locale provided in query parameter (parameter name  is
     configurable). 
     Prepend the retrieved locale to the front of `detectedLanguages`.

   + Retrieve locale provided in route parameter (parameter name is
     configurable). 
     Prepend the retrieved locale to the front of `detectedLanguages`.

1. The language service iterates through `detectedLanguages` and stops
   iterating after the first detected locale that exists in 
   [supported_locales]. The matched locale is now accessible via 
   `$sm->get('WebsafeZfModLanguageService')->getCurrentLocale()`.

1. The language service applies the current locale to the `translator`
   service.