<?php
/**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
'et' => 'Eesti',
'fi-fi' => 'Suomi',
'fr' => 'Français',
+ 'gd' => 'Gàidhlig',
'hu' => 'Magyar',
'is' => 'Íslenska',
'it' => 'Italiano',
* @var Database
*/
private $dba;
+ /**
+ * @var IManageConfigValues
+ */
+ private $config;
public function __construct(IManageConfigValues $config, Database $dba, IHandleSessions $session, array $server, array $get)
{
$this->dba = $dba;
+ $this->config = $config;
$this->loadTranslationTable(L10n::detectLanguage($server, $get, $config->get('system', 'language', self::DEFAULT)));
$this->setSessionVariable($session);
$a->strings = [];
// load enabled addons strings
- $addons = $this->dba->select('addon', ['name'], ['installed' => true]);
- while ($p = $this->dba->fetch($addons)) {
- $name = Strings::sanitizeFilePathItem($p['name']);
+ $addons = array_keys($this->config->get('addons') ?? []);
+ foreach ($addons as $addon) {
+ $name = Strings::sanitizeFilePathItem($addon);
if (file_exists(__DIR__ . "/../../addon/$name/lang/$lang/strings.php")) {
include __DIR__ . "/../../addon/$name/lang/$lang/strings.php";
}
{
$lang_variable = $server['HTTP_ACCEPT_LANGUAGE'] ?? null;
- $acceptedLanguages = preg_split('/,\s*/', $lang_variable);
-
- if (empty($acceptedLanguages)) {
+ if (empty($lang_variable)) {
$acceptedLanguages = [];
+ } else {
+ $acceptedLanguages = preg_split('/,\s*/', $lang_variable);
}
// Add get as absolute quality accepted language (except this language isn't valid)
// for some languages there is only a single array item
$s = $t[0];
}
- // if $t is empty, skip it, because empty strings array are indended
+ // if $t is empty, skip it, because empty strings array are intended
// to make string file smaller when there's no translation
} else {
$s = $t;
*
* @return array
*/
- public static function getAvailableLanguages(): array
+ public function getAvailableLanguages(): array
{
$langs = [];
$strings_file_paths = glob('view/lang/*/strings.php');
return $langs;
}
+ /**
+ * Get language codes that are detectable by our language detection routines.
+ * Languages are excluded that aren't used often and that tend to false detections.
+ * The listed codes are a collection of both the official ISO 639-1 codes and
+ * the codes that are used by our built-in language detection routine.
+ * When the detection is done, the result only consists of the official ISO 639-1 codes.
+ *
+ * @return array
+ */
+ public function getDetectableLanguages(): array
+ {
+ $additional_langs = [
+ 'af', 'az', 'az-Cyrl', 'az-Latn', 'be', 'bn', 'bs', 'bs-Cyrl', 'bs-Latn',
+ 'cy', 'da', 'el', 'el-monoton', 'el-polyton', 'en', 'eu', 'fa', 'fi',
+ 'ga', 'gl', 'gu', 'he', 'hi', 'hr', 'hy', 'id', 'in', 'iu', 'iw', 'jv', 'jw',
+ 'ka', 'km', 'ko', 'lt', 'lv', 'mo', 'ms', 'ms-Arab', 'ms-Latn', 'nb', 'nn', 'no',
+ 'pt', 'pt-PT', 'pt-BR', 'ro', 'sa', 'sk', 'sl', 'sq', 'sr', 'sr-Cyrl', 'sr-Latn', 'sw',
+ 'ta', 'th', 'tl', 'tr', 'ug', 'uk', 'uz', 'vi', 'zh', 'zh-Hant', 'zh-Hans',
+ ];
+
+ if (in_array('cld2', get_loaded_extensions())) {
+ $additional_langs = array_merge($additional_langs,
+ ['dv', 'kn', 'lo', 'ml', 'or', 'pa', 'sd', 'si', 'te', 'yi']);
+ }
+
+ $langs = array_merge($additional_langs, array_keys($this->getAvailableLanguages()));
+ sort($langs);
+ return $langs;
+ }
+
+ /**
+ * Return a list of supported languages with their two byte language codes.
+ *
+ * @param bool $international If set to true, additionally the international language name is returned as well.
+ * @return array
+ */
+ public function getLanguageCodes(bool $international = false): array
+ {
+ $iso639 = new \Matriphe\ISO639\ISO639;
+
+ $languages = [];
+
+ foreach ($this->getDetectableLanguages() as $code) {
+ $code = $this->toISO6391($code);
+ $native = $iso639->nativeByCode1($code);
+ $language = $iso639->languageByCode1($code);
+ if ($native != $language && $international) {
+ $languages[$code] = $this->t('%s (%s)', $native, $language);
+ } else {
+ $languages[$code] = $native;
+ }
+ }
+
+ return $languages;
+ }
+
+ /**
+ * Convert the language code to ISO639-1
+ * It also converts old codes to their new counterparts.
+ *
+ * @param string $code
+ * @return string
+ */
+ public function toISO6391(string $code): string
+ {
+ if ((strlen($code) > 2) && (substr($code, 2, 1) == '-')) {
+ $code = substr($code, 0, 2);
+ }
+ if (in_array($code, ['nb', 'nn'])) {
+ $code = 'no';
+ }
+ if ($code == 'in') {
+ $code = 'id';
+ }
+ if ($code == 'iw') {
+ $code = 'he';
+ }
+ if ($code == 'jw') {
+ $code = 'jv';
+ }
+ if ($code == 'mo') {
+ $code = 'ro';
+ }
+ return $code;
+ }
+
/**
* Translate days and months names.
*
return $ret;
}
- /**
- * Load poke verbs
- *
- * @return array index is present tense verb
- * value is array containing past tense verb, translation of present, translation of past
- * @throws \Friendica\Network\HTTPException\InternalServerErrorException
- * @hook poke_verbs pokes array
- */
- public function getPokeVerbs(): array
- {
- // index is present tense verb
- // value is array containing past tense verb, translation of present, translation of past
- $arr = [
- 'poke' => ['poked', $this->t('poke'), $this->t('poked')],
- 'ping' => ['pinged', $this->t('ping'), $this->t('pinged')],
- 'prod' => ['prodded', $this->t('prod'), $this->t('prodded')],
- 'slap' => ['slapped', $this->t('slap'), $this->t('slapped')],
- 'finger' => ['fingered', $this->t('finger'), $this->t('fingered')],
- 'rebuff' => ['rebuffed', $this->t('rebuff'), $this->t('rebuffed')],
- ];
-
- Hook::callAll('poke_verbs', $arr);
-
- return $arr;
- }
-
/**
* Creates a new L10n instance based on the given langauge
*