From 66832c41e9fff481c20ca219b3cc0a4e53b8b551 Mon Sep 17 00:00:00 2001 From: Klaus Weidenbach Date: Wed, 25 Oct 2017 23:21:07 +0200 Subject: :arrow_up: Update intl library. Update intl library from v0.4? (2014) to v0.7.4 (2016). Use global composer autoloader now. --- .../intl/scripts/currency/generate.php | 192 +++++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 vendor/commerceguys/intl/scripts/currency/generate.php (limited to 'vendor/commerceguys/intl/scripts/currency/generate.php') diff --git a/vendor/commerceguys/intl/scripts/currency/generate.php b/vendor/commerceguys/intl/scripts/currency/generate.php new file mode 100644 index 000000000..63f0ef302 --- /dev/null +++ b/vendor/commerceguys/intl/scripts/currency/generate.php @@ -0,0 +1,192 @@ +CcyTbl->CcyNtry as $currency) { + $attributes = (array) $currency->CcyNm->attributes(); + if (!empty($attributes) && !empty($attributes['@attributes']['IsFund'])) { + // Ignore funds. + continue; + } + $currency = (array) $currency; + if (empty($currency['Ccy'])) { + // Ignore placeholders like "Antarctica". + continue; + } + if (substr($currency['CtryNm'], 0, 2) == 'ZZ' || in_array($currency['Ccy'], ['XUA', 'XSU', 'XDR'])) { + // Ignore special currencies. + continue; + } + + $currencyCode = $currency['Ccy']; + $baseData[$currencyCode] = [ + 'numeric_code' => $currency['CcyNbr'], + ]; + // Take the fraction digits from CLDR, not ISO, because it reflects real + // life usage more closely. If the digits aren't set, that means that the + // default value (2) should be used. + if (isset($currencyData[$currencyCode]['_digits'])) { + $fractionDigits = $currencyData[$currencyCode]['_digits']; + if ($fractionDigits != 2) { + $baseData[$currencyCode]['fraction_digits'] = $fractionDigits; + } + } +} + +// Write out base.json. +ksort($baseData); +file_put_json('base.json', $baseData); + +// Gather available locales. +$locales = []; +if ($handle = opendir($localeDirectory)) { + while (false !== ($entry = readdir($handle))) { + if (substr($entry, 0, 1) != '.') { + $entryParts = explode('-', $entry); + if (!in_array($entry, $ignoredLocales) && !in_array($entryParts[0], $ignoredLocales)) { + $locales[] = $entry; + } + } + } + closedir($handle); +} + +// Make sure 'en' is processed first so that it can be used as a fallback. +$index = array_search('en', $locales); +unset($locales[$index]); +array_unshift($locales, 'en'); + +// Create the localizations. +$currencies = []; +$untranslatedCounts = []; +foreach ($locales as $locale) { + $data = json_decode(file_get_contents($numbersDirectory . $locale . '/currencies.json'), true); + $data = $data['main'][$locale]['numbers']['currencies']; + foreach ($data as $currencyCode => $currency) { + if (isset($baseData[$currencyCode])) { + $currencyName = $currency['displayName']; + // This currency name is untranslated, use the english version. + if ($currencyCode == $currencyName) { + $currencyName = $currencies['en'][$currencyCode]['name']; + // Maintain a count of untranslated currencies per locale. + $untranslatedCounts += [$locale => 0]; + $untranslatedCounts[$locale]++; + } + + $currencies[$locale][$currencyCode] = [ + 'name' => $currencyName, + ]; + // Decrease the dataset size by exporting the symbol only if it's + // different from the currency code. + if ($currency['symbol'] != $currencyCode) { + $currencies[$locale][$currencyCode]['symbol'] = $currency['symbol']; + } + } + } +} + +// Ignore locales that are more than 80% untranslated. +foreach ($untranslatedCounts as $locale => $count) { + $totalCount = count($currencies[$locale]); + $untranslatedPercentage = $count * (100 / $totalCount); + if ($untranslatedPercentage >= 80) { + unset($currencies[$locale]); + } +} + +// Identify localizations that are the same as the ones for the parent locale. +// For example, "fr-FR" if "fr" has the same data. +$duplicates = []; +foreach ($currencies as $locale => $localizedCurrencies) { + if (strpos($locale, '-') !== false) { + $localeParts = explode('-', $locale); + array_pop($localeParts); + $parentLocale = implode('-', $localeParts); + $diff = array_udiff($localizedCurrencies, $currencies[$parentLocale], function ($first, $second) { + return ($first['name'] == $second['name']) ? 0 : 1; + }); + + if (empty($diff)) { + // The duplicates are not removed right away because they might + // still be needed for other duplicate checks (for example, + // when there are locales like bs-Latn-BA, bs-Latn, bs). + $duplicates[] = $locale; + } + } +} +// Remove the duplicates. +foreach ($duplicates as $locale) { + unset($currencies[$locale]); +} + +// Write out the localizations. +foreach ($currencies as $locale => $localizedCurrencies) { + $collator = collator_create($locale); + uasort($localizedCurrencies, function ($a, $b) use ($collator) { + return collator_compare($collator, $a['name'], $b['name']); + }); + file_put_json($locale . '.json', $localizedCurrencies); +} + +/** + * Converts the provided data into json and writes it to the disk. + */ +function file_put_json($filename, $data) +{ + $data = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); + // Indenting with tabs instead of 4 spaces gives us 20% smaller files. + $data = str_replace(' ', "\t", $data); + file_put_contents($filename, $data); +} -- cgit v1.2.3