Skip to content

The open alternative to Salesforce, designed for AI.

Lay the app out right to left for Arabic and Hebrew (#25854)

Félix Malfaita3c77035dad8
diff --git a/packages/twenty-front/src/modules/app/components/App.tsx b/packages/twenty-front/src/modules/app/components/App.tsx
index 20fe0ee885a..7c2c1a25ade 100644
--- a/packages/twenty-front/src/modules/app/components/App.tsx
+++ b/packages/twenty-front/src/modules/app/components/App.tsx
@@ -1,5 +1,6 @@
import { DomainShell } from '@/app/components/DomainShell';
import { I18nActivationGate } from '@/app/components/I18nActivationGate';
+import { LocaleDirectionProvider } from '@/app/components/LocaleDirectionProvider';
import { ApolloDevLogEffect } from '@/debug/components/ApolloDevLogEffect';
import { AppErrorBoundary } from '@/error-handler/components/AppErrorBoundary';
import { AppRootErrorFallback } from '@/error-handler/components/AppRootErrorFallback';
@@ -25,22 +26,24 @@ export const App = () => {
>
<I18nActivationGate>
<I18nProvider i18n={i18n}>
- <ApolloDevLogEffect />
- <SnackBarComponentInstanceContext.Provider
- value={{ instanceId: 'snack-bar-manager' }}
- >
- <IconsProvider>
- <ExceptionHandlerProvider>
- <HelmetProvider>
- <ClickOutsideListenerContext.Provider
- value={{ excludedClickOutsideId: undefined }}
- >
- <DomainShell />
- </ClickOutsideListenerContext.Provider>
- </HelmetProvider>
- </ExceptionHandlerProvider>
- </IconsProvider>
- </SnackBarComponentInstanceContext.Provider>
+ <LocaleDirectionProvider>
+ <ApolloDevLogEffect />
+ <SnackBarComponentInstanceContext.Provider
+ value={{ instanceId: 'snack-bar-manager' }}
+ >
+ <IconsProvider>
+ <ExceptionHandlerProvider>
+ <HelmetProvider>
+ <ClickOutsideListenerContext.Provider
+ value={{ excludedClickOutsideId: undefined }}
+ >
+ <DomainShell />
+ </ClickOutsideListenerContext.Provider>
+ </HelmetProvider>
+ </ExceptionHandlerProvider>
+ </IconsProvider>
+ </SnackBarComponentInstanceContext.Provider>
+ </LocaleDirectionProvider>
</I18nProvider>
</I18nActivationGate>
</AppErrorBoundary>
diff --git a/packages/twenty-front/src/modules/app/components/LocaleDirectionProvider.tsx b/packages/twenty-front/src/modules/app/components/LocaleDirectionProvider.tsx
new file mode 100644
index 00000000000..cfc6c93380e
--- /dev/null
+++ b/packages/twenty-front/src/modules/app/components/LocaleDirectionProvider.tsx
@@ -0,0 +1,23 @@
+import { useLingui } from '@lingui/react';
+import { type ReactNode } from 'react';
+import { getLocaleTextDirection } from 'twenty-shared/translations';
+import { TextDirectionProvider } from 'twenty-ui/layout';
+
+type LocaleDirectionProviderProps = {
+ children: ReactNode;
+};
+
+// twenty-ui components already read their direction from Base UI, but nothing
+// ever provided one, so they all resolved to its ltr default no matter which
+// locale was active.
+export const LocaleDirectionProvider = ({
+ children,
+}: LocaleDirectionProviderProps) => {
+ const { i18n } = useLingui();
+
+ return (
+ <TextDirectionProvider direction={getLocaleTextDirection(i18n.locale)}>
+ {children}
+ </TextDirectionProvider>
+ );
+};
diff --git a/packages/twenty-front/src/utils/i18n/__tests__/dynamicActivate.test.ts b/packages/twenty-front/src/utils/i18n/__tests__/dynamicActivate.test.ts
new file mode 100644
index 00000000000..453b484a2a9
--- /dev/null
+++ b/packages/twenty-front/src/utils/i18n/__tests__/dynamicActivate.test.ts
@@ -0,0 +1,25 @@
+import { dynamicActivate } from '~/utils/i18n/dynamicActivate';
+
+describe('dynamicActivate', () => {
+ it('lays the document out right to left for a right-to-left locale', async () => {
+ await dynamicActivate('he-IL');
+
+ expect(document.documentElement.dir).toBe('rtl');
+ expect(document.documentElement.lang).toBe('he-IL');
+ });
+
+ it('lays it back out left to right when the locale changes', async () => {
+ await dynamicActivate('ar-SA');
+ await dynamicActivate('fr-FR');
+
+ expect(document.documentElement.dir).toBe('ltr');
+ expect(document.documentElement.lang).toBe('fr-FR');
+ });
+
+ it('falls back to the source locale for an unknown one', async () => {
+ await dynamicActivate('zz-ZZ' as 'en');
+
+ expect(document.documentElement.lang).toBe('en');
+ expect(document.documentElement.dir).toBe('ltr');
+ });
+});
diff --git a/packages/twenty-front/src/utils/i18n/dynamicActivate.ts b/packages/twenty-front/src/utils/i18n/dynamicActivate.ts
index 1aa56ec8829..5e743de3ee6 100644
--- a/packages/twenty-front/src/utils/i18n/dynamicActivate.ts
+++ b/packages/twenty-front/src/utils/i18n/dynamicActivate.ts
@@ -1,5 +1,9 @@
import { i18n } from '@lingui/core';
-import { APP_LOCALES, SOURCE_LOCALE } from 'twenty-shared/translations';
+import {
+ APP_LOCALES,
+ getLocaleTextDirection,
+ SOURCE_LOCALE,
+} from 'twenty-shared/translations';
export const dynamicActivate = async (locale: keyof typeof APP_LOCALES) => {
if (!Object.values(APP_LOCALES).includes(locale)) {
@@ -10,4 +14,7 @@ export const dynamicActivate = async (locale: keyof typeof APP_LOCALES) => {
const { messages } = await import(`../../locales/generated/${locale}.ts`);
i18n.load(locale, messages);
i18n.activate(locale);
+
+ document.documentElement.lang = locale;
+ document.documentElement.dir = getLocaleTextDirection(locale);
};
diff --git a/packages/twenty-shared/src/translations/index.ts b/packages/twenty-shared/src/translations/index.ts
index c444affad1f..92a142c49a7 100644
--- a/packages/twenty-shared/src/translations/index.ts
+++ b/packages/twenty-shared/src/translations/index.ts
@@ -10,3 +10,5 @@
export type { AppLocale } from './constants/AppLocales';
export { APP_LOCALES } from './constants/AppLocales';
export { SOURCE_LOCALE } from './constants/SourceLocale';
+export type { TextDirection } from './types/TextDirection';
+export { getLocaleTextDirection } from './utils/getLocaleTextDirection';
diff --git a/packages/twenty-shared/src/translations/types/TextDirection.ts b/packages/twenty-shared/src/translations/types/TextDirection.ts
new file mode 100644
index 00000000000..0099293356c
--- /dev/null
+++ b/packages/twenty-shared/src/translations/types/TextDirection.ts
@@ -0,0 +1 @@
+export type TextDirection = 'ltr' | 'rtl';
diff --git a/packages/twenty-shared/src/translations/utils/__tests__/getLocaleTextDirection.test.ts b/packages/twenty-shared/src/translations/utils/__tests__/getLocaleTextDirection.test.ts
new file mode 100644
index 00000000000..75d780ea6f7
--- /dev/null
+++ b/packages/twenty-shared/src/translations/utils/__tests__/getLocaleTextDirection.test.ts
@@ -0,0 +1,36 @@
+import { getLocaleTextDirection } from '@/translations/utils/getLocaleTextDirection';
+import { APP_LOCALES } from '@/translations/constants/AppLocales';
+
+describe('getLocaleTextDirection', () => {
+ it.each([
+ ['ar-SA', 'rtl'],
+ ['he-IL', 'rtl'],
+ ['en', 'ltr'],
+ ['fr-FR', 'ltr'],
+ ['zh-CN', 'ltr'],
+ ['sr-Latn', 'ltr'],
+ ['uz-UZ', 'ltr'],
+ ])('reads %s as %s', (locale, expected) => {
+ expect(getLocaleTextDirection(locale)).toBe(expected);
+ });
+
+ it('matches on the language subtag, not the region', () => {
+ expect(getLocaleTextDirection('ar')).toBe('rtl');
+ expect(getLocaleTextDirection('ar-EG')).toBe('rtl');
+ });
+
+ // A locale it cannot place is laid out left to right rather than throwing:
+ // the wrong direction is recoverable, a blank app is not.
+ it('falls back to ltr for an unknown locale', () => {
+ expect(getLocaleTextDirection('zz-ZZ')).toBe('ltr');
+ expect(getLocaleTextDirection('')).toBe('ltr');
+ });
+
+ it('places every locale the app ships', () => {
+ const directions = Object.values(APP_LOCALES).map(getLocaleTextDirection);
+
+ expect(directions.filter((direction) => direction === 'rtl')).toHaveLength(
+ 2,
+ );
+ });
+});
diff --git a/packages/twenty-shared/src/translations/utils/getLocaleTextDirection.ts b/packages/twenty-shared/src/translations/utils/getLocaleTextDirection.ts
new file mode 100644
index 00000000000..d81e16d8e5e
--- /dev/null
+++ b/packages/twenty-shared/src/translations/utils/getLocaleTextDirection.ts
@@ -0,0 +1,19 @@
+import { type TextDirection } from '@/translations/types/TextDirection';
+
+// Scripts written right to left, by ISO 639-1 language subtag. Twenty ships
+// Arabic and Hebrew today; the rest are listed so that ticking one of them in
+// Crowdin does not silently lay the app out backwards.
+const RIGHT_TO_LEFT_LANGUAGES = [
+ 'ar',
+ 'dv',
+ 'fa',
+ 'he',
+ 'ps',
+ 'sd',
+ 'ug',
+ 'ur',
+ 'yi',
+];
+
+export const getLocaleTextDirection = (locale: string): TextDirection =>
+ RIGHT_TO_LEFT_LANGUAGES.includes(locale.split('-')[0]) ? 'rtl' : 'ltr';
diff --git a/packages/twenty-ui/src/layout/TextDirectionProvider/TextDirectionProvider.tsx b/packages/twenty-ui/src/layout/TextDirectionProvider/TextDirectionProvider.tsx
new file mode 100644
index 00000000000..a1e230d8d6e
--- /dev/null
+++ b/packages/twenty-ui/src/layout/TextDirectionProvider/TextDirectionProvider.tsx
@@ -0,0 +1,17 @@
+import { DirectionProvider } from '@base-ui/react/direction-provider';
+import { type ReactNode } from 'react';
+
+type TextDirectionProviderProps = {
+ direction: 'ltr' | 'rtl';
+ children: ReactNode;
+};
+
+// Components here read their direction from Base UI, so the provider that sets
+// it belongs here too: consumers reach it through twenty-ui the way they reach
+// icons through twenty-ui/icon, rather than taking on Base UI themselves.
+export const TextDirectionProvider = ({
+ direction,
+ children,
+}: TextDirectionProviderProps) => (
+ <DirectionProvider direction={direction}>{children}</DirectionProvider>
+);
diff --git a/packages/twenty-ui/src/layout/index.ts b/packages/twenty-ui/src/layout/index.ts
index 66006c01058..86c82b2b4d7 100644
--- a/packages/twenty-ui/src/layout/index.ts
+++ b/packages/twenty-ui/src/layout/index.ts
@@ -24,3 +24,4 @@ export { HorizontalSeparator } from './HorizontalSeparator/HorizontalSeparator';
export { useResizeHandle } from './ResizeHandle/hooks/useResizeHandle';
export { ResizeHandle } from './ResizeHandle/ResizeHandle';
export { SectionAlignment, SectionFontColor, Section } from './Section/Section';
+export { TextDirectionProvider } from './TextDirectionProvider/TextDirectionProvider';