From 9cab8ae58a29ecf7387e6865aa170715caeabf04 Mon Sep 17 00:00:00 2001 From: Stefan Parviainen Date: Tue, 30 Dec 2014 19:57:12 +0100 Subject: Language names via intl library. Fixes #773 --- include/language.php | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'include/language.php') diff --git a/include/language.php b/include/language.php index 9db57dceb..0db5ec86e 100644 --- a/include/language.php +++ b/include/language.php @@ -259,11 +259,25 @@ 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) { - if($l === null) - $l = $s; + // 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)); - logger('get_language_name: for ' . $s . ' in ' . $l . ' returns: ' . Locale::getDisplayLanguage($s, $l), LOGGER_DEBUG); - return Locale::getDisplayLanguage($s, $l); + $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); + $language = $languageRepository->get($s, $l); + } + + return $language->getName(); } -- cgit v1.2.3 From b54bbf0fb8af72bd5273597f564f084aa1c1ac79 Mon Sep 17 00:00:00 2001 From: Stefan Parviainen Date: Tue, 30 Dec 2014 20:34:26 +0100 Subject: Update function doc, return language code if language is not recognized --- include/language.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'include/language.php') diff --git a/include/language.php b/include/language.php index 0db5ec86e..f5ee3471e 100644 --- a/include/language.php +++ b/include/language.php @@ -250,8 +250,7 @@ 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 can be in any format that PHP's Locale understands. We will mostly - * use the 2-letter ISO 639-1 (en, de, fr) format. + * $s and $l should be in 2-letter ISO 639-1 format * * If nothing could be looked up it returns $s. * @@ -275,7 +274,12 @@ function get_language_name($s, $l = null) { catch(CommerceGuys\Intl\Exception\UnknownLanguageException $e) { $s = substr($s,0,2); if($l !== null) $l = substr($s,0,2); - $language = $languageRepository->get($s, $l); + try { + $language = $languageRepository->get($s, $l); + } + catch(CommerceGuys\Intl\Exception\UnknownLanguageException $e) { + return $s; // Give up + } } return $language->getName(); -- cgit v1.2.3 From 4f35efa0bad4ae6489b63f3eebafe6542d654094 Mon Sep 17 00:00:00 2001 From: RedMatrix Date: Wed, 31 Dec 2014 10:43:19 +1100 Subject: Revert "Language names via intl library." --- include/language.php | 30 ++++++------------------------ 1 file changed, 6 insertions(+), 24 deletions(-) (limited to 'include/language.php') diff --git a/include/language.php b/include/language.php index f5ee3471e..9db57dceb 100644 --- a/include/language.php +++ b/include/language.php @@ -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); } -- cgit v1.2.3 From 7bf7f8180dbc3a89824815de3bc1e4f40857d2f6 Mon Sep 17 00:00:00 2001 From: Stefan Parviainen Date: Wed, 31 Dec 2014 10:42:08 +0100 Subject: Revert "Revert "Language names via intl library."" This reverts commit 4f35efa0bad4ae6489b63f3eebafe6542d654094. --- include/language.php | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) (limited to 'include/language.php') diff --git a/include/language.php b/include/language.php index 9db57dceb..f5ee3471e 100644 --- a/include/language.php +++ b/include/language.php @@ -250,8 +250,7 @@ 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 can be in any format that PHP's Locale understands. We will mostly - * use the 2-letter ISO 639-1 (en, de, fr) format. + * $s and $l should be in 2-letter ISO 639-1 format * * If nothing could be looked up it returns $s. * @@ -259,11 +258,30 @@ 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) { - if($l === null) - $l = $s; + // 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)); - logger('get_language_name: for ' . $s . ' in ' . $l . ' returns: ' . Locale::getDisplayLanguage($s, $l), LOGGER_DEBUG); - return Locale::getDisplayLanguage($s, $l); + $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(); } -- cgit v1.2.3