一个轻量级的本地化数据库和翻译工具,包含来自CLDR、IANA、ISO等的数据。

v2.4.1 2022-12-23 00:40 UTC

This package is auto-updated.

Last update: 2024-09-23 04:39:28 UTC


README

此包表示从AWESOME fisharebest/localization 克隆的精简参考数据。主要区别

  • 移除了 Translator
  • PHP版本 >= 8.1 以便使用严格类型、枚举等
  • 显然,改变了命名空间和包名

本地化标准和数据

此包结合了来自许多标准的本地化数据,包括Unicode CLDRRFC5646 / IANA子标签注册表ISO-3166ISO-639ISO-15924等,以帮助您制作出能够良好服务于全球访客的应用程序。

包含超过800个区域的定义。

区域、语言、脚本和领土

区域由三个要素组成:一种语言、一种脚本和一个领土。脚本和领土通常从语言中隐含得出。

通常您只需处理区域,可以忽略语言和脚本。

$locale = new LocaleJa;         // Create a locale for Japanese.
$locale = Locale::create('ja'); // Create a locale for Japanese, from its code.

$locale->code();                // "ja_JP" (territories are always included in locale codes)
$locale->languageTag();         // "ja" (redundant territories are omitted in tags)
$locale->endonym();             // "日本語" (Japanese name for Japanese)

// Languages - extract from the locale, or create with "new LanguageXx"
$locale->language();            // LanguageJa
$locale->language()->code();    // "ja" (ISO-639 code)

// Scripts - extract from the locale, or create with "new ScriptXxxx"
$locale->script();              // ScriptJpan
$locale->script()->code();      // "Jpan" (ISO-15924 code)
$locale->script()->direction(); // "ltr" (left to right)

// Territories - extract from the locale, or create with "new TerritoryXx"
$locale->territory();           // TerritoryJp
$locale->territory()->code();   // "JP" (ISO-3166 code)

// A few locales can also specify variants.
$locale = new LocaleCaValencia; // The Valencian dialect of Catalan
$locale->variant();             // VariantValencia
$locale->variant()->code();     // "valencia"

本地化

创建一个区域并使用它来本地化您应用程序中的数据。

// Many ways to create locales
$locale = new LocaleEnGb;
$locale = Locale::create('en-GB'); // Use upper/lower case, hyphens/underscores/@
$locale = Locale::httpAcceptLanguage($_SERVER, $available_locales, $default_locale);

// Markup for HTML elements containing this locale
$locale->htmlAttributes();      // lang="ar" dir="rtl"

// Is text written left-to-right or right-to-left
$locale->direction();           // "ltr" or "rtl"

// Days of the week.
$locale->firstDay();            // 0=Sunday, 1=Monday, etc.
$locale->weekendStart();        // 0=Sunday, 1=Monday, etc.
$locale->weekendEnd();          // 0=Sunday, 1=Monday, etc.

// Measurement systems and paper sizes.
$locale->measurementSystem();   // "metric", "UK" or "US"
$locale->paperSize();           // "A4" or "US-Letter"

// Formatting numbers
$locale = new LocaleGr;         // Gujarati
$locale->digits('2014');        // "૨૦૧૪"
$locale = new LocaleItCh;       // Swiss Italian
$locale->number('12345678.9');  // "12'345'678.9"
$locale->percent(0.123);        // "12.3%"

// To sort data properly in MySQL, you need to specify a collation sequence.
// See https://dev.mysqlserver.cn/doc/refman/5.7/en/charset-unicode-sets.html
$locale->collation();           // "unicode_ci", "swedish_ci", etc.

复数规则

为每个区域定义了复数规则。以下示例显示,尽管英语和法语都有两种复数形式,但英语认为零是复数,而法语则认为它是单数。

$locale = new LocaleEn;
$locale->pluralRule()->plurals(); // 2 (English has two plural forms)
$locale->pluralRule()->plural(0); // 1 (zero is plural in English "zero apples")
$locale = new LocaleFr;
$locale->pluralRule()->plurals(); // 2 (French also has two plural forms)
$locale->pluralRule()->plural(0); // 0 (zero is singular in French "zero apple")

欢迎更新

请提供参考资料,例如