定制 gettext/languages 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gettext/languages

Composer 安装命令:

composer require gettext/languages

包简介

gettext languages with plural rules

README 文档

README

Tests

gettext language list automatically generated from CLDR data

Static usage

To use the languages data generated from this tool you can use the bin/export-plural-rules command.

Export command line options

export-plural-rules supports the following options:

  • --us-ascii If specified, the output will contain only US-ASCII characters. If not specified, the output charset is UTF-8.
  • --languages=<LanguageId>[,<LanguageId>,...]] --language=<LanguageId>[,<LanguageId>,...]] Export only the specified language codes. Separate languages with commas; you can also use this argument more than once; it's case insensitive and accepts both '_' and '-' as locale chunks separator (eg we accept it_IT as well as it-it). If this option is not specified, the result will contain all the available languages.
  • --reduce=yes|no If set to yes the output won't contain languages with the same base language and rules. For instance nl_BE (Flemish) will be omitted because it's the same as nl (Dutch). Defaults to no if --languages is specified, to yes otherwise.
  • --parenthesis=yes|no If set to no, extra parenthesis will be omitted in generated plural rules formulas. Those extra parenthesis are needed to create a PHP-compatible formula. Defaults to yes
  • --output=<file name> If specified, the output will be saved to <file name>. If not specified we'll output to standard output.

Export formats

export-plural-rules can generate data in the following formats:

  • json: compressed JSON data

    export-plural-rules json
  • prettyjson: uncompressed JSON data

    export-plural-rules prettyjson
  • html: html table (see the result)

    export-plural-rules html
  • php: build a php file that can be included

    export-plural-rules --output=yourfile.php php

    Then you can use that generated file in your php scripts:

    $languages = include 'yourfile.php';
  • ruby: build a ruby file that can be included

    export-plural-rules --parenthesis=no --output=yourfile.rb ruby

    Then you can use that generated file in your ruby scripts:

    require './yourfile.rb'
    PLURAL_RULES['en']
  • xml: generate an XML document (here you can find the xsd XML schema)

    export-plural-rules xml
  • po: generate the gettext .po headers for a single language

    export-plural-rules po --language=YourLanguageCode

Dynamic usage

With Composer

You can use Composer to include this tool in your project. Simply launch composer require gettext/languages or add "gettext/languages": "*" to the "require" section of your composer.json file.

Without Composer

If you don't use composer in your project, you can download this package in a directory of your project and include the autoloader file:

require_once 'path/to/src/autoloader.php';

Main methods

The most useful functions of this tools are the following

$allLanguages = Gettext\Languages\Language::getAll();
...
$oneLanguage = Gettext\Languages\Language::getById('en_US');
...

getAll returns a list of Gettext\Languages\Language instances, getById returns a single Gettext\Languages\Language instance (or null if the specified language identifier is not valid).

The main properties of the Gettext\Languages\Language instances are:

  • id: the normalized language ID (for instance en_US)
  • name: the language name (for instance American English for en_US)
  • supersededBy: the code of a language that supersedes this language code (for instance, jw is superseded by jv to represent the Javanese language)
  • script: the script name (for instance, for zh_Hans - Simplified Chinese - the script is Simplified Han)
  • territory: the name of the territory (for instance United States for en_US)
  • baseLanguage: the name of the base language (for instance English for en_US)
  • formula: the gettext formula to distinguish between different plural rules. For instance n != 1
  • categories: the plural cases applicable for this language. It's an array of Gettext\Languages\Category instances. Each instance has these properties:
    • id: can be (in this order) one of zero, one, two, few, many or other. The other case is always present.
    • examples: a representation of some values for which this plural case is valid (examples are simple numbers like 1 or complex ranges like 0, 2~16, 100, 1000, 10000, 100000, 1000000, …)

Is this data correct?

Yes - as far as you trust the Unicode CLDR project.

The conversion from CLDR to gettext includes also a lot of tests to check the results. And all passes 😉.

Reference

CLDR

The CLDR specifications define the following variables to be used in the CLDR plural formulas:

  • n: absolute value of the source number (integer and decimals) (eg: 9.870 => 9.87)
  • i: integer digits of n (eg: 9.870 => 9)
  • v: number of visible fraction digits in n, with trailing zeros (eg: 9.870 => 3)
  • w: number of visible fraction digits in n, without trailing zeros (eg: 9.870 => 2)
  • f: visible fractional digits in n, with trailing zeros (eg: 9.870 => 870)
  • t: visible fractional digits in n, without trailing zeros (eg: 9.870 => 87)
  • c: exponent of the power of 10 used in compact decimal formatting (eg: 98c7 => 7)
  • e: synonym for c

gettext

The gettext specifications define the following variables to be used in the gettext plural formulas:

  • n: unsigned long int

Conversion CLDR > gettext

CLDR variable gettext equivalent
n n
i n
v 0
w 0
f empty
t empty
c empty
e empty

Parenthesis in ternary operators

The generated gettext formulas contain some extra parenthesis, in order to avoid problems in some programming language. For instance, let's assume we have this formula: (0 == 0) ? 0 : (0 == 1) ? 1 : 2

So, in order to avoid problems, instead of a simple a ? 0 : b ? 1 : 2 the resulting formulas will be in this format: a ? 0 : (b ? 1 : 2)

Contributing

Generating the CLDR data

This repository uses the CLDR data, including American English (en_US) json files. In order to generate this data, you can use the bin/import-cldr-data CLI command.

Support this project

You can offer me a monthy coffee or a one-time coffee 😉

gettext/languages 适用场景与选型建议

gettext/languages 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 32.55M 次下载、GitHub Stars 达 75, 最近一次更新时间为 2015 年 02 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「i18n」 「translations」 「language」 「l10n」 「localization」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 gettext/languages 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 gettext/languages 我们能提供哪些服务?
定制开发 / 二次开发

基于 gettext/languages 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 32.55M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 78
  • 点击次数: 42
  • 依赖项目数: 10
  • 推荐数: 0

GitHub 信息

  • Stars: 75
  • Watchers: 4
  • Forks: 10
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-02-13