Skip to content

Commit

Permalink
Set layout direction based on locale
Browse files Browse the repository at this point in the history
Signed-off-by: ali ghorbani <[email protected]>
  • Loading branch information
ghorbani-ali authored and nickvergessen committed Aug 20, 2024
1 parent 560282a commit a6268fb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion core/templates/layout.user.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<?php emit_script_loading_tags($_); ?>
<?php print_unescaped($_['headers']); ?>
</head>
<body id="<?php p($_['bodyid']);?>" <?php foreach ($_['enabledThemes'] as $themeId) {
<body dir="<?php p($_['direction']); ?>" id="<?php p($_['bodyid']);?>" <?php foreach ($_['enabledThemes'] as $themeId) {
p("data-theme-$themeId ");
}?> data-themes=<?php p(join(',', $_['enabledThemes'])) ?>>
<?php include 'layout.noscript.warning.php'; ?>
Expand Down
15 changes: 15 additions & 0 deletions lib/private/L10N/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ class Factory implements IFactory {
'pt_BR', 'pt_PT', 'da', 'fi_FI', 'nb_NO', 'sv', 'tr', 'zh_CN', 'ko'
];

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(
Expand Down Expand Up @@ -364,6 +370,15 @@ public function languageExists($app, $lang) {
return in_array($lang, $languages);
}

public function getLanguageDirectionFromLocale($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) {
Expand Down
4 changes: 3 additions & 1 deletion lib/private/TemplateLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,15 @@ public function __construct($renderAs, $appId = '') {
} 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)) {
Expand Down
8 changes: 8 additions & 0 deletions lib/public/L10N/IFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ public function languageExists($app, $lang);
*/
public function localeExists($locale);

/**
* Return the current language direction from locale
*
* @param string $locale
* @return string
*/
public function getLanguageDirectionFromLocale($locale): string;

/**
* iterate through language settings (if provided) in this order:
* 1. returns the forced language or:
Expand Down

0 comments on commit a6268fb

Please sign in to comment.