New function to change language codes for the library

This commit is contained in:
Michael 2023-09-03 16:42:17 +00:00
parent 7220027332
commit c5ef0cfe20
4 changed files with 40 additions and 11 deletions

View file

@ -378,7 +378,7 @@ class L10n
*
* @return array
*/
public static function getAvailableLanguages(): array
public function getAvailableLanguages(bool $additional = false): array
{
$langs = [];
$strings_file_paths = glob('view/lang/*/strings.php');
@ -392,10 +392,45 @@ class L10n
$path_array = explode('/', $strings_file_path);
$langs[$path_array[2]] = self::LANG_NAMES[$path_array[2]] ?? $path_array[2];
}
if ($additional) {
// See https://github.com/friendica/friendica/issues/10511
// Persian is manually added to language detection until a persian translation is provided for the interface, at
// which point it will be automatically available through `getAvailableLanguages()` and this should be removed.
// Additionally Portuguese, Ukrainian and Welsh are added to that list.
$langs = array_merge(['cy' => 'Cymraeg', 'uk' => 'Українська', 'pt-PT' => 'Português', 'fa' => 'فارسی'], $langs);
ksort($langs);
}
}
return $langs;
}
/**
* The language detection routine uses some slightly different language codes.
* This function changes the language language codes accordingly.
*
* @param array $languages
* @return array
*/
public function convertForLanguageDetection(array $languages): array
{
$languages['fi'] = $languages['fi-fi'];
unset($languages['fi-fi']);
$languages['da'] = $languages['da-dk'];
unset($languages['da-dk']);
$languages['nb'] = $languages['nb-no'];
unset($languages['nb-no']);
$languages['pt-BR'] = $languages['pt-br'];
unset($languages['pt-br']);
$languages['zh-Hans'] = $languages['zh-cn'];
$languages['zh-Hant'] = $languages['zh-cn'];
unset($languages['zh-cn']);
ksort($languages);
return $languages;
}
/**
* Translate days and months names.
*

View file

@ -2015,11 +2015,8 @@ class Item
$naked_body = self::getDominantLanguage($naked_body);
$availableLanguages = DI::l10n()->getAvailableLanguages();
// See https://github.com/friendica/friendica/issues/10511
// Persian is manually added to language detection until a persian translation is provided for the interface, at
// which point it will be automatically available through `getAvailableLanguages()` and this should be removed.
$availableLanguages['fa'] = 'fa';
$availableLanguages = DI::l10n()->getAvailableLanguages(true);
$availableLanguages = DI::l10n()->convertForLanguageDetection($availableLanguages);
$ld = new Language(array_keys($availableLanguages));
return $ld->detect($naked_body)->limit(0, $count)->close() ?: [];

View file

@ -543,15 +543,12 @@ class User
public static function getLanguageCode(int $uid, bool $short): string
{
$owner = self::getOwnerDataById($uid);
$languages = DI::l10n()->getAvailableLanguages();
$languages = DI::l10n()->getAvailableLanguages($short);
if (in_array($owner['language'], array_keys($languages))) {
$language = $owner['language'];
} else {
$language = DI::config()->get('system', 'language');
}
if ($short) {
return substr($language, 0, 2);
}
return $language;
}

View file

@ -219,7 +219,7 @@ class Display extends BaseSettings
];
$channel_languages = $this->pConfig->get($uid, 'channel', 'languages', [User::getLanguageCode($uid, false)]);
$languages = $this->l10n->getAvailableLanguages();
$languages = $this->l10n->getAvailableLanguages(true);
$first_day_of_week = $this->pConfig->get($uid, 'calendar', 'first_day_of_week', 0);
$weekdays = [