From abab7eb7f071282a4a3fa839dcc6f61ac4632f6b Mon Sep 17 00:00:00 2001
From: Thomas Citharel <tcit@tcit.fr>
Date: Thu, 13 Jul 2017 10:34:27 +0200
Subject: [PATCH] handle config.php values

Signed-off-by: Georg Ehrke <developer@georgehrke.com>
---
 .../lib/Controller/UsersController.php        |  4 ++++
 config/config.sample.php                      | 23 +++++++++++++++++++
 lib/private/L10N/Factory.php                  | 15 ++++++++----
 3 files changed, 37 insertions(+), 5 deletions(-)

diff --git a/apps/provisioning_api/lib/Controller/UsersController.php b/apps/provisioning_api/lib/Controller/UsersController.php
index 32967742bd2..8b39de29086 100644
--- a/apps/provisioning_api/lib/Controller/UsersController.php
+++ b/apps/provisioning_api/lib/Controller/UsersController.php
@@ -442,6 +442,10 @@ class UsersController extends AUserData {
 			if ($this->config->getSystemValue('force_language', false) === false ||
 				$this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
 				$permittedFields[] = 'language';
+			}
+
+			if ($this->config->getSystemValue('force_locale', false) === false ||
+				$this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
 				$permittedFields[] = 'locale';
 			}
 
diff --git a/config/config.sample.php b/config/config.sample.php
index a1445d77095..2218021bab9 100644
--- a/config/config.sample.php
+++ b/config/config.sample.php
@@ -178,6 +178,29 @@ $CONFIG = array(
  */
 'force_language' => 'en',
 
+/**
+ * This sets the default locale on your Nextcloud server, using ISO_639
+ * language codes such as ``en`` for English, ``de`` for German, and ``fr`` for
+ * French, and ISO-3166 country codes such as ``GB``, ``US``, ``CA``, as defined
+ * in RFC 5646. It overrides automatic locale detection on public pages like
+ * login or shared items. User's locale preferences configured under "personal
+ * -> locale" override this setting after they have logged in.
+ *
+ * Defaults to ``en``
+ */
+'default_locale' => 'en_US',
+
+/**
+ * With this setting a locale can be forced for all users. If a locale is
+ * forced, the users are also unable to change their locale in the personal
+ * settings. If users shall be unable to change their locale, but users have
+ * different languages, this value can be set to ``true`` instead of a locale
+ * code.
+ *
+ * Defaults to ``false``
+ */
+'force_locale' => 'en_US',
+
 /**
  * Set the default app to open on login. Use the app names as they appear in the
  * URL after clicking them in the Apps menu, such as documents, calendar, and
diff --git a/lib/private/L10N/Factory.php b/lib/private/L10N/Factory.php
index a6b91d53e4f..86f2ab3b1f0 100644
--- a/lib/private/L10N/Factory.php
+++ b/lib/private/L10N/Factory.php
@@ -111,6 +111,11 @@ class Factory implements IFactory {
 			$lang = $forceLang;
 		}
 
+		$forceLocale = $this->config->getSystemValue('force_locale', false);
+		if (is_string($forceLocale)) {
+			$locale = $forceLocale;
+		}
+
 		if ($lang === null || !$this->languageExists($app, $lang)) {
 			$lang = $this->findLanguage($app);
 		}
@@ -207,17 +212,17 @@ class Factory implements IFactory {
 			return $userLocale;
 		}
 
-		// If no user locale set, use lang as locale
-		if (null !== $lang && $this->localeExists($lang)) {
-			return $lang;
-		}
-
 		// Default : use system default locale
 		$defaultLocale = $this->config->getSystemValue('default_locale', false);
 		if ($defaultLocale !== false && $this->localeExists($defaultLocale)) {
 			return $defaultLocale;
 		}
 
+		// If no user locale set, use lang as locale
+		if (null !== $lang && $this->localeExists($lang)) {
+			return $lang;
+		}
+
 		// At last, return USA
 		return 'en_US';
 	}
-- 
GitLab