Factory.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OC\L10N;
  9. use OCP\App\AppPathNotFoundException;
  10. use OCP\App\IAppManager;
  11. use OCP\ICache;
  12. use OCP\ICacheFactory;
  13. use OCP\IConfig;
  14. use OCP\IRequest;
  15. use OCP\IUser;
  16. use OCP\IUserSession;
  17. use OCP\L10N\IFactory;
  18. use OCP\L10N\ILanguageIterator;
  19. use function is_null;
  20. /**
  21. * A factory that generates language instances
  22. */
  23. class Factory implements IFactory {
  24. /** @var string */
  25. protected $requestLanguage = '';
  26. /**
  27. * cached instances
  28. * @var array Structure: Lang => App => \OCP\IL10N
  29. */
  30. protected $instances = [];
  31. /**
  32. * @var array Structure: App => string[]
  33. */
  34. protected $availableLanguages = [];
  35. /**
  36. * @var array
  37. */
  38. protected $localeCache = [];
  39. /**
  40. * @var array
  41. */
  42. protected $availableLocales = [];
  43. /**
  44. * @var array Structure: string => callable
  45. */
  46. protected $pluralFunctions = [];
  47. public const COMMON_LANGUAGE_CODES = [
  48. 'en', 'es', 'fr', 'de', 'de_DE', 'ja', 'ar', 'ru', 'nl', 'it',
  49. 'pt_BR', 'pt_PT', 'da', 'fi_FI', 'nb_NO', 'sv', 'tr', 'zh_CN', 'ko'
  50. ];
  51. /**
  52. * Keep in sync with `build/translation-checker.php`
  53. */
  54. public const RTL_LANGUAGES = [
  55. 'ar', // Arabic
  56. 'fa', // Persian
  57. 'he', // Hebrew
  58. 'ps', // Pashto,
  59. 'ug', // 'Uyghurche / Uyghur
  60. 'ur_PK', // Urdu
  61. ];
  62. private ICache $cache;
  63. public function __construct(
  64. protected IConfig $config,
  65. protected IRequest $request,
  66. protected IUserSession $userSession,
  67. ICacheFactory $cacheFactory,
  68. protected string $serverRoot,
  69. protected IAppManager $appManager,
  70. ) {
  71. $this->cache = $cacheFactory->createLocal('L10NFactory');
  72. }
  73. /**
  74. * Get a language instance
  75. *
  76. * @param string $app
  77. * @param string|null $lang
  78. * @param string|null $locale
  79. * @return \OCP\IL10N
  80. */
  81. public function get($app, $lang = null, $locale = null) {
  82. return new LazyL10N(function () use ($app, $lang, $locale) {
  83. $app = $this->appManager->cleanAppId($app);
  84. if ($lang !== null) {
  85. $lang = str_replace(['\0', '/', '\\', '..'], '', $lang);
  86. }
  87. $forceLang = $this->config->getSystemValue('force_language', false);
  88. if (is_string($forceLang)) {
  89. $lang = $forceLang;
  90. }
  91. $forceLocale = $this->config->getSystemValue('force_locale', false);
  92. if (is_string($forceLocale)) {
  93. $locale = $forceLocale;
  94. }
  95. if ($lang === null || !$this->languageExists($app, $lang)) {
  96. $lang = $this->findLanguage($app);
  97. }
  98. if ($locale === null || !$this->localeExists($locale)) {
  99. $locale = $this->findLocale($lang);
  100. }
  101. if (!isset($this->instances[$lang][$app])) {
  102. $this->instances[$lang][$app] = new L10N(
  103. $this,
  104. $app,
  105. $lang,
  106. $locale,
  107. $this->getL10nFilesForApp($app, $lang)
  108. );
  109. }
  110. return $this->instances[$lang][$app];
  111. });
  112. }
  113. /**
  114. * Find the best language
  115. *
  116. * @param string|null $appId App id or null for core
  117. *
  118. * @return string language If nothing works it returns 'en'
  119. */
  120. public function findLanguage(?string $appId = null): string {
  121. // Step 1: Forced language always has precedence over anything else
  122. $forceLang = $this->config->getSystemValue('force_language', false);
  123. if (is_string($forceLang)) {
  124. $this->requestLanguage = $forceLang;
  125. }
  126. // Step 2: Return cached language
  127. if ($this->requestLanguage !== '' && $this->languageExists($appId, $this->requestLanguage)) {
  128. return $this->requestLanguage;
  129. }
  130. /**
  131. * Step 3: At this point Nextcloud might not yet be installed and thus the lookup
  132. * in the preferences table might fail. For this reason we need to check
  133. * whether the instance has already been installed
  134. *
  135. * @link https://github.com/owncloud/core/issues/21955
  136. */
  137. if ($this->config->getSystemValueBool('installed', false)) {
  138. $userId = !is_null($this->userSession->getUser()) ? $this->userSession->getUser()->getUID() : null;
  139. if (!is_null($userId)) {
  140. $userLang = $this->config->getUserValue($userId, 'core', 'lang', null);
  141. } else {
  142. $userLang = null;
  143. }
  144. } else {
  145. $userId = null;
  146. $userLang = null;
  147. }
  148. if ($userLang) {
  149. $this->requestLanguage = $userLang;
  150. if ($this->languageExists($appId, $userLang)) {
  151. return $userLang;
  152. }
  153. }
  154. // Step 4: Check the request headers
  155. try {
  156. // Try to get the language from the Request
  157. $lang = $this->getLanguageFromRequest($appId);
  158. if ($userId !== null && $appId === null && !$userLang) {
  159. $this->config->setUserValue($userId, 'core', 'lang', $lang);
  160. }
  161. return $lang;
  162. } catch (LanguageNotFoundException $e) {
  163. // Finding language from request failed fall back to default language
  164. $defaultLanguage = $this->config->getSystemValue('default_language', false);
  165. if ($defaultLanguage !== false && $this->languageExists($appId, $defaultLanguage)) {
  166. return $defaultLanguage;
  167. }
  168. }
  169. // Step 5: fall back to English
  170. return 'en';
  171. }
  172. public function findGenericLanguage(?string $appId = null): string {
  173. // Step 1: Forced language always has precedence over anything else
  174. $forcedLanguage = $this->config->getSystemValue('force_language', false);
  175. if ($forcedLanguage !== false) {
  176. return $forcedLanguage;
  177. }
  178. // Step 2: Check if we have a default language
  179. $defaultLanguage = $this->config->getSystemValue('default_language', false);
  180. if ($defaultLanguage !== false && $this->languageExists($appId, $defaultLanguage)) {
  181. return $defaultLanguage;
  182. }
  183. // Step 3.1: Check if Nextcloud is already installed before we try to access user info
  184. if (!$this->config->getSystemValueBool('installed', false)) {
  185. return 'en';
  186. }
  187. // Step 3.2: Check the current user (if any) for their preferred language
  188. $user = $this->userSession->getUser();
  189. if ($user !== null) {
  190. $userLang = $this->config->getUserValue($user->getUID(), 'core', 'lang', null);
  191. if ($userLang !== null) {
  192. return $userLang;
  193. }
  194. }
  195. // Step 4: Check the request headers
  196. try {
  197. return $this->getLanguageFromRequest($appId);
  198. } catch (LanguageNotFoundException $e) {
  199. // Ignore and continue
  200. }
  201. // Step 5: fall back to English
  202. return 'en';
  203. }
  204. /**
  205. * find the best locale
  206. *
  207. * @param string $lang
  208. * @return null|string
  209. */
  210. public function findLocale($lang = null) {
  211. $forceLocale = $this->config->getSystemValue('force_locale', false);
  212. if (is_string($forceLocale) && $this->localeExists($forceLocale)) {
  213. return $forceLocale;
  214. }
  215. if ($this->config->getSystemValueBool('installed', false)) {
  216. $userId = $this->userSession->getUser() !== null ? $this->userSession->getUser()->getUID() : null;
  217. $userLocale = null;
  218. if ($userId !== null) {
  219. $userLocale = $this->config->getUserValue($userId, 'core', 'locale', null);
  220. }
  221. } else {
  222. $userId = null;
  223. $userLocale = null;
  224. }
  225. if ($userLocale && $this->localeExists($userLocale)) {
  226. return $userLocale;
  227. }
  228. // Default : use system default locale
  229. $defaultLocale = $this->config->getSystemValue('default_locale', false);
  230. if ($defaultLocale !== false && $this->localeExists($defaultLocale)) {
  231. return $defaultLocale;
  232. }
  233. // If no user locale set, use lang as locale
  234. if ($lang !== null && $this->localeExists($lang)) {
  235. return $lang;
  236. }
  237. // At last, return USA
  238. return 'en_US';
  239. }
  240. /**
  241. * find the matching lang from the locale
  242. *
  243. * @param string $app
  244. * @param string $locale
  245. * @return null|string
  246. */
  247. public function findLanguageFromLocale(string $app = 'core', ?string $locale = null) {
  248. if ($this->languageExists($app, $locale)) {
  249. return $locale;
  250. }
  251. // Try to split e.g: fr_FR => fr
  252. $locale = explode('_', $locale)[0];
  253. if ($this->languageExists($app, $locale)) {
  254. return $locale;
  255. }
  256. }
  257. /**
  258. * Find all available languages for an app
  259. *
  260. * @param string|null $app App id or null for core
  261. * @return string[] an array of available languages
  262. */
  263. public function findAvailableLanguages($app = null): array {
  264. $key = $app;
  265. if ($key === null) {
  266. $key = 'null';
  267. }
  268. if ($availableLanguages = $this->cache->get($key)) {
  269. $this->availableLanguages[$key] = $availableLanguages;
  270. }
  271. // also works with null as key
  272. if (!empty($this->availableLanguages[$key])) {
  273. return $this->availableLanguages[$key];
  274. }
  275. $available = ['en']; //english is always available
  276. $dir = $this->findL10nDir($app);
  277. if (is_dir($dir)) {
  278. $files = scandir($dir);
  279. if ($files !== false) {
  280. foreach ($files as $file) {
  281. if (str_ends_with($file, '.json') && !str_starts_with($file, 'l10n')) {
  282. $available[] = substr($file, 0, -5);
  283. }
  284. }
  285. }
  286. }
  287. // merge with translations from theme
  288. $theme = $this->config->getSystemValueString('theme');
  289. if (!empty($theme)) {
  290. $themeDir = $this->serverRoot . '/themes/' . $theme . substr($dir, strlen($this->serverRoot));
  291. if (is_dir($themeDir)) {
  292. $files = scandir($themeDir);
  293. if ($files !== false) {
  294. foreach ($files as $file) {
  295. if (str_ends_with($file, '.json') && !str_starts_with($file, 'l10n')) {
  296. $available[] = substr($file, 0, -5);
  297. }
  298. }
  299. }
  300. }
  301. }
  302. $this->availableLanguages[$key] = $available;
  303. $this->cache->set($key, $available, 60);
  304. return $available;
  305. }
  306. /**
  307. * @return array|mixed
  308. */
  309. public function findAvailableLocales() {
  310. if (!empty($this->availableLocales)) {
  311. return $this->availableLocales;
  312. }
  313. $localeData = file_get_contents(\OC::$SERVERROOT . '/resources/locales.json');
  314. $this->availableLocales = \json_decode($localeData, true);
  315. return $this->availableLocales;
  316. }
  317. /**
  318. * @param string|null $app App id or null for core
  319. * @param string $lang
  320. * @return bool
  321. */
  322. public function languageExists($app, $lang) {
  323. if ($lang === 'en') { //english is always available
  324. return true;
  325. }
  326. $languages = $this->findAvailableLanguages($app);
  327. return in_array($lang, $languages);
  328. }
  329. public function getLanguageDirection(string $language): string {
  330. if (in_array($language, self::RTL_LANGUAGES, true)) {
  331. return 'rtl';
  332. }
  333. return 'ltr';
  334. }
  335. public function getLanguageIterator(?IUser $user = null): ILanguageIterator {
  336. $user = $user ?? $this->userSession->getUser();
  337. if ($user === null) {
  338. throw new \RuntimeException('Failed to get an IUser instance');
  339. }
  340. return new LanguageIterator($user, $this->config);
  341. }
  342. /**
  343. * Return the language to use when sending something to a user
  344. *
  345. * @param IUser|null $user
  346. * @return string
  347. * @since 20.0.0
  348. */
  349. public function getUserLanguage(?IUser $user = null): string {
  350. $language = $this->config->getSystemValue('force_language', false);
  351. if ($language !== false) {
  352. return $language;
  353. }
  354. if ($user instanceof IUser) {
  355. $language = $this->config->getUserValue($user->getUID(), 'core', 'lang', null);
  356. if ($language !== null) {
  357. return $language;
  358. }
  359. // Use language from request
  360. if ($this->userSession->getUser() instanceof IUser &&
  361. $user->getUID() === $this->userSession->getUser()->getUID()) {
  362. try {
  363. return $this->getLanguageFromRequest();
  364. } catch (LanguageNotFoundException $e) {
  365. }
  366. }
  367. }
  368. return $this->config->getSystemValueString('default_language', 'en');
  369. }
  370. /**
  371. * @param string $locale
  372. * @return bool
  373. */
  374. public function localeExists($locale) {
  375. if ($locale === 'en') { //english is always available
  376. return true;
  377. }
  378. if ($this->localeCache === []) {
  379. $locales = $this->findAvailableLocales();
  380. foreach ($locales as $l) {
  381. $this->localeCache[$l['code']] = true;
  382. }
  383. }
  384. return isset($this->localeCache[$locale]);
  385. }
  386. /**
  387. * @throws LanguageNotFoundException
  388. */
  389. private function getLanguageFromRequest(?string $app = null): string {
  390. $header = $this->request->getHeader('ACCEPT_LANGUAGE');
  391. if ($header !== '') {
  392. $available = $this->findAvailableLanguages($app);
  393. // E.g. make sure that 'de' is before 'de_DE'.
  394. sort($available);
  395. $preferences = preg_split('/,\s*/', strtolower($header));
  396. foreach ($preferences as $preference) {
  397. [$preferred_language] = explode(';', $preference);
  398. $preferred_language = str_replace('-', '_', $preferred_language);
  399. $preferred_language_parts = explode('_', $preferred_language);
  400. foreach ($available as $available_language) {
  401. if ($preferred_language === strtolower($available_language)) {
  402. return $this->respectDefaultLanguage($app, $available_language);
  403. }
  404. if (strtolower($available_language) === $preferred_language_parts[0] . '_' . end($preferred_language_parts)) {
  405. return $available_language;
  406. }
  407. }
  408. // Fallback from de_De to de
  409. foreach ($available as $available_language) {
  410. if (substr($preferred_language, 0, 2) === $available_language) {
  411. return $available_language;
  412. }
  413. }
  414. }
  415. }
  416. throw new LanguageNotFoundException();
  417. }
  418. /**
  419. * if default language is set to de_DE (formal German) this should be
  420. * preferred to 'de' (non-formal German) if possible
  421. */
  422. protected function respectDefaultLanguage(?string $app, string $lang): string {
  423. $result = $lang;
  424. $defaultLanguage = $this->config->getSystemValue('default_language', false);
  425. // use formal version of german ("Sie" instead of "Du") if the default
  426. // language is set to 'de_DE' if possible
  427. if (
  428. is_string($defaultLanguage) &&
  429. strtolower($lang) === 'de' &&
  430. strtolower($defaultLanguage) === 'de_de' &&
  431. $this->languageExists($app, 'de_DE')
  432. ) {
  433. $result = 'de_DE';
  434. }
  435. return $result;
  436. }
  437. /**
  438. * Checks if $sub is a subdirectory of $parent
  439. *
  440. * @param string $sub
  441. * @param string $parent
  442. * @return bool
  443. */
  444. private function isSubDirectory($sub, $parent) {
  445. // Check whether $sub contains no ".."
  446. if (str_contains($sub, '..')) {
  447. return false;
  448. }
  449. // Check whether $sub is a subdirectory of $parent
  450. if (str_starts_with($sub, $parent)) {
  451. return true;
  452. }
  453. return false;
  454. }
  455. /**
  456. * Get a list of language files that should be loaded
  457. *
  458. * @return string[]
  459. */
  460. private function getL10nFilesForApp(string $app, string $lang): array {
  461. $languageFiles = [];
  462. $i18nDir = $this->findL10nDir($app);
  463. $transFile = strip_tags($i18nDir) . strip_tags($lang) . '.json';
  464. if (($this->isSubDirectory($transFile, $this->serverRoot . '/core/l10n/')
  465. || $this->isSubDirectory($transFile, $this->serverRoot . '/lib/l10n/')
  466. || $this->isSubDirectory($transFile, $this->appManager->getAppPath($app) . '/l10n/'))
  467. && file_exists($transFile)
  468. ) {
  469. // load the translations file
  470. $languageFiles[] = $transFile;
  471. }
  472. // merge with translations from theme
  473. $theme = $this->config->getSystemValueString('theme');
  474. if (!empty($theme)) {
  475. $transFile = $this->serverRoot . '/themes/' . $theme . substr($transFile, strlen($this->serverRoot));
  476. if (file_exists($transFile)) {
  477. $languageFiles[] = $transFile;
  478. }
  479. }
  480. return $languageFiles;
  481. }
  482. /**
  483. * find the l10n directory
  484. *
  485. * @param string $app App id or empty string for core
  486. * @return string directory
  487. */
  488. protected function findL10nDir($app = null) {
  489. if (in_array($app, ['core', 'lib'])) {
  490. if (file_exists($this->serverRoot . '/' . $app . '/l10n/')) {
  491. return $this->serverRoot . '/' . $app . '/l10n/';
  492. }
  493. } elseif ($app) {
  494. try {
  495. return $this->appManager->getAppPath($app) . '/l10n/';
  496. } catch (AppPathNotFoundException) {
  497. /* App not found, continue */
  498. }
  499. }
  500. return $this->serverRoot . '/core/l10n/';
  501. }
  502. /**
  503. * @inheritDoc
  504. */
  505. public function getLanguages(): array {
  506. $forceLanguage = $this->config->getSystemValue('force_language', false);
  507. if ($forceLanguage !== false) {
  508. $l = $this->get('lib', $forceLanguage);
  509. $potentialName = $l->t('__language_name__');
  510. return [
  511. 'commonLanguages' => [[
  512. 'code' => $forceLanguage,
  513. 'name' => $potentialName,
  514. ]],
  515. 'otherLanguages' => [],
  516. ];
  517. }
  518. $languageCodes = $this->findAvailableLanguages();
  519. $reduceToLanguages = $this->config->getSystemValue('reduce_to_languages', []);
  520. if (!empty($reduceToLanguages)) {
  521. $languageCodes = array_intersect($languageCodes, $reduceToLanguages);
  522. }
  523. $commonLanguages = [];
  524. $otherLanguages = [];
  525. foreach ($languageCodes as $lang) {
  526. $l = $this->get('lib', $lang);
  527. // TRANSLATORS this is the language name for the language switcher in the personal settings and should be the localized version
  528. $potentialName = $l->t('__language_name__');
  529. if ($l->getLanguageCode() === $lang && $potentialName[0] !== '_') { //first check if the language name is in the translation file
  530. $ln = [
  531. 'code' => $lang,
  532. 'name' => $potentialName
  533. ];
  534. } elseif ($lang === 'en') {
  535. $ln = [
  536. 'code' => $lang,
  537. 'name' => 'English (US)'
  538. ];
  539. } else { //fallback to language code
  540. $ln = [
  541. 'code' => $lang,
  542. 'name' => $lang
  543. ];
  544. }
  545. // put appropriate languages into appropriate arrays, to print them sorted
  546. // common languages -> divider -> other languages
  547. if (in_array($lang, self::COMMON_LANGUAGE_CODES)) {
  548. $commonLanguages[array_search($lang, self::COMMON_LANGUAGE_CODES)] = $ln;
  549. } else {
  550. $otherLanguages[] = $ln;
  551. }
  552. }
  553. ksort($commonLanguages);
  554. // sort now by displayed language not the iso-code
  555. usort($otherLanguages, function ($a, $b) {
  556. if ($a['code'] === $a['name'] && $b['code'] !== $b['name']) {
  557. // If a doesn't have a name, but b does, list b before a
  558. return 1;
  559. }
  560. if ($a['code'] !== $a['name'] && $b['code'] === $b['name']) {
  561. // If a does have a name, but b doesn't, list a before b
  562. return -1;
  563. }
  564. // Otherwise compare the names
  565. return strcmp($a['name'], $b['name']);
  566. });
  567. return [
  568. // reset indexes
  569. 'commonLanguages' => array_values($commonLanguages),
  570. 'otherLanguages' => $otherLanguages
  571. ];
  572. }
  573. }