11"use client" ;
22
33import React , { createContext , useContext , useState , useCallback , useEffect } from "react" ;
4- import type { Locale , I18nContextType } from "./types" ;
4+ import type { Locale , I18nContextType , LocaleConfig } from "./types" ;
55import { translate } from "./translations" ;
66
77const I18nContext = createContext < I18nContextType | undefined > ( undefined ) ;
88I18nContext . displayName = "I18nContext" ;
99
1010const STORAGE_KEY = "clipcash_locale" ;
1111
12- const AVAILABLE_LOCALES : { value : Locale ; label : string } [ ] = [
13- { value : "en" , label : "English" } ,
14- { value : "es" , label : "Español" } ,
15- { value : "fr" , label : "Français" } ,
16- { value : "pt" , label : "Português" } ,
12+ const AVAILABLE_LOCALES : LocaleConfig [ ] = [
13+ { value : "en" , label : "English" , direction : "ltr" } ,
14+ { value : "es" , label : "Español" , direction : "ltr" } ,
15+ { value : "fr" , label : "Français" , direction : "ltr" } ,
16+ { value : "pt" , label : "Português" , direction : "ltr" } ,
17+ { value : "ar" , label : "العربية" , direction : "rtl" } ,
18+ { value : "he" , label : "עברית" , direction : "rtl" } ,
1719] ;
1820
21+ const RTL_LOCALES = new Set < Locale > ( [ "ar" , "he" ] ) ;
22+
1923export function I18nProvider ( { children } : { children : React . ReactNode } ) {
2024 const [ locale , setLocaleState ] = useState < Locale > ( "en" ) ;
2125
2226 useEffect ( ( ) => {
2327 const stored = localStorage . getItem ( STORAGE_KEY ) ;
24- if ( stored === "en" || stored === "es" || stored === "fr" || stored === "pt" ) {
28+ if ( stored === "en" || stored === "es" || stored === "fr" || stored === "pt" || stored === "ar" || stored === "he" ) {
2529 setLocaleState ( stored as Locale ) ;
2630 }
2731 } , [ ] ) ;
@@ -30,10 +34,12 @@ export function I18nProvider({ children }: { children: React.ReactNode }) {
3034 setLocaleState ( newLocale ) ;
3135 localStorage . setItem ( STORAGE_KEY , newLocale ) ;
3236 document . documentElement . lang = newLocale ;
37+ document . documentElement . dir = RTL_LOCALES . has ( newLocale ) ? "rtl" : "ltr" ;
3338 } , [ ] ) ;
3439
3540 useEffect ( ( ) => {
3641 document . documentElement . lang = locale ;
42+ document . documentElement . dir = RTL_LOCALES . has ( locale ) ? "rtl" : "ltr" ;
3743 } , [ locale ] ) ;
3844
3945 const t = useCallback (
@@ -43,13 +49,16 @@ export function I18nProvider({ children }: { children: React.ReactNode }) {
4349 [ locale ]
4450 ) ;
4551
52+ const dir = RTL_LOCALES . has ( locale ) ? "rtl" : "ltr" ;
53+
4654 return (
4755 < I18nContext . Provider
4856 value = { {
4957 locale,
5058 setLocale,
5159 t,
5260 locales : AVAILABLE_LOCALES ,
61+ dir,
5362 } }
5463 >
5564 { children }
@@ -63,4 +72,4 @@ export function useI18n(): I18nContextType {
6372 throw new Error ( "useI18n must be used within an I18nProvider" ) ;
6473 }
6574 return context ;
66- }
75+ }
0 commit comments