Countries.Net
1.0.0
See the version list below for details.
dotnet add package Countries.Net --version 1.0.0
NuGet\Install-Package Countries.Net -Version 1.0.0
<PackageReference Include="Countries.Net" Version="1.0.0" />
paket add Countries.Net --version 1.0.0
#r "nuget: Countries.Net, 1.0.0"
// Install Countries.Net as a Cake Addin #addin nuget:?package=Countries.Net&version=1.0.0 // Install Countries.Net as a Cake Tool #tool nuget:?package=Countries.Net&version=1.0.0
Countries.Net
Get access to in-memory database of countries with an easy to use api and zero dependencies.
Quick setup
to get started install the package using the NuGet package manager Install-Package Countries.Net
.
Country entity
the country entity expose the following properties:
name
: the name of the country, a collection of key-value pairs where:key
: three-letter ISO 639-3 language code.value
: a country name object with:official
: the official name of the country.common
: the common name of the country.
cca2
: code ISO 3166-1 alpha-2.cca3
: code ISO 3166-1 alpha-3.ccn3
: code ISO 3166-1 numeric.cioc
: code International Olympic Committee.independent
: ISO 3166-1 independence status (denotes the country is considered a sovereign state).status
: ISO 3166-1 assignment status.unMember
: UN Member status.flag
: the country flag.region
: the country region.subregion
: the country Subregion.landlocked
: the landlocked status.idd
: International Direct Dialing info, with:root
: the root geographical code prefix. e.g. +6 for New Zealand, +4 for UK.suffixes
: list of all suffixes assigned to this country. 4 for NZ, 809, 829, and 849 for Dominican Republic.
borders
: the country land borders.capital
: the country capital city(ies).altSpellings
: the country alternative spellings.tld
: country top-level domain list.latlng
: latitude and longitude.timezones
: the country time zones.languages
: the country languages, with:Name
: name of the language.ISO6391
: the 2 chars code of the language.ISO6392
: the 3 chars code of the language.
currencies
: the country currencies, with:Code
: code of the currency.Name
: the name of the currency.Symbol
: the symbol of the currency.
demonyms
: name of residents, translated & genderized, a collection of key-value pairs where:key
: three-letter ISO 639-3 language code.value
: genderized demonym object with:male
: the male resident name.female
: the female resident name.
here is a json representation of the country entity:
{
"Name": {
"eng": { "Official": "Kingdom of Morocco", "Common": "Morocco" },
"ara": { "Official": "المملكة المغربية", "Common": "المغرب" },
"fra": { "Official": "Royaume du Maroc", "Common": "Maroc" },
"spa": { "Official": "Reino de Marruecos", "Common": "Marruecos" }
},
"CCA2": "MA",
"CCA3": "MAR",
"CCN3": "504",
"CIOC": "MAR",
"Flag": "🇲🇦",
"Region": 0,
"Subregion": "Northern Africa",
"Independent": true,
"Status": "officially-assigned",
"UnMember": true,
"Landlocked": false,
"IDD": { "Root": "+2", "Suffixes": ["12"] },
"Borders": ["DZA", "ESH", "ESP"],
"Capital": ["Rabat"],
"AltSpellings": ["MA", "Kingdom of Morocco", "Al-Mamlakah al-Maġribiyah"],
"TLD": [".ma", "المغرب."],
"Latlng": [32, -5],
"Timezones": ["UTC"],
"Languages": [
{
"ISO6391": "ar",
"ISO6392": "ara",
"Name": "Arabic"
},
{
"ISO6391": "",
"ISO6392": "ber",
"Name": "Berber"
}
],
"Currencies": [
{
"Code": "MAD",
"Name": "Moroccan dirham",
"Symbol": "د.م."
}
],
"Demonyms": {
"eng": {
"Male": "Moroccan",
"Female": "Moroccan"
},
"fra": {
"Male": "Moroccan",
"Female": "Moroccan"
}
}
}
Usage
1. working with names:
to get the name of the county you can do an implicit conversion to string with:
// get the morocco country instance.
var morocco = Countries.Data.Morocco.Instance;
// the name prop is LocalizedCountryName object,
// but you can use the implicit conversion to get the name value.
string name = morocco.Name;
the name of the country is collection of translation of the common and official name, wrapped in a object type LocalizedCountryName
this object type exposes 3 functions GetName()
, GetName(string languageCode)
& GetName(string languageCode, bool useOfficialName)
starting with GetName(string languageCode, bool useOfficialName)
:
// get the morocco country instance.
var morocco = Countries.Data.Morocco.Instance;
// get the official name of the country in the given language,
// this example with get the French official name of the country
// to use the common name, pass false instead of true.
var name = morocco.Name.GetName("fra", true);
this function will give you the name of the country in the given language and will return either the common or official name based on the bool useOfficialName
parameters.
if no translation has been found for the given language, an exception will be thrown (this is applied to all 3 functions), if you don't want to throw an exception you can change this behavior with the static WorldCountriesConfiguration
configuration class:
// by setting the `ThrowIfNameNotFoundForALanguage` option to false, null will be returned instead of throwing the exception.
WorldCountriesConfiguration.ThrowIfNameNotFoundForALanguage = false;
for the function GetName(string languageCode)
it same as the previous one, the only difference is that the type of the returned name (common or official) will be detrmined from WorldCountriesConfiguration
configuration class
// if set to `true` we will return the official name of the country when retrieving the name,
// if set to `false` the common name will be used, by default is set to `true`
WorldCountriesConfiguration.UserOfficialNameWhenRetrievingCountryName = true;
finally the function GetName()
it relays on the WorldCountriesConfiguration
configuration class to do it job.
for the language to be used for retrieving the name is determined with this 2 options:
DefaultLanguage
: the default language to be used when retrieving the name of the country withGetName()
method, the default value iseng
.UseCurrentCultureInfoForCountryName
: if set totrue
theCultureInfo.CurrentCulture
language will be used when retrieving the name, otherwiseDefaultLanguage
will be used, by default is set tofalse
.
// the default language to be used with `GetName()` when retrieving the name of the country
WorldCountriesConfiguration.DefaultLanguage = `eng`;
// true to use the current CultureInfo language instead of DefaultLanguage
WorldCountriesConfiguration.UseCurrentCultureInfoForCountryName = false;
with that we can say that the implicit conversion of the country name to string is simply using the GetName()
under the hood
public static implicit operator string(LocalizedCountryName names) => names.GetName();
-2 working the countries data:
all countries are located in the Data
namespace, and you can interact with it using the CountriesService
.
// init the service
var service = new CountriesService();
// to get the list of all countries:
var countries = service.GetAll();
// find the country with the given CCA2, CCA3, CCN3 or CIOC code:
var country = service.FindByCode("MA");
// find the country by the name:
var country = service.FindByName("Maroc");
// find list of countries in a given region:
var countries = service.FindByRegion(Region.Africa);
// find list of countries using the given language code:
var countries = service.FindByLanguage("ara");
// find list of countries using the given currency code:
var countries = service.FindByCurrency("MAD");
// get the list of countries as key-value, where the key is Country.CCA2 and the value is the name:
KeyValuePair<string, string> countries = service.GetAsKeyValue();
you can find the list of all supported countries here.
Credits
Thanks to:
- RestCountries api for the countries data.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Countries.Net:
Package | Downloads |
---|---|
Countries.Net.DependencyInjection
Get the access to the countries dataset with no dependencies and no API calls. |
GitHub repositories
This package is not used by any popular GitHub repositories.