aboutsummaryrefslogtreecommitdiffstats
path: root/include/language.php
diff options
context:
space:
mode:
authorStefan Parviainen <saparvia@caterva.eu>2014-12-30 19:57:12 +0100
committerStefan Parviainen <saparvia@caterva.eu>2014-12-30 20:29:31 +0100
commit9cab8ae58a29ecf7387e6865aa170715caeabf04 (patch)
tree97c4791763ecb7c877c13b562a0ad3b80857b9d7 /include/language.php
parent8e034a3b6b67a9aaa20fe9db671350e198fe7c42 (diff)
downloadvolse-hubzilla-9cab8ae58a29ecf7387e6865aa170715caeabf04.tar.gz
volse-hubzilla-9cab8ae58a29ecf7387e6865aa170715caeabf04.tar.bz2
volse-hubzilla-9cab8ae58a29ecf7387e6865aa170715caeabf04.zip
Language names via intl library. Fixes #773
Diffstat (limited to 'include/language.php')
-rw-r--r--include/language.php22
1 files changed, 18 insertions, 4 deletions
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();
}