arthurydalgo/laravel-iso-countries

提供即用型Laravel模型和关系,用于国家(ISO 3166)、语言(ISO 639-1)和货币(ISO 4217)信息,支持多语言。

dev-main 2024-06-13 14:12 UTC

This package is not auto-updated.

Last update: 2024-09-20 13:39:36 UTC


README

⚠️ 这是一个分支,因为官方仓库不支持Laravel 11 ⚠️

Latest Version on Packagist GitHub Tests Action Status Total Downloads

此包提供即用型应用程序模型,并在数据库中用来自不同来源的ISO数据初始化。此包可用于多语言应用程序,并支持几乎任何地区中的国家/语言/货币名称。

包含的数据集

与其他包不同,此包还包括相关数据关系,例如

// Official languages spoken in Luxembourg ('LU')
Country::find('LU')->languages;

// Currencies used in Ghana ('GH')
Country::find('GH')->currencies;

// Countries that have Spanish ('es') as one of their official languages 
Language::find('es')->countries;

// Countries that use the Euro ('EUR') as currency
Currency::find('EUR')->countries;

安装

您可以通过composer安装此包

composer require arthurydalgo/laravel-iso-countries

此包的最新版本需要PHP版本8.0或更高。如果您需要PHP 7.4的支持,请安装此包的版本2。

迁移

无需运行任何迁移。所有国家数据信息都存储在存储在此包中的预编译SQLITE数据库中。

默认情况下,此数据库包括所有国家/语言/货币名称翻译成英语、德语、法语和西班牙语。如果您想使用其他语言编译自己的数据库,请参见此处说明

重建数据库

国家级别的ISO数据变化不频繁。不过,如果您何时想更新ISO数据到最新版本,您可以手动重新初始化表

配置

可选地,您可以使用以下命令发布配置文件

php artisan vendor:publish --provider="Io238\ISOCountries\ISOCountriesServiceProvider" --tag="config"

在配置中,您可以定义您想在数据库中存储哪些国家/语言/货币名称的翻译。

这是发布配置文件的内容

[
    // Supported locales for names (countries, languages, currencies)
    'locales' => [
        'en',
        'de',
        'fr',
        'es',
    ],

    // Path for storing your own SQLITE database
    'database_path' => database_path('iso-countries.sqlite'),
];

更改配置后,您需要使用以下命令重新构建数据库

php artisan db:seed countries:build

这将在您的项目中创建一个新的SQLITE数据库,并将其存储在database/iso-countries.sqlite。从.gitignore中排除此文件,以便在所有环境中可用。

# database/.gitignore

*.sqlite*
!iso-countries.sqlite

使用方法

国家模型

Country::find('AD');

Io238\ISOCountries\Models\Country {
     id: "AD",
     alpha3: "AND",
     name: "{"en":"Andorra","de":"Andorra","fr":"Andorre","es":"Andorra"}",
     native_name: "Andorra",
     capital: "Andorra la Vella",
     top_level_domain: ".ad",
     calling_code: "376",
     region: "Europe",
     subregion: "Southern Europe",
     population: 78014,
     lat: 42.5,
     lon: 1.5,
     demonym: "Andorran",
     area: 468,
   }

语言模型

Country::find('pt');

Io238\ISOCountries\Models\Language {
     id: "pt",
     iso639_2: "por",
     iso639_2b: null,
     name: "{"en":"Portuguese","de":"Portugiesisch","fr":"portugais","es":"portugu\u00e9s"}",
     native_name: "Português",
     family: "Indo-European",
     wiki_url: "https://en.wikipedia.org/wiki/Portuguese_language",
   }

货币模型

Currency::find('COP');

Io238\ISOCountries\Models\Currency {
     id: "COP",
     name: "{"en":"Colombian Peso","de":"Kolumbianischer Peso","fr":"peso colombien","es":"peso colombiano"}",
     name_plural: "Colombian pesos",
     symbol: "CO$",
     symbol_native: "$",
     decimal_digits: 0,
     rounding: 0,
   }

查询关系

所有模型都有预定义的多对多关系,可以查询

// Retrieve languages that are spoken in Luxembourg
Country::find('LU')->languages;

