diff options
-rw-r--r-- | config/config.sample.php | 11 | ||||
-rw-r--r-- | lib/private/L10N/Factory.php | 4 | ||||
-rw-r--r-- | tests/lib/L10N/FactoryTest.php | 47 |
3 files changed, 61 insertions, 1 deletions
diff --git a/config/config.sample.php b/config/config.sample.php index 288ea7e4a9b..77fd2490a14 100644 --- a/config/config.sample.php +++ b/config/config.sample.php @@ -5,7 +5,7 @@ * SPDX-FileCopyrightText: 2016 ownCloud, Inc. * SPDX-License-Identifier: AGPL-3.0-only */ - + /** * This configuration file is only provided to document the different * configuration options and their usage. @@ -229,6 +229,15 @@ $CONFIG = [ 'default_locale' => 'en_US', /** + * With this setting is possible to reduce the languages available in the + * language chooser. The languages have to be set as array values using ISO_639-1 + * language codes such as ``en`` for English, ``de`` for German etc. + * + * For example: Set to ['de', 'fr'] to only allow German and French languages. + */ +'reduce_to_languages' => [], + +/** * This sets the default region for phone numbers on your Nextcloud server, * using ISO 3166-1 country codes such as ``DE`` for Germany, ``FR`` for France, … * It is required to allow inserting phone numbers in the user profiles starting diff --git a/lib/private/L10N/Factory.php b/lib/private/L10N/Factory.php index 1825719fb75..6b6dc5d3b40 100644 --- a/lib/private/L10N/Factory.php +++ b/lib/private/L10N/Factory.php @@ -576,6 +576,10 @@ class Factory implements IFactory { } $languageCodes = $this->findAvailableLanguages(); + $reduceToLanguages = $this->config->getSystemValue('reduce_to_languages', []); + if (!empty($reduceToLanguages)) { + $languageCodes = array_intersect($languageCodes, $reduceToLanguages); + } $commonLanguages = []; $otherLanguages = []; diff --git a/tests/lib/L10N/FactoryTest.php b/tests/lib/L10N/FactoryTest.php index 771ce75d3e9..16113c0972f 100644 --- a/tests/lib/L10N/FactoryTest.php +++ b/tests/lib/L10N/FactoryTest.php @@ -700,6 +700,53 @@ class FactoryTest extends TestCase { self::assertSame($expected, $result); } + public static function dataTestReduceToLanguages(): array { + return [ + ['en', ['en', 'de', 'fr', 'it', 'es'], ['en', 'fr', 'de'], ['en', 'fr', 'de']], + ['en', ['en', 'de', 'fr', 'it', 'es'], ['en', 'de'], ['en', 'de']], + ['en', ['en', 'de', 'fr', 'it', 'es'], [], ['de', 'en', 'es', 'fr', 'it']], + ]; + } + + /** + * test + * - if available languages set can be reduced by configuration + * - if available languages set is not reduced to an empty set if + * the reduce config is an empty set + * + * @dataProvider dataTestReduceToLanguages + * + * @param string $lang + * @param array $availableLanguages + * @param array $reducedLanguageSet + * @param array $expected + */ + public function testReduceLanguagesByConfiguration(string $lang, array $availableLanguages, array $reducedLanguageSet, array $expected): void { + $factory = $this->getFactory(['findAvailableLanguages', 'languageExists']); + $factory->expects(self::any()) + ->method('languageExists')->willReturn(true); + $factory->expects(self::any()) + ->method('findAvailableLanguages') + ->willReturnCallback(function ($app) use ($availableLanguages) { + return $availableLanguages; + }); + + $this->config + ->method('getSystemValue') + ->willReturnMap([ + ['force_language', false, false], + ['default_language', false, $lang], + ['reduce_to_languages', [], $reducedLanguageSet] + ]); + + $result = $this->invokePrivate($factory, 'getLanguages'); + $commonLanguagesCodes = array_map(function ($lang) { + return $lang['code']; + }, $result['commonLanguages']); + + self::assertEqualsCanonicalizing($expected, $commonLanguagesCodes); + } + public function languageIteratorRequestProvider():array { return [ [ true, $this->createMock(IUser::class)], |