summaryrefslogtreecommitdiffstats
path: root/lib/private/L10N
diff options
context:
space:
mode:
authorRoeland Jago Douma <roeland@famdouma.nl>2019-05-22 11:22:12 +0200
committerRoeland Jago Douma <roeland@famdouma.nl>2019-05-22 11:22:12 +0200
commitb2cc5d8fb6d6e25c86b9f93311dd2cb47f6dcb09 (patch)
treeff326a90cb8b2d093d747816068e6ee42e3bc66a /lib/private/L10N
parentf2885c171f5fc3b3dc54b612454616f5a929132a (diff)
downloadnextcloud-server-b2cc5d8fb6d6e25c86b9f93311dd2cb47f6dcb09.tar.gz
nextcloud-server-b2cc5d8fb6d6e25c86b9f93311dd2cb47f6dcb09.zip
Make the L10N loading lazy
Fixes #15675 This makes loading of the actual L10N lazy. So we only detect and load the actual translations when they are used. Instead of trying to load them all the time just because an app is enabled. Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'lib/private/L10N')
-rw-r--r--lib/private/L10N/Factory.php53
-rw-r--r--lib/private/L10N/LazyL10N.php70
2 files changed, 98 insertions, 25 deletions
diff --git a/lib/private/L10N/Factory.php b/lib/private/L10N/Factory.php
index 1c56949c40d..80f12c0a832 100644
--- a/lib/private/L10N/Factory.php
+++ b/lib/private/L10N/Factory.php
@@ -108,37 +108,40 @@ class Factory implements IFactory {
* @return \OCP\IL10N
*/
public function get($app, $lang = null, $locale = null) {
- $app = \OC_App::cleanAppId($app);
- if ($lang !== null) {
- $lang = str_replace(array('\0', '/', '\\', '..'), '', (string) $lang);
- }
+ return new LazyL10N(function() use ($app, $lang, $locale) {
- $forceLang = $this->config->getSystemValue('force_language', false);
- if (is_string($forceLang)) {
- $lang = $forceLang;
- }
+ $app = \OC_App::cleanAppId($app);
+ if ($lang !== null) {
+ $lang = str_replace(array('\0', '/', '\\', '..'), '', (string)$lang);
+ }
- $forceLocale = $this->config->getSystemValue('force_locale', false);
- if (is_string($forceLocale)) {
- $locale = $forceLocale;
- }
+ $forceLang = $this->config->getSystemValue('force_language', false);
+ if (is_string($forceLang)) {
+ $lang = $forceLang;
+ }
- if ($lang === null || !$this->languageExists($app, $lang)) {
- $lang = $this->findLanguage($app);
- }
+ $forceLocale = $this->config->getSystemValue('force_locale', false);
+ if (is_string($forceLocale)) {
+ $locale = $forceLocale;
+ }
- if ($locale === null || !$this->localeExists($locale)) {
- $locale = $this->findLocale($lang);
- }
+ if ($lang === null || !$this->languageExists($app, $lang)) {
+ $lang = $this->findLanguage($app);
+ }
- if (!isset($this->instances[$lang][$app])) {
- $this->instances[$lang][$app] = new L10N(
- $this, $app, $lang, $locale,
- $this->getL10nFilesForApp($app, $lang)
- );
- }
+ if ($locale === null || !$this->localeExists($locale)) {
+ $locale = $this->findLocale($lang);
+ }
- return $this->instances[$lang][$app];
+ if (!isset($this->instances[$lang][$app])) {
+ $this->instances[$lang][$app] = new L10N(
+ $this, $app, $lang, $locale,
+ $this->getL10nFilesForApp($app, $lang)
+ );
+ }
+
+ return $this->instances[$lang][$app];
+ });
}
/**
diff --git a/lib/private/L10N/LazyL10N.php b/lib/private/L10N/LazyL10N.php
new file mode 100644
index 00000000000..d30acf67a25
--- /dev/null
+++ b/lib/private/L10N/LazyL10N.php
@@ -0,0 +1,70 @@
+<?php
+declare(strict_types=1);
+/**
+ * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OC\L10N;
+
+use OCP\IL10N;
+
+class LazyL10N implements IL10N {
+
+ /** @var IL10N */
+ private $l;
+
+ /** @var \Closure */
+ private $factory;
+
+
+ public function __construct(\Closure $factory) {
+ $this->factory = $factory;
+ }
+
+ private function getL(): IL10N {
+ if ($this->l === null) {
+ $this->l = ($this->factory)();
+ }
+
+ return $this->l;
+ }
+
+ public function t(string $text, $parameters = []): string {
+ return $this->getL()->t($text, $parameters);
+ }
+
+ public function n(string $text_singular, string $text_plural, int $count, array $parameters = []): string {
+ return $this->getL()->n($text_singular, $text_plural, $count, $parameters);
+ }
+
+ public function l(string $type, $data, array $options = []) {
+ return $this->getL()->l($type, $data, $options);
+ }
+
+ public function getLanguageCode(): string {
+ return $this->getL()->getLanguageCode();
+ }
+
+ public function getLocaleCode(): string {
+ return $this->getL()->getLocaleCode();
+ }
+
+}