Illuminate\Database\Eloquent\Collection {                                                                  
     all: [                                                                                                        
       Io238\ISOCountries\Models\Language {                                                                   
         id: "de",                                                                                                 
         iso639_2: "deu",                                                                                          
         iso639_2b: "ger",                                                                                         
         name: "{"en":"German","de":"Deutsch","fr":"allemand","es":"alem\u00e1n"}",                                
         native_name: "Deutsch",                                                                                   
         family: "Indo-European",                                                                                  
         wiki_url: "https://en.wikipedia.org/wiki/German_language",                                                
       },                                                                                                          
       Io238\ISOCountries\Models\Language {                                                                   
         id: "fr",                                                                                                 
         iso639_2: "fra",                                                                                          
         iso639_2b: "fre",                                                                                         
         name: "{"en":"French","de":"Franz\u00f6sisch","fr":"fran\u00e7ais","es":"franc\u00e9s"}",                 
         native_name: "français, langue française",                                                                
         family: "Indo-European",                                                                                  
         wiki_url: "https://en.wikipedia.org/wiki/French_language",                                                
       },                                                                                                          
       Io238\ISOCountries\Models\Language {                                                                   
         id: "lb",                                                                                                 
         iso639_2: "ltz",                                                                                          
         iso639_2b: null,                                                                                          
         name: "{"en":"Luxembourgish","de":"Luxemburgisch","fr":"luxembourgeois","es":"luxemburgu\u00e9s"}",       
         native_name: "Lëtzebuergesch",                                                                            
         family: "Indo-European",                                                                                  
         wiki_url: "https://en.wikipedia.org/wiki/Luxembourgish_language",                                         
       },                                                                                                          
     ],                                                                                                            
   }                                                                                                                                                                                                     
// Retrieve all countries that use the Euro (EUR) as currency.
Currency::find('EUR')->countries->pluck('name');

Illuminate\Support\Collection {
     all: [
       "Andorra",
       "Austria",
       "Åland Islands",
       "Belgium",
       "St. Barthélemy",
       "Cyprus",
       "Germany",
       "Estonia",
       "Spain",
       "Finland",
       "France",
       "French Guiana",
       "Guadeloupe",
       "Greece",
       "Ireland",
       "Italy",
       "Lithuania",
       "Luxembourg",
       "Latvia",
       "Monaco",
       "Montenegro",
       "St. Martin",
       "Martinique",
       "Malta",
       "Netherlands",
       "St. Pierre & Miquelon",
       "Portugal",
       "Réunion",
       "Slovenia",
       "Slovakia",
       "San Marino",
       "French Southern Territories",
       "Vatican City",
       "Republic of Kosovo",
       "Mayotte",
       "Zimbabwe",
     ],
   }

本地化名称

每个包模型(国家、语言、货币)都有一个->name属性,它将使用spatie/laravel-translatable包在应用程序的本地化中显示。默认配置将迁移本地化名称为endefres。这可以在配置中调整。配置中的app.localeapp.fallback_locale将自动包含。

// Set the app locale
app()->setLocale('fr');

// Retrieve the top 10 countries in Africa (by population):
Io238\ISOCountries\Models\Country::where('region', 'Africa')->orderByDesc('population')->limit(10)->pluck('name');

// Country names will be returned according to the app locale (fr = French)
Illuminate\Support\Collection {
     all: [
       "Nigéria",
       "Éthiopie",
       "Égypte",
       "Congo-Kinshasa",
       "Afrique du Sud",
       "Tanzanie",
       "Kenya",
       "Algérie",
       "Soudan",
       "Ouganda",
     ],
   }

属性转换

如果您已经在模型中使用ISO代码,您可以通过将它们转换为国家/语言/货币模型来丰富它们

use Io238\ISOCountries\Casts\Currency;

class MyModel{
    
    // ...

    protected $casts = [
        'currency' => Currency::class,
    ];
    
    // ...
}

// Now you can dynamically retrieve all meta data associated with the currency
MyModel::first()->currency;

Io238\ISOCountries\Models\Currency {
     id: "JPY",
     name: "{"en":"Japanese Yen","de":"Japanischer Yen","fr":"yen japonais","es":"yen"}",
     name_plural: "Japanese yen",
     symbol: "¥",
     symbol_native: "",
     decimal_digits: 0,
     rounding: 0,
   }


// When filling the model, the ISO code (string) or the model can be used 
MyModel::first()->update(['currency' => 'USD']);
MyModel::first()->update(['currency' => Io238\ISOCountries\Models\Currency::find('USD')]);

测试

composer test

贡献

请参阅贡献指南获取详细信息。

安全漏洞

请查阅我们的安全策略,了解如何报告安全漏洞。

鸣谢

许可

MIT许可(MIT)。请参阅许可文件获取更多信息。