New functions for the language library

This commit is contained in:
Michael 2023-09-03 17:44:44 +00:00
parent 64c8c2afdf
commit 595508a91f
4 changed files with 41 additions and 14 deletions

View file

@ -397,8 +397,8 @@ class L10n
// 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);
// Additionally Portuguese, Ukrainian, traditional Chinese and Welsh are added to that list.
$langs = array_merge(['cy' => 'Cymraeg', 'uk' => 'Українська', 'pt-PT' => 'Português', 'zh-hant' => '繁體', 'fa' => 'فارسی'], $langs);
ksort($langs);
}
}
@ -407,7 +407,7 @@ class L10n
/**
* The language detection routine uses some slightly different language codes.
* This function changes the language language codes accordingly.
* This function changes the language array accordingly.
*
* @param array $languages
* @return array
@ -423,7 +423,6 @@ class L10n
$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);
@ -431,6 +430,34 @@ class L10n
return $languages;
}
/**
* The language detection routine uses some slightly different language codes.
* This function changes the language codes accordingly.
*
* @param string $language
* @return string
*/
public function convertCodeForLanguageDetection(string $language): string
{
switch ($language) {
case 'da-dk':
return 'da';
case 'en-us':
case 'en-gb':
return 'en';
case 'fi-fi':
return 'fi';
case 'nb-no':
return 'nb';
case 'pt-br':
return 'pt-BR';
case 'zh-cn':
return 'zh-Hans';
default:
return $language;
}
}
/**
* Translate days and months names.
*

View file

@ -536,14 +536,13 @@ class User
/**
* Fetch the language code from the given user. If the code is invalid, return the system language
*
* @param integer $uid User-Id
* @param boolean $short If true, return the short form g.g. "en", otherwise the long form e.g. "en-gb"
* @param integer $uid User-Id
* @return string
*/
public static function getLanguageCode(int $uid, bool $short): string
public static function getLanguageCode(int $uid): string
{
$owner = self::getOwnerDataById($uid);
$languages = DI::l10n()->getAvailableLanguages($short);
$languages = DI::l10n()->getAvailableLanguages(true);
if (in_array($owner['language'], array_keys($languages))) {
$language = $owner['language'];
} else {

View file

@ -144,8 +144,8 @@ class Channel extends BaseModule
'accesskey' => 'h'
];
$language = User::getLanguageCode($this->session->getLocalUserId(), false);
$languages = $this->l10n->getAvailableLanguages();
$language = User::getLanguageCode($this->session->getLocalUserId());
$languages = $this->l10n->getAvailableLanguages(true);
$tabs[] = [
'label' => $languages[$language],
@ -344,7 +344,7 @@ class Channel extends BaseModule
} elseif (self::$content == self::AUDIO) {
$condition = ["`media-type` & ?", 4];
} elseif (self::$content == self::LANGUAGE) {
$condition = ["JSON_EXTRACT(JSON_KEYS(language), '$[0]') = ?", User::getLanguageCode($this->session->getLocalUserId(), true)];
$condition = ["JSON_EXTRACT(JSON_KEYS(language), '$[0]') = ?", $this->l10n->convertCodeForLanguageDetection(User::getLanguageCode($this->session->getLocalUserId()))];
}
if (self::$content != self::LANGUAGE) {
@ -404,10 +404,11 @@ class Channel extends BaseModule
private function addLanguageCondition(array $condition): array
{
$conditions = [];
$languages = $this->pConfig->get($this->session->getLocalUserId(), 'channel', 'languages', [User::getLanguageCode($this->session->getLocalUserId(), false)]);
$languages = $this->pConfig->get($this->session->getLocalUserId(), 'channel', 'languages', [User::getLanguageCode($this->session->getLocalUserId())]);
$languages = $this->l10n->convertForLanguageDetection($languages);
foreach ($languages as $language) {
$conditions[] = "JSON_EXTRACT(JSON_KEYS(language), '$[0]') = ?";
$condition[] = substr($language, 0, 2);
$condition[] = $language;
}
if (!empty($conditions)) {
$condition[0] .= " AND (`language` IS NULL OR " . implode(' OR ', $conditions) . ")";

View file

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