zerossb / laravel-world
提供国家、州和城市关系数据库。(源自 khsing/laravel-world)
1.0
2024-07-03 18:11 UTC
Requires
- php: >7.3|^8.0
- laravel/framework: ^10.0|^11.0
Requires (Dev)
- mockery/mockery: ^1.6
- phpunit/phpunit: ^10.1|^11.0
This package is auto-updated.
Last update: 2024-09-03 18:39:43 UTC
README
本包专注于全球国家、地区和城市数据库,支持Laravel本地化。
概念
本包包含5个主要对象。
- World:地球。
- Continent:7个大洲
- Country:248个国家
- Division:如州/省等行政划分。
- City:地区最后一级行政单位,有些城市属于国家,有些属于行政划分。
属性
通用属性
name
:地区通用名称(英文)。full_name
:完整名称或官方名称(英文)。code
:ISO-3166-1-alpha2/ISO-3166-2代码。local_name
:通用名称的翻译。local_full_name
:完整名称的翻译。local_alias
:不同语言中的别名。local_abbr
:缩写。
国家特定属性
emoji
:国家的表情符号国旗。capital
:该国的首都。code_alpha3
:ISO-3166-1-alpha3代码。currency_code
:ISO-4177货币代码,例如USD、CNY。currency_name
:ISO-4177货币名称。local_currency_name
:本地化货币名称。
示例
use Khsing\World\World; $china = World::getByCode('cn'); $china->setLocale('zh-cn'); $china->name; // China $china->local_name; // 中国 $china->full_name; // People's Republic of China $china->local_full_name; // 中华人民共和国 $china->emoji; // 🇨🇳 $china->callingcode; // 86 $china->code; // CN $china->code_alpha3; // CHN $china->has_division; // true $china->currency_code; // CNY $china->currency_name; // Yuan Renminbi $china->local_currency_name; // 人民币
本地化
目前,仅支持英语(默认和回退)和中文简体 zh-cn
。区域设置遵循Laravel项目中的config/app.php
设置。
设置
composer require
composer require khsing/world
- 将服务提供者添加到
config/app.php
中,(仅适用于Laravel 5.5之前)
'providers' => [ // ... Khsing\World\WorldServiceProvider::class, ]
- 发布和初始化
php artisan vendor:publish --force --provider="Khsing\World\WorldServiceProvider" composer dump-autoload php artisan world:init
用法
- 获取所有大洲
use Khsing\World\World; World::Continents()
- 获取所有国家
use Khsing\World\World; World::Countries()
- 通过代码获取国家/城市/行政划分
use Khsing\World\World; World::getByCode('cn'); // iso-3166 alpha 2 code World::getByCode('chn'); // iso-3166 alpha 3 code World::getByCode('cn-11'); // Beijing
- 获取属于某个大洲的国家
use Khsing\World\Models\Continent; $asia = Continent::getByCode('AS'); $countries = $asia->countries()->get(); // or use children method $countries = $asia->children();
- 获取大洲或上级
$china = Country::getByCode('cn'); $asia = $china->parent();
- 通过国家获取行政划分/州/省
$china = Country::getByCode('cn'); $provinces = $china->divisions()->get() // or use children method $provinces = $china->children();
- 通过国家或行政划分获取城市。
$china = Country::getByCode('cn'); // check has_division to determine next level is division or city. $china->has_division; // true, otherwise is false $regsions = $china->children();
贡献
如果您想为此库做出贡献,欢迎提交issue和pr。请按照以下步骤操作。
- 启动一个新的Laravel项目并安装此库。
- 安装orangehill/iseed。
- 通过SQL修改数据。
- 通过以下命令生成种子:
artisan iseed world_cities,world_cities_locale,world_continents,world_continents_locale,world_countries,world_countries_locale,world_divisions,world_divisions_locale
- 将
delete()
替换为truncate()
,cd database/seeders/ && sed -i 's/->delete()/->truncate()/g' World*.php
- 将种子文件复制到库中。
- 提交您的作品。 ;)
待办事项
- 更改种子数据的方式,例如从JSON加载数据?
- 添加前端支持
- 找到更新数据集的方法
数据来源
- ISO 639-1 Standard Language Codes:语言代码
- ISO 639-1 standard language codes:语言代码
- United Nations Statistics Division: Standard country or area codes for statistical use (M49):ISO-3166-alpha3代码和国家列表。
- ISO 3166-2:主要数据来源
感谢
关于
本包在MIT许可下发布。如果您有任何问题或建议,请随时提交issue,或通过电子邮件联系我 Guixing<khsing.cn(AT)gmail.com>。
祝您愉快。