From b3921c65a68111aa319e75f546953f77ebd8008f Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Wed, 27 Jun 2018 19:43:46 +0200 Subject: cache available locales to minimize fs access Signed-off-by: Georg Ehrke --- lib/private/L10N/Factory.php | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/private/L10N/Factory.php b/lib/private/L10N/Factory.php index 8c35d75a4b5..6d6f452738c 100644 --- a/lib/private/L10N/Factory.php +++ b/lib/private/L10N/Factory.php @@ -54,6 +54,11 @@ class Factory implements IFactory { */ protected $availableLanguages = []; + /** + * @var array + */ + protected $availableLocales = []; + /** * @var array Structure: string => callable */ @@ -195,8 +200,13 @@ class Factory implements IFactory { return 'en'; } - public function findLocale($lang = null) - { + /** + * find the best locale + * + * @param string $lang + * @return null|string + */ + public function findLocale($lang = null) { if ($this->config->getSystemValue('installed', false)) { $userId = null !== $this->userSession->getUser() ? $this->userSession->getUser()->getUID() : null; $userLocale = null; @@ -278,10 +288,18 @@ class Factory implements IFactory { return $available; } - public function findAvailableLocales() - { + /** + * @return array|mixed + */ + public function findAvailableLocales() { + if (!empty($this->availableLocales)) { + return $this->availableLocales; + } + $localeData = file_get_contents(__DIR__ . '/locales.json'); - return json_decode($localeData, true); + $this->availableLocales = \json_decode($localeData, true); + + return $this->availableLocales; } /** @@ -308,7 +326,6 @@ class Factory implements IFactory { } $locales = $this->findAvailableLocales(); - $userLocale = array_filter($locales, function($value) use ($locale) { return $locale === $value['code']; }); -- cgit v1.2.3