sylvaincombes/php-lcid

一个库,用于从微软LCID中查找ISO语言代码

v1.0 2018-03-06 13:42 UTC

This package is auto-updated.

Last update: 2024-09-25 08:59:29 UTC


README

这是一个PHP库,旨在为给定的Microsoft LCID获取有效的Unicode区域设置。

安装

composer require sylvaincombes/php-lcid

用法

基本用法

<?php

require '../vendor/autoload.php';

use SylvainCombes\Lcid\Finder;

$finder = new Finder();

// NB : find methods returns null if not found


// 1. Finding a locale by lcid

// You should always use this method (use fallbacks if no 'perfect match' to try to answer something to your query instead of failing if no 'perfect match')
$locale = $finder->findByLcidWithFallback(8201);
// Should give you $locale == 'en'

// You can avoid searching in fallbacks if you really want to
$locale = $finder->findByLcid(1036);
// Should give you $locale == 'fr_FR'


// 2. Finding lcid(s) by locale
$localeLcid = $finder->findOneByLocale('fr');
// Should give you $localeLcid == 1036
// NB : this method is limited as it will return only 'perfect match'

$localeLcid = $finder->findByLocale('fr');
/**
 * Get an array or null in response (search everywhere)
 * First result in array should be the 'best' match
 * 
 * Return example :
 * array(8) {
 *     [0] => int(1036)
 *     [1] => int(11276)
 *     [2] => int(9228)
 *     [3] => int(12300)
 *     [4] => int(13324)
 *     [5] => int(14348)
 *     [6] => int(10252)
 *     [7] => int(7180)
 *   }
 * 
 */

“高级”用法

使用自定义数据

<?php

require '../vendor/autoload.php';

use SylvainCombes\Lcid\Finder;

// Pass your json file in constructor
// Your json must validate against the json schema, see src/SylvainCombes/Lcid/Resources/datas-schema.json
$finder = new Finder('src/Me/Resources/my-lcids.json');

// ...

为什么选择这个库

有一天,我在工作中需要消费某些暴露了微软SharePoint数据的API服务。语言信息仅通过微软LCID提供,而我需要它们以可利用的ICU标准区域设置的形式。

经过长时间的搜索,我发现这个“简单”任务实际上并不容易,因为我找不到一个详尽的完整列表或现有的PHP项目来满足我的需求。此外,我还了解到,根据php-intl扩展,一些区域设置最终并不匹配。

因此,我使用从网络上找到的一些数据源和一些其他库和工具进行了修改,希望永远不要再做这个非常无聊的任务,也许还能帮助其他开发者 :)

有趣的事实

LCID在Windows Vista中被弃用,微软建议开发者使用BCP47风格的标签代替(uloc_toLanguageTag)。

贡献

欢迎所有贡献。

更新数据列表

有一个命令可以构建数据,它所做的操作是

  1. sindresorhus/lcid获取一个JSON文件 - 这是我们开始时不可编辑的数据点
  2. 检查区域设置是否在symfony/intl区域设置列表中找到
    • 如果是,则保留它们
    • 如果不是,尝试查找是否找到了区域设置或语言
      • 如果是,则将其添加到后备列表中
      • 如果不是,则删除此条目
  3. 将PHP数组加载到文件src/SylvainCombes/Lcid/Resources/datas-manual.php中,如果某些LCID代码不在数据中,请使用lcid、语言、区域设置字段将其添加到列表或后备条目中。

因此,要编辑/删除/添加不在基本JSON中的某些数据,您应该编辑src/SylvainCombes/Lcid/Resources/datas-manual.php并重新运行命令

php bin/console lcid:generate-datas -v

并且datas.json将被更新。

注意:这也意味着您不能覆盖从sindresorhus/lcid抓取的基本JSON提供的数据

测试

启动项目测试

composer run test

PHP代码风格检查

composer run lint

您还可以“自动修复”

composer run fix

参考

ICU区域设置

LCID

BCP 47

格式

使用的库

必需的

开发中使用的

  • phpunit - 用于 ... 测试
  • symfony/intl - 在开发过程中用于检查数据源中的区域设置是否与提供的区域设置列表匹配(我发现这个区域设置列表与php-intl扩展提供的不同,我倾向于认为这个包中的区域设置更健壮且更广泛使用,而不是依赖于本地安装/配置的php扩展)
  • symfony/console - 在开发过程中用于构建控制台命令数据
  • phpcs + coke + symfony2-coding-standard - 优化我的工作环境
  • json schema lint - 用于检查/测试编写JSON模式
  • regex101 - 用于编写/测试区域正则表达式