Commit 4f35efa0 authored by RedMatrix's avatar RedMatrix
Browse files

Revert "Language names via intl library."

parent ae9d0826
......@@ -250,7 +250,8 @@ function detect_language($s) {
* By default we use the localized language name. You can switch the result
* to any language with the optional 2nd parameter $l.
*
* $s and $l should be in 2-letter ISO 639-1 format
* $s and $l can be in any format that PHP's Locale understands. We will mostly
* use the 2-letter ISO 639-1 (en, de, fr) format.
*
* If nothing could be looked up it returns $s.
*
......@@ -258,30 +259,11 @@ function detect_language($s) {
* @param $l (optional) In which language to return the name
* @return string with the language name, or $s if unrecognized
*/
require_once(__DIR__ . '/../library/intl/vendor/autoload.php');
use CommerceGuys\Intl\Language\LanguageRepository;
function get_language_name($s, $l = null) {
// get() expects the second part to be in upper case
if(strpos($s,'-') !== false) $s = substr($s,0,2) . strtoupper(substr($s,2));
if($l !== null && strpos($l,'-') !== false) $l = substr($l,0,2) . strtoupper(substr($l,2));
if($l === null)
$l = $s;
$languageRepository = new LanguageRepository;
// Sometimes intl doesn't like the second part at all ...
try {
$language = $languageRepository->get($s, $l);
}
catch(CommerceGuys\Intl\Exception\UnknownLanguageException $e) {
$s = substr($s,0,2);
if($l !== null) $l = substr($s,0,2);
try {
$language = $languageRepository->get($s, $l);
}
catch(CommerceGuys\Intl\Exception\UnknownLanguageException $e) {
return $s; // Give up
}
}
return $language->getName();
logger('get_language_name: for ' . $s . ' in ' . $l . ' returns: ' . Locale::getDisplayLanguage($s, $l), LOGGER_DEBUG);
return Locale::getDisplayLanguage($s, $l);
}
......@@ -1629,7 +1629,7 @@ function lang_selector() {
$ll = substr($l,5);
$ll = substr($ll,0,strrpos($ll,'/'));
$selected = (($ll === $a->language && (x($_SESSION, 'language'))) ? $ll : $selected);
$lang_options[$ll] = get_language_name($ll, $ll) . " ($ll)";
$lang_options[$ll]=$ll;
}
}
......
composer.lock
vendor/*
language: php
php:
- 5.4
- 5.5
- 5.6
- hhvm
install:
- composer self-update
- composer install
script:
- ./vendor/bin/phpunit -c ./phpunit.xml --coverage-text --strict
The MIT License (MIT)
Copyright (c) 2014 Commerce Guys
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
intl
=====
[![Build Status](https://travis-ci.org/commerceguys/intl.svg?branch=master)](https://travis-ci.org/commerceguys/intl)
A PHP 5.4+ internationalization library, powered by CLDR data.
Features:
- NumberFormatter, inspired by [intl](http://php.net/manual/en/class.numberformatter.php).
- Currencies
- Countries
- Languages
Coming soon: date formatting.
Why not use the intl extension?
-------------------------------
The intl extension isn't present by default on PHP installs, requiring
it can hurt software adoption.
Behind the scenes the extension relies on libicu which includes the CLDR dataset,
but depending on the OS/distribution used, could be several major CLDR releases behind.
Since the CLDR dataset is freely available in JSON form, it is possible to
reimplement the intl functionality in pure PHP code while ensuring that the
dataset is always fresh.
Having access to the CLDR dataset also makes it possible to offer additional APIs,
such as listing all currencies.
More backstory can be found in [this blog post](https://drupalcommerce.org/blog/15916/commerce-2x-stories-internationalization).
Formatting numbers
------------------
Formats numbers (decimals, percents, currency amounts) using locale-specific rules.
This ensures that the decimal and grouping separators, the position of the currency
symbol, as well as the actual symbol used match what the user is expecting.
The amounts passed for formatting should already be rounded, because the
formatter doesn't do any rounding of its own.
```php
use CommerceGuys\Intl\Currency\CurrencyRepository;
use CommerceGuys\Intl\NumberFormat\NumberFormatRepository;
use CommerceGuys\Intl\Formatter\NumberFormatter;
$currencyRepository = new CurrencyRepository;
$numberFormatRepository = new NumberFormatRepository;
$currency = $currencyRepository->get('USD');
$numberFormat = $numberFormatRepository->get('en');
$decimalFormatter = new NumberFormatter($numberFormat);
echo $decimalFormatter->format('1234.99'); // 123,456.99
$percentFormatter = new NumberFormatter($numberFormat, NumberFormatter::PERCENT);
echo $percentFormatter->format('0.75'); // 75%
$currencyFormatter = new NumberFormatter($numberFormat, NumberFormatter::CURRENCY);
echo $currencyFormatter->formatCurrency('2.99', $currency); // $2.99
// The accounting pattern shows negative numbers differently and is used
// primarily for amounts shown on invoices.
$invoiceCurrencyFormatter = new NumberFormatter($numberFormat, NumberFormatter::CURRENCY_ACCOUNTING);
echo $invoiceCurrencyFormatter->formatCurrency('-2.99', $currency); // (2.99$)
// Arabic, Arabic extended, Bengali, Devanagari digits are supported as expected.
$currency = $currencyRepository->get('USD', 'ar');
$numberFormat = $numberFormatRepository->get('ar');
$currencyFormatter = new NumberFormatter($numberFormat, NumberFormatter::CURRENCY);
echo $currencyFormatter->formatCurrency('1230.99', $currency); // US$ ١٬٢٣٠٫٩٩
// Parse formatted values into numeric values.
echo $currencyFormatter->parseCurrency('US$ ١٬٢٣٠٫٩٩', $currency); // 1230.99
```
Currencies
----------
```php
use CommerceGuys\Intl\Currency\CurrencyRepository;
// Reads the currency definitions from resources/currency.
$currencyRepository = new CurrencyRepository;
// Get the USD currency using the default locale (en).
$currency = $currencyRepository->get('USD');
echo $currency->getCurrencyCode(); // USD
echo $currency->getNumericCode(); // 840
echo $currency->getFractionDigits(); // 2
echo $currency->getName(); // US Dollar
echo $currency->getSymbol(); // $
echo $currency->getLocale(); // en
// Get the USD currency using the fr-FR locale.
$currency = $currencyRepository->get('USD', 'fr-FR');
echo $currency->getName(); // dollar des États-Unis
echo $currency->getSymbol(); // $US
echo $currency->getLocale(); // fr-FR
$allCurrencies = $currencyRepository->getAll();
```
Countries
---------
```php
use CommerceGuys\Intl\Country\CountryRepository;
// Reads the country definitions from resources/country.
$countryRepository = new CountryRepository;
// Get the US country using the default locale (en).
$country = $countryRepository->get('US');
echo $country->getCountryCode(); // US
echo $country->getTelephoneCode(); // 1
echo $country->getName(); // United States
// Get the US country using the fr-FR locale.
$country = $countryRepository->get('US', 'fr-FR');
echo $country->getName(); // États-Unis
$allCountries = $countryRepository->getAll();
```
Languages
---------
```php
use CommerceGuys\Intl\Language\LanguageRepository;
// Reads the language definitions from resources/language.
$languageRepository = new LanguageRepository;
// Get the german language using the default locale (en).
$language = $languageRepository->get('de');
echo $language->getLanguageCode(); // de
echo $language->getName(); // German
// Get the german language using the fr-FR locale.
$language = $languageRepository->get('de', 'fr-FR');
echo $language->getName(); // allemand
$allLanguages = $languageRepository->getAll();
```
Implementing the library
------------------------
While the library can be used as-is, most applications will want to store the
relevant definitions (currencies, countries, etc) in the database.
This allows for better performance while giving users the ability to modify and
expand the data.
Taking currencies as an example, a merchant frequently wants to be able to:
- Define custom currencies.
- Enable/disable existing currencies
- Modify an existing currency (changing the default number of fraction digits, for example).
This would be accomplished by using the CurrencyRepository to get all default currencies and
insert them into the database. The doctrine entity (or any similar data object) would then implement
the CurrencyInterface so that the NumberFormatter can continue to work.
Related projects
----------------
[commerceguys/pricing](http://github.com/commerceguys/pricing) provides a Price object.
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="CommerceGuys Intl Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
</phpunit>
{
"AF": {
"name": "Afganistan"
},
"AX": {
"name": "Ålandeilande"
},
"AL": {
"name": "Albanië"
},
"DZ": {
"name": "Algerië"
},
"AS": {
"name": "Amerikaans-Samoa"
},
"VI": {
"name": "Amerikaanse Maagde-eilande"
},
"AD": {
"name": "Andorra"
},
"AO": {
"name": "Angola"
},
"AI": {
"name": "Anguilla"
},
"AQ": {
"name": "Antarktika"
},
"AG": {
"name": "Antigua en Barbuda"
},
"AR": {
"name": "Argentinië"
},
"AM": {
"name": "Armenië"
},
"AW": {
"name": "Aruba"
},
"AC": {
"name": "Ascensioneiland"
},
"AU": {
"name": "Australië"
},
"AZ": {
"name": "Azerbeidjan"
},
"BS": {
"name": "Bahamas"
},
"BH": {
"name": "Bahrein"
},
"BD": {
"name": "Bangladesj"
},
"BB": {
"name": "Barbados"
},
"BY": {
"name": "Belarus"
},
"BE": {
"name": "België"
},
"BZ": {
"name": "Belize"
},
"BJ": {
"name": "Benin"
},
"BM": {
"name": "Bermuda"
},
"BT": {
"name": "Bhoetan"
},
"BO": {
"name": "Bolivië"
},
"BA": {
"name": "Bosnië en Herzegowina"
},
"BW": {
"name": "Botswana"
},
"BR": {
"name": "Brasilië"
},
"IO": {
"name": "Britse Indiese Oseaangebied"
},
"VG": {
"name": "Britse Maagde-eilande"
},
"BN": {
"name": "Broenei"
},
"BG": {
"name": "Bulgarye"
},
"BF": {
"name": "Burkina Faso"
},
"BI": {
"name": "Burundi"
},
"EA": {
"name": "Ceuta en Melilla"
},
"CL": {
"name": "Chili"
},
"CC": {
"name": "Cocos- (Keeling) eilande"
},
"CO": {
"name": "Colombië"
},
"KM": {
"name": "Comore"
},
"CK": {
"name": "Cookeilande"
},
"CR": {
"name": "Costa Rica"
},
"CW": {
"name": "Curaçao"
},
"CD": {
"name": "Demokratiese Republiek van die Kongo"
},
"DK": {
"name": "Denemarke"
},
"DG": {
"name": "Diego Garcia"
},
"DJ": {
"name": "Djiboeti"
},
"DM": {
"name": "Dominica"
},
"DO": {
"name": "Dominikaanse Republiek"
},
"DE": {
"name": "Duitsland"
},
"EC": {
"name": "Ecuador"
},
"EG": {
"name": "Egipte"
},
"IM": {
"name": "Eiland Man"
},
"GQ": {
"name": "Ekwatoriaal-Guinee"
},
"SV": {
"name": "El Salvador"
},
"ER": {
"name": "Eritrea"
},
"EE": {
"name": "Estland"
},
"ET": {
"name": "Ethiopië"
},
"FK": {
"name": "Falklandeilande"
},
"FO": {
"name": "Faroëreilande"
},
"FJ": {
"name": "Fidji"
},
"PH": {
"name": "Filippyne"
},
"FI": {
"name": "Finland"
},
"FR": {
"name": "Frankryk"
},
"GF": {
"name": "Frans-Guyana"
},
"PF": {
"name": "Frans-Polinesië"
},
"TF": {
"name": "Franse Suidelike Gebiede"
},
"GA": {
"name": "Gaboen"
},
"GM": {
"name": "Gambië"
},
"GE": {
"name": "Georgië"
},
"GH": {
"name": "Ghana"
},
"GI": {
"name": "Gibraltar"
},
"GD": {
"name": "Grenada"
},
"GR": {
"name": "Griekeland"
},
"GL": {
"name": "Groenland"
},
"GP": {
"name": "Guadeloupe"
},
"GU": {
"name": "Guam"
},
"GT": {
"name": "Guatemala"
},
"GG": {
"name": "Guernsey"
},
"GN": {
"name": "Guinee"
},
"GW": {
"name": "Guinee-Bissau"
},
"GY": {
"name": "Guyana"
},
"HT": {
"name": "Haïti"
},
"HN": {
"name": "Honduras"
},
"HU": {
"name": "Hongarye"
},
"HK": {
"name": "Hongkong SAS Sjina"
},
"IE": {
"name": "Ierland"
},
"IN": {
"name": "Indië"
},
"ID": {
"name": "Indonesië"
},
"IQ": {
"name": "Irak"
},
"IR": {
"name": "Iran"
},
"IL": {
"name": "Israel"
},
"IT": {
"name": "Italië"
},
"CI": {
"name": "Ivoorkus"
},
"JM": {
"name": "Jamaika"
},
"JP": {
"name": "Japan"
},
"YE": {
"name": "Jemen"
},
"JE": {
"name": "Jersey"
},
"JO": {
"name": "Jordanië"
},
"KY": {
"name": "Kaaimanseilande"
},
"CV": {
"name": "Kaap Verde"
},
"KH": {
"name": "Kambodja"
},
"CM": {
"name": "Kameroen"
},
"CA": {
"name": "Kanada"
},
"IC": {
"name": "Kanariese Eilande"
},
"BQ": {
"name": "Karibiese Nederland"
},
"QA": {
"name": "Katar"
},
"KZ": {
"name": "Kazakstan"