Merge pull request #13394 from annando/language

New functions for the language library
This commit is contained in:
Hypolite Petovan 2023-09-04 07:01:52 -04:00 committed by GitHub
commit bcfe5fee2d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 82 additions and 20 deletions

View file

@ -378,7 +378,7 @@ class L10n
* *
* @return array * @return array
*/ */
public static function getAvailableLanguages(): array public function getAvailableLanguages(bool $additional = false): array
{ {
$langs = []; $langs = [];
$strings_file_paths = glob('view/lang/*/strings.php'); $strings_file_paths = glob('view/lang/*/strings.php');
@ -392,10 +392,78 @@ class L10n
$path_array = explode('/', $strings_file_path); $path_array = explode('/', $strings_file_path);
$langs[$path_array[2]] = self::LANG_NAMES[$path_array[2]] ?? $path_array[2]; $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, traditional Chinese and Welsh are added to that list.
$additional_langs = [
'cy' => 'Cymraeg',
'uk' => 'Українська',
'pt-PT' => 'Português',
'zh-hant' => '繁體',
'fa' => 'فارسی'
];
$langs = array_merge($additional_langs, $langs);
ksort($langs);
}
} }
return $langs; return $langs;
} }
/**
* The language detection routine uses some slightly different language codes.
* This function changes the language array accordingly.
*
* @param array $languages
* @return array
*/
public function convertForLanguageDetection(array $languages): array
{
foreach ($languages as $key => $language) {
$newkey = $this->convertCodeForLanguageDetection($key);
if ($newkey != $key) {
if (!isset($languages[$newkey])) {
$languages[$newkey] = $language;
}
unset($languages[$key]);
}
}
ksort($languages);
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. * Translate days and months names.
* *

View file

@ -2015,11 +2015,8 @@ class Item
$naked_body = self::getDominantLanguage($naked_body); $naked_body = self::getDominantLanguage($naked_body);
$availableLanguages = DI::l10n()->getAvailableLanguages(); $availableLanguages = DI::l10n()->getAvailableLanguages(true);
// See https://github.com/friendica/friendica/issues/10511 $availableLanguages = DI::l10n()->convertForLanguageDetection($availableLanguages);
// 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';
$ld = new Language(array_keys($availableLanguages)); $ld = new Language(array_keys($availableLanguages));
return $ld->detect($naked_body)->limit(0, $count)->close() ?: []; return $ld->detect($naked_body)->limit(0, $count)->close() ?: [];

View file

@ -536,22 +536,18 @@ class User
/** /**
* Fetch the language code from the given user. If the code is invalid, return the system language * Fetch the language code from the given user. If the code is invalid, return the system language
* *
* @param integer $uid User-Id * @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"
* @return string * @return string
*/ */
public static function getLanguageCode(int $uid, bool $short): string public static function getLanguageCode(int $uid): string
{ {
$owner = self::getOwnerDataById($uid); $owner = self::getOwnerDataById($uid);
$languages = DI::l10n()->getAvailableLanguages(); $languages = DI::l10n()->getAvailableLanguages(true);
if (in_array($owner['language'], array_keys($languages))) { if (in_array($owner['language'], array_keys($languages))) {
$language = $owner['language']; $language = $owner['language'];
} else { } else {
$language = DI::config()->get('system', 'language'); $language = DI::config()->get('system', 'language');
} }
if ($short) {
return substr($language, 0, 2);
}
return $language; return $language;
} }

View file

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

View file

@ -218,8 +218,8 @@ class Display extends BaseSettings
BBCode::PREVIEW_LARGE => $this->t('Large Image'), 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(); $languages = $this->l10n->getAvailableLanguages(true);
$first_day_of_week = $this->pConfig->get($uid, 'calendar', 'first_day_of_week', 0); $first_day_of_week = $this->pConfig->get($uid, 'calendar', 'first_day_of_week', 0);
$weekdays = [ $weekdays = [