wp-cldr
  • Package
  • Class

Packages

  • wp-cldr

Classes

  • WP_CLDR

Class WP_CLDR

Gives WordPress developers easy access to localized country, region, language, currency, and calendar info from the Unicode Common Locale Data Repository.

Examples:

The default locale is English:

$cldr = new WP_CLDR();
$territories_in_english = $cldr->get_territories();

You can override the default locale per-call by passing in a language slug in the second parameter:

$germany_in_arabic = $cldr->get_territory_name( 'DE' , 'ar_AR' );

Use a convenience parameter during instantiation to change the default locale:

$cldr = new WP_CLDR( 'fr' );
$germany_in_french = $cldr->get_territory_name( 'DE' );
$us_dollar_in_french = $cldr->get_currency_name( 'USD' );
$canadian_french_in_french = $cldr->get_language_name( 'fr-ca' );
$canadian_french_in_english = $cldr->get_language_name( 'fr-ca' , 'en' );
$german_in_german = $cldr->get_language_name( 'de_DE' , 'de-DE' );
$bengali_in_japanese = $cldr->get_language_name( 'bn_BD' , 'ja_JP' );
$us_dollar_symbol_in_simplified_chinese = $cldr->get_currency_symbol( 'USD', 'zh' );
$africa_in_french = $cldr->get_territory_name( '002' );

Switch locales after the object has been created:

$cldr->set_locale( 'en' );
$us_dollar_in_english = $cldr->get_currency_name( 'USD' );

Testing:

The class includes a suite of PHPUnit tests. To run them, call phpunit from the plugin directory.

Package: wp-cldr
Link: http://cldr.unicode.org
Link: https://github.com/unicode-cldr/cldr-json
Autounit: wp-cldr
Located at

Methods summary

public
# __construct( string $locale = 'en', boolean $use_cache = true )

Constructs a new instance of the class, including setting defaults for locale and caching.

Constructs a new instance of the class, including setting defaults for locale and caching.

Parameters

$locale
Optional. A WordPress locale code.
$use_cache
Optional. Whether to use caching (primarily used to suppress caching for unit testing).
public
# set_locale( string $locale )

Sets the locale.

Sets the locale.

Parameters

$locale
A WordPress locale code.
public static string
# get_cldr_locale( string $wp_locale )

Gets CLDR code for the equivalent WordPress locale code.

Gets CLDR code for the equivalent WordPress locale code.

Parameters

$wp_locale
A WordPress locale code.

Returns

string
The equivalent CLDR locale code.
public static string
# get_cldr_json_path( string $cldr_locale, string $bucket )

Gets the absolute path of a CLDR JSON file for a given WordPress locale and CLDR data item.

Gets the absolute path of a CLDR JSON file for a given WordPress locale and CLDR data item.

Parameters

$cldr_locale
A CLDR locale.
$bucket
The CLDR data item.

Returns

string
An array with the CLDR data from the file, or an empty array if no match with any CLDR data files.
public static boolean
# is_cldr_json_available( string $cldr_locale, string $bucket )

Checks to see if there is an installed CLDR JSON file for a given CLDR locale and data item.

Checks to see if there is an installed CLDR JSON file for a given CLDR locale and data item.

Parameters

$cldr_locale
The CLDR locale.
$bucket
The CLDR data item.

Returns

boolean
Whether or not the CLDR JSON file is available.
public static array
# get_cldr_json_file( string $cldr_locale, string $bucket )

Loads a CLDR JSON data file.

Loads a CLDR JSON data file.

Parameters

$cldr_locale
The CLDR locale.
$bucket
The CLDR data item.

Returns

array
An array with the CLDR data from the file, or an empty array if no match with any CLDR data files.
public static string
# get_best_available_cldr_json_locale( string $locale, string $bucket )

Uses fallback logic to get the best available CLDR JSON locale for a given WordPress locale.

Uses fallback logic to get the best available CLDR JSON locale for a given WordPress locale.

Parameters

$locale
A WordPress locale code.
$bucket
The CLDR data item.

Returns

string
The best available CLDR JSON locale, or an empty string if no JSON locale is available.
public
# flush_wp_cache_for_locale_bucket( string $locale, string $bucket )

Flushes the WordPress object cache for a single CLDR data item for a single locale.

Flushes the WordPress object cache for a single CLDR data item for a single locale.

Parameters

$locale
A WordPress locale code.
$bucket
A CLDR data item.
public
# flush_all_wp_caches( )

Clears the WordPress object cache for all CLDR data items across all locales.

Clears the WordPress object cache for all CLDR data items across all locales.

public string
# get_territory_name( string $territory_code, string $locale = '' )

Gets a localized country or region name.

Gets a localized country or region name.

Parameters

$territory_code
An ISO 3166-1 country code, or a UN M.49 region code.
$locale
Optional. A WordPress locale code.

Returns

string
The name of the territory in the provided locale.

Link

ISO 3166 country codes
UN M.49 region codes
public string
# get_currency_symbol( string $currency_code, string $locale = '' )

Gets a localized currency symbol.

Gets a localized currency symbol.

Parameters

$currency_code
An ISO 4217 currency code.
$locale
Optional. A WordPress locale code.

Returns

string
The symbol for the currency in the provided locale.

Link

ISO 4217 currency codes
public string
# get_currency_name( string $currency_code, string $locale = '' )

Gets a localized currency name.

Gets a localized currency name.

Parameters

$currency_code
An ISO 4217 currency code.
$locale
Optional. A WordPress locale code.

Returns

string
The name of the currency in the provided locale.

Link

ISO 4217 currency codes
public string
# get_language_name( string $language_code, string $locale = '' )

Gets a localized language name.

Gets a localized language name.

Parameters

$language_code
An ISO 639 language code.
$locale
Optional. A WordPress locale code.

Returns

string
The name of the language in the provided locale.

Link

ISO 639 language codes
public array
# get_territories( string $locale = '' )

Gets all country and region names in a locale.

Gets all country and region names in a locale.

Parameters

$locale
Optional. A WordPress locale code.

Returns

array
An associative array of ISO 3166-1 alpha-2 country codes and UN M.49 region codes, along with localized names, from CLDR

Link

ISO 3166 country codes
UN M.49 region codes
public array
# get_languages( string $locale = '' )

Gets all language names in a locale.

Gets all language names in a locale.

Parameters

$locale
Optional. A WordPress locale code.

Returns

array
An associative array of ISO 639 codes and localized language names from CLDR

Link

ISO 639 language codes
public string
# get_telephone_code( string $country_code )

Gets telephone code for a country.

Gets telephone code for a country.

Parameters

$country_code
A two-letter ISO 3166 country code.

Returns

string
The telephone code for the provided country.

Link

CLDR Telephone Code Data
ISO 3166 country codes
public string
# get_first_day_of_week( string $country_code )

Gets the day which typically starts a calendar week in a country.

Gets the day which typically starts a calendar week in a country.

Parameters

$country_code
A two-letter ISO 3166 country code.

Returns

string
The first three characters, in lowercase, of the English name for the day considered to be the start of the week.

Link

CLDR week data
ISO 3166 country codes
public array
# get_currency_for_all_countries( )

Gets the currency used in each country worldwide.

Gets the currency used in each country worldwide.

Returns

array
An associative array of ISO 3166 country codes and the ISO 4217 code for the currency currently used in each country.

Link

ISO 3166 country codes
ISO 4217 currency codes
public string
# get_currency_for_country( string $country_code )

Gets the currency currently used in a country.

Gets the currency currently used in a country.

Parameters

$country_code
A two-letter ISO 3166-1 country code.

Returns

string
The three-letter ISO 4217 code for the currency currently used in that country.

Link

ISO 3166 country codes
ISO 4217 currency codes
public array
# get_countries_for_all_currencies( )

Gets the countries that use each currency in use today.

Gets the countries that use each currency in use today.

Returns

array
An associative array of ISO 4217 currency codes and then an array of the ISO 3166 codes for countries which currently each currency.

Link

ISO 4217 currency codes
ISO 3166 country codes
public array
# get_countries_for_currency( string $currency_code )

Gets the countries that use a particular currency.

Gets the countries that use a particular currency.

Parameters

$currency_code
A three-letter ISO 4217 currency code.

Returns

array
The ISO 3166 codes for the countries which currently the currency.

Link

ISO 4217 currency codes
ISO 3166 country codes
public array
# get_territories_contained( string $region_code )

Gets the countries contained by a region code.

Gets the countries contained by a region code.

Parameters

$region_code
A UN M.49 region code or a two-letter ISO 3166-1 country code.

Returns

array
The countries included in that region, or the country if $region_code is a country.

Link

CLDR info page on territory containment
ISO 3166 country codes
UN M.49 region codes
public array
# get_languages_spoken( string $country_code )

Gets the languages spoken in a country, in descending order of use.

Gets the languages spoken in a country, in descending order of use.

Parameters

$country_code
A two-letter ISO 3166-1 country code.

Returns

array
An associative array with the key of a language code and the value of the percentage of population which speaks the language in that country.

Link

ISO 3166 country codes
Detail on CLDR language information
public string
# get_most_spoken_language( string $country_code )

Gets the most widely spoken language spoken in a country.

Gets the most widely spoken language spoken in a country.

Parameters

$country_code
A two-letter ISO 3166-1 country code.

Returns

string
The ISO 639 code for the language most widely spoken in the country.

Link

ISO 3166 country codes
Detail on CLDR language information
ISO 639 language codes
public array
# get_territory_info( string $country_code )

Gets GDP, population, and language information for a country.

Gets GDP, population, and language information for a country.

Parameters

$country_code
Optional. A two-letter ISO 3166-1 country code.

Returns

array
CLDR's territory information.

Link

ISO 3166 country codes
CLDR's territory information
public array
# get_time_zone_cities( string $locale = '' )

Gets the time zone city names -- aka exemplars -- for a locale.

Gets the time zone city names -- aka exemplars -- for a locale.

Parameters

$locale
Optional. A WordPress locale code.

Returns

array
As associative array of time zone IDs (e.g. Europe/Istanbul) and the localized exemplar city for each.

Link

IANA time zone
CLDR info on time zone names
public string
# get_time_zone_city( string $time_zone_id, string $locale = '' )

Gets the time zone city name -- aka exemplar -- for a time zone ID.

Gets the time zone city name -- aka exemplar -- for a time zone ID.

Parameters

$time_zone_id
An IANA time zone ID (e.g. Europe/Istanbul).
$locale
Optional. A WordPress locale code.

Returns

string
The localized name of the time zone exemplar city.

Link

IANA time zone
CLDR info on time zone names

Constants summary

string CACHE_GROUP

The cache group name to use for the WordPress object cache.

The cache group name to use for the WordPress object cache.

# 'wp-cldr'
string CLDR_VERSION

The CLDR version, which the class uses to determine path to JSON files.

The CLDR version, which the class uses to determine path to JSON files.

# '29.0.0'
wp-cldr API documentation generated by ApiGen