Factory.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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->request->getParam('forceLanguage') ?? $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->request->getParam('forceLanguage') ?? $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->request->getParam('forceLanguage') ?? $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. if (($forcedLanguage = $this->request->getParam('forceLanguage')) !== null) {
  360. return $forcedLanguage;
  361. }
  362. // Use language from request
  363. if ($this->userSession->getUser() instanceof IUser &&
  364. $user->getUID() === $this->userSession->getUser()->getUID()) {
  365. try {
  366. return $this->getLanguageFromRequest();
  367. } catch (LanguageNotFoundException $e) {
  368. }
  369. }
  370. }
  371. return $this->request->getParam('forceLanguage') ?? $this->config->getSystemValueString('default_language', 'en');
  372. }
  373. /**
  374. * @param string $locale
  375. * @return bool
  376. */
  377. public function localeExists($locale) {
  378. if ($locale === 'en') { //english is always available
  379. return true;
  380. }
  381. if ($this->localeCache === []) {
  382. $locales = $this->findAvailableLocales();
  383. foreach ($locales as $l) {
  384. $this->localeCache[$l['code']] = true;
  385. }
  386. }
  387. return isset($this->localeCache[$locale]);
  388. }
  389. /**
  390. * @throws LanguageNotFoundException
  391. */
  392. private function getLanguageFromRequest(?string $app = null): string {
  393. $header = $this->request->getHeader('ACCEPT_LANGUAGE');
  394. if ($header !== '') {
  395. $available = $this->findAvailableLanguages($app);
  396. // E.g. make sure that 'de' is before 'de_DE'.
  397. sort($available);
  398. $preferences = preg_split('/,\s*/', strtolower($header));
  399. foreach ($preferences as $preference) {
  400. [$preferred_language] = explode(';', $preference);
  401. $preferred_language = str_replace('-', '_', $preferred_language);
  402. $preferred_language_parts = explode('_', $preferred_language);
  403. foreach ($available as $available_language) {
  404. if ($preferred_language === strtolower($available_language)) {
  405. return $this->respectDefaultLanguage($app, $available_language);
  406. }
  407. if (strtolower($available_language) === $preferred_language_parts[0] . '_' . end($preferred_language_parts)) {
  408. return $available_language;
  409. }
  410. }
  411. // Fallback from de_De to de
  412. foreach ($available as $available_language) {
  413. if (substr($preferred_language, 0, 2) === $available_language) {
  414. return $available_language;
  415. }
  416. }
  417. }
  418. }
  419. throw new LanguageNotFoundException();
  420. }
  421. /**
  422. * if default language is set to de_DE (formal German) this should be
  423. * preferred to 'de' (non-formal German) if possible
  424. */
  425. protected function respectDefaultLanguage(?string $app, string $lang): string {
  426. $result = $lang;
  427. $defaultLanguage = $this->config->getSystemValue('default_language', false);
  428. // use formal version of german ("Sie" instead of "Du") if the default
  429. // language is set to 'de_DE' if possible
  430. if (
  431. is_string($defaultLanguage) &&
  432. strtolower($lang) === 'de' &&
  433. strtolower($defaultLanguage) === 'de_de' &&
  434. $this->languageExists($app, 'de_DE')
  435. ) {
  436. $result = 'de_DE';
  437. }
  438. return $result;
  439. }
  440. /**
  441. * Checks if $sub is a subdirectory of $parent
  442. *
  443. * @param string $sub
  444. * @param string $parent
  445. * @return bool
  446. */
  447. private function isSubDirectory($sub, $parent) {
  448. // Check whether $sub contains no ".."
  449. if (str_contains($sub, '..')) {
  450. return false;
  451. }
  452. // Check whether $sub is a subdirectory of $parent
  453. if (str_starts_with($sub, $parent)) {
  454. return true;
  455. }
  456. return false;
  457. }
  458. /**
  459. * Get a list of language files that should be loaded
  460. *
  461. * @return string[]
  462. */
  463. private function getL10nFilesForApp(string $app, string $lang): array {
  464. $languageFiles = [];
  465. $i18nDir = $this->findL10nDir($app);
  466. $transFile = strip_tags($i18nDir) . strip_tags($lang) . '.json';
  467. if (($this->isSubDirectory($transFile, $this->serverRoot . '/core/l10n/')
  468. || $this->isSubDirectory($transFile, $this->serverRoot . '/lib/l10n/')
  469. || $this->isSubDirectory($transFile, $this->appManager->getAppPath($app) . '/l10n/'))
  470. && file_exists($transFile)
  471. ) {
  472. // load the translations file
  473. $languageFiles[] = $transFile;
  474. }
  475. // merge with translations from theme
  476. $theme = $this->config->getSystemValueString('theme');
  477. if (!empty($theme)) {
  478. $transFile = $this->serverRoot . '/themes/' . $theme . substr($transFile, strlen($this->serverRoot));
  479. if (file_exists($transFile)) {
  480. $languageFiles[] = $transFile;
  481. }
  482. }
  483. return $languageFiles;
  484. }
  485. /**
  486. * find the l10n directory
  487. *
  488. * @param string $app App id or empty string for core
  489. * @return string directory
  490. */
  491. protected function findL10nDir($app = null) {
  492. if (in_array($app, ['core', 'lib'])) {
  493. if (file_exists($this->serverRoot . '/' . $app . '/l10n/')) {
  494. return $this->serverRoot . '/' . $app . '/l10n/';
  495. }
  496. } elseif ($app) {
  497. try {
  498. return $this->appManager->getAppPath($app) . '/l10n/';
  499. } catch (AppPathNotFoundException) {
  500. /* App not found, continue */
  501. }
  502. }
  503. return $this->serverRoot . '/core/l10n/';
  504. }
  505. /**
  506. * @inheritDoc
  507. */
  508. public function getLanguages(): array {
  509. $forceLanguage = $this->config->getSystemValue('force_language', false);
  510. if ($forceLanguage !== false) {
  511. $l = $this->get('lib', $forceLanguage);
  512. $potentialName = $l->t('__language_name__');
  513. return [
  514. 'commonLanguages' => [[
  515. 'code' => $forceLanguage,
  516. 'name' => $potentialName,
  517. ]],
  518. 'otherLanguages' => [],
  519. ];
  520. }
  521. $languageCodes = $this->findAvailableLanguages();
  522. $reduceToLanguages = $this->config->getSystemValue('reduce_to_languages', []);
  523. if (!empty($reduceToLanguages)) {
  524. $languageCodes = array_intersect($languageCodes, $reduceToLanguages);
  525. }
  526. $commonLanguages = [];
  527. $otherLanguages = [];
  528. foreach ($languageCodes as $lang) {
  529. $l = $this->get('lib', $lang);
  530. // TRANSLATORS this is the language name for the language switcher in the personal settings and should be the localized version
  531. $potentialName = $l->t('__language_name__');
  532. if ($l->getLanguageCode() === $lang && $potentialName[0] !== '_') { //first check if the language name is in the translation file
  533. $ln = [
  534. 'code' => $lang,
  535. 'name' => $potentialName
  536. ];
  537. } elseif ($lang === 'en') {
  538. $ln = [
  539. 'code' => $lang,
  540. 'name' => 'English (US)'
  541. ];
  542. } else { //fallback to language code
  543. $ln = [
  544. 'code' => $lang,
  545. 'name' => $lang
  546. ];
  547. }
  548. // put appropriate languages into appropriate arrays, to print them sorted
  549. // common languages -> divider -> other languages
  550. if (in_array($lang, self::COMMON_LANGUAGE_CODES)) {
  551. $commonLanguages[array_search($lang, self::COMMON_LANGUAGE_CODES)] = $ln;
  552. } else {
  553. $otherLanguages[] = $ln;
  554. }
  555. }
  556. ksort($commonLanguages);
  557. // sort now by displayed language not the iso-code
  558. usort($otherLanguages, function ($a, $b) {
  559. if ($a['code'] === $a['name'] && $b['code'] !== $b['name']) {
  560. // If a doesn't have a name, but b does, list b before a
  561. return 1;
  562. }
  563. if ($a['code'] !== $a['name'] && $b['code'] === $b['name']) {
  564. // If a does have a name, but b doesn't, list a before b
  565. return -1;
  566. }
  567. // Otherwise compare the names
  568. return strcmp($a['name'], $b['name']);
  569. });
  570. return [
  571. // reset indexes
  572. 'commonLanguages' => array_values($commonLanguages),
  573. 'otherLanguages' => $otherLanguages
  574. ];
  575. }
  576. }