From 2b7efd66b6deb7fca62046a54b8ca0fe0310fe34 Mon Sep 17 00:00:00 2001 From: ali ghorbani Date: Fri, 15 Dec 2023 20:30:22 +0330 Subject: feat(rtl): Set layout direction based on language Signed-off-by: ali ghorbani --- lib/private/L10N/Factory.php | 16 ++++++++++++++++ lib/private/TemplateLayout.php | 4 +++- lib/public/L10N/IFactory.php | 9 +++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/private/L10N/Factory.php b/lib/private/L10N/Factory.php index 7dd0704682d..a74781a2ed0 100644 --- a/lib/private/L10N/Factory.php +++ b/lib/private/L10N/Factory.php @@ -58,6 +58,12 @@ class Factory implements IFactory { 'pt_BR', 'pt_PT', 'da', 'fi_FI', 'nb_NO', 'sv', 'tr', 'zh_CN', 'ko' ]; + public const RTL_LANGUAGES = [ + 'ae', 'ar', 'arc', 'arz', 'bcc', 'bqi', 'ckb', 'dv', 'fa', 'glk', + 'ha', 'he', 'khw', 'ks', 'ku', 'mzn', 'nqo', 'pnb', 'ps', 'sd', 'ug', + 'ur', 'uzs', 'yi', + ]; + private ICache $cache; public function __construct( @@ -364,6 +370,16 @@ class Factory implements IFactory { return in_array($lang, $languages); } + + public function getLanguageDirectionFromLocale(string $locale): string + { + if (in_array($locale, self::RTL_LANGUAGES)) { + return 'rtl'; + } + + return 'ltr'; + } + public function getLanguageIterator(?IUser $user = null): ILanguageIterator { $user = $user ?? $this->userSession->getUser(); if ($user === null) { diff --git a/lib/private/TemplateLayout.php b/lib/private/TemplateLayout.php index c21df495b5b..bca9af16d22 100644 --- a/lib/private/TemplateLayout.php +++ b/lib/private/TemplateLayout.php @@ -193,13 +193,15 @@ class TemplateLayout extends \OC_Template { } else { parent::__construct('core', 'layout.base'); } - // Send the language and the locale to our layouts + // Send the language, locale, and direction to our layouts $lang = \OC::$server->get(IFactory::class)->findLanguage(); $locale = \OC::$server->get(IFactory::class)->findLocale($lang); + $direction = \OC::$server->getL10NFactory()->getLanguageDirectionFromLocale($locale); $lang = str_replace('_', '-', $lang); $this->assign('language', $lang); $this->assign('locale', $locale); + $this->assign('direction', $direction); if (\OC::$server->getSystemConfig()->getValue('installed', false)) { if (empty(self::$versionHash)) { diff --git a/lib/public/L10N/IFactory.php b/lib/public/L10N/IFactory.php index 5c2f4dcbfa4..4259baa3b2f 100644 --- a/lib/public/L10N/IFactory.php +++ b/lib/public/L10N/IFactory.php @@ -103,6 +103,15 @@ interface IFactory { */ public function localeExists($locale); + /** + * Return the current language direction from locale + * + * @param string $locale + * @return 'ltr'|'rtl' + * @since 31.0.0 + */ + public function getLanguageDirectionFromLocale(string $locale): string; + /** * iterate through language settings (if provided) in this order: * 1. returns the forced language or: -- cgit v1.2.3 From ae06ab5860b007e94d63891d679e5c40b9292a8a Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 20 Aug 2024 16:32:42 +0200 Subject: fix(rtl): Fix list of RTL languages Signed-off-by: Joas Schilling --- lib/private/L10N/Factory.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/private/L10N/Factory.php b/lib/private/L10N/Factory.php index a74781a2ed0..ea6360efdf0 100644 --- a/lib/private/L10N/Factory.php +++ b/lib/private/L10N/Factory.php @@ -58,10 +58,17 @@ class Factory implements IFactory { 'pt_BR', 'pt_PT', 'da', 'fi_FI', 'nb_NO', 'sv', 'tr', 'zh_CN', 'ko' ]; + /** + * Keep in sync with `build/translation-checker.php` + */ public const RTL_LANGUAGES = [ - 'ae', 'ar', 'arc', 'arz', 'bcc', 'bqi', 'ckb', 'dv', 'fa', 'glk', - 'ha', 'he', 'khw', 'ks', 'ku', 'mzn', 'nqo', 'pnb', 'ps', 'sd', 'ug', - 'ur', 'uzs', 'yi', + 'ar', // Arabic + 'fa', // Persian + 'he', // Hebrew + 'ps', // Pashto, + 'ug', // 'Uyghurche / Uyghur + 'ur_PK', // Urdu + 'uz', // Uzbek Afghan ]; private ICache $cache; @@ -371,8 +378,7 @@ class Factory implements IFactory { } - public function getLanguageDirectionFromLocale(string $locale): string - { + public function getLanguageDirectionFromLocale(string $locale): string { if (in_array($locale, self::RTL_LANGUAGES)) { return 'rtl'; } -- cgit v1.2.3 From 82566c5479b7cf3374501a1e6b113680ae75afee Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 18 Sep 2024 11:13:30 +0200 Subject: fix(rtl): Make clear that the direction is based on language Signed-off-by: Joas Schilling --- lib/private/L10N/Factory.php | 5 ++--- lib/private/TemplateLayout.php | 2 +- lib/public/L10N/IFactory.php | 6 +++--- tests/lib/L10N/FactoryTest.php | 8 ++++---- 4 files changed, 10 insertions(+), 11 deletions(-) (limited to 'lib') diff --git a/lib/private/L10N/Factory.php b/lib/private/L10N/Factory.php index ea6360efdf0..a519ae7e761 100644 --- a/lib/private/L10N/Factory.php +++ b/lib/private/L10N/Factory.php @@ -377,9 +377,8 @@ class Factory implements IFactory { return in_array($lang, $languages); } - - public function getLanguageDirectionFromLocale(string $locale): string { - if (in_array($locale, self::RTL_LANGUAGES)) { + public function getLanguageDirection(string $language): string { + if (in_array($language, self::RTL_LANGUAGES, true)) { return 'rtl'; } diff --git a/lib/private/TemplateLayout.php b/lib/private/TemplateLayout.php index bca9af16d22..60c7526435e 100644 --- a/lib/private/TemplateLayout.php +++ b/lib/private/TemplateLayout.php @@ -196,7 +196,7 @@ class TemplateLayout extends \OC_Template { // Send the language, locale, and direction to our layouts $lang = \OC::$server->get(IFactory::class)->findLanguage(); $locale = \OC::$server->get(IFactory::class)->findLocale($lang); - $direction = \OC::$server->getL10NFactory()->getLanguageDirectionFromLocale($locale); + $direction = \OC::$server->getL10NFactory()->getLanguageDirection($lang); $lang = str_replace('_', '-', $lang); $this->assign('language', $lang); diff --git a/lib/public/L10N/IFactory.php b/lib/public/L10N/IFactory.php index 4259baa3b2f..aebd318dfad 100644 --- a/lib/public/L10N/IFactory.php +++ b/lib/public/L10N/IFactory.php @@ -104,13 +104,13 @@ interface IFactory { public function localeExists($locale); /** - * Return the current language direction from locale + * Return the language direction * - * @param string $locale + * @param string $language * @return 'ltr'|'rtl' * @since 31.0.0 */ - public function getLanguageDirectionFromLocale(string $locale): string; + public function getLanguageDirection(string $language): string; /** * iterate through language settings (if provided) in this order: diff --git a/tests/lib/L10N/FactoryTest.php b/tests/lib/L10N/FactoryTest.php index 510c165eba9..c29c31bf650 100644 --- a/tests/lib/L10N/FactoryTest.php +++ b/tests/lib/L10N/FactoryTest.php @@ -776,7 +776,7 @@ class FactoryTest extends TestCase { self::assertInstanceOf(ILanguageIterator::class, $iterator); } - public static function languagesWithRespectedDirection():array { + public static function dataGetLanguageDirection(): array { return [ ['en', 'ltr'], ['de', 'ltr'], @@ -786,11 +786,11 @@ class FactoryTest extends TestCase { } /** - * @dataProvider languagesWithRespectedDirection + * @dataProvider dataGetLanguageDirection */ - public function testDirectionOfLocales(string $locale, string $expectedDirection) { + public function testGetLanguageDirection(string $language, string $expectedDirection) { $factory = $this->getFactory(); - self::assertEquals($expectedDirection, $factory->getLanguageDirectionFromLocale($locale)); + self::assertEquals($expectedDirection, $factory->getLanguageDirection($language)); } } -- cgit v1.2.3