From 2b7efd66b6deb7fca62046a54b8ca0fe0310fe34 Mon Sep 17 00:00:00 2001 From: ali ghorbani Date: Fri, 15 Dec 2023 20:30:22 +0330 Subject: [PATCH] feat(rtl): Set layout direction based on language Signed-off-by: ali ghorbani --- core/templates/layout.user.php | 2 +- lib/private/L10N/Factory.php | 16 ++++++++++++++++ lib/private/TemplateLayout.php | 4 +++- lib/public/L10N/IFactory.php | 9 +++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/core/templates/layout.user.php b/core/templates/layout.user.php index fc0f3af2d09..7f5322fee29 100644 --- a/core/templates/layout.user.php +++ b/core/templates/layout.user.php @@ -51,7 +51,7 @@ p($theme->getTitle()); - data-themes=> 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: