@@ -22,7 +22,11 @@ import tr from 'lang/locale/tr';
2222import uk from 'lang/locale/tr' ;
2323import zhCN from 'lang/locale/zh-cn' ;
2424import zhTW from 'lang/locale/zh-tw' ;
25+
26+ import * as Locales from 'date-fns/locale' ;
27+
2528import { LOGGER } from 'services/Logger' ;
29+ import { registerLocale } from 'react-datepicker' ;
2630
2731const localeMap : { [ k : string ] : Partial < typeof en > } = {
2832 ar,
@@ -51,8 +55,8 @@ const localeMap: { [k: string]: Partial<typeof en> } = {
5155 zh : zhCN ,
5256} ;
5357
54- const lang = localStorage . getItem ( 'language' ) ;
55- const locale = localeMap [ lang || 'en' ] ;
58+ export const OBSIDIAN_LOCALE = localStorage . getItem ( 'language' ) ;
59+ const locale = localeMap [ OBSIDIAN_LOCALE || 'en' ] ;
5660
5761/**
5862 * Translate a string to the current language or English if not found.
@@ -64,7 +68,7 @@ const locale = localeMap[lang || 'en'];
6468 */
6569export function t ( str : keyof typeof en , ...args : string [ ] ) : string {
6670 if ( ! locale ) {
67- LOGGER . error ( 'Error: database locale not found' , lang ) ;
71+ LOGGER . error ( 'Error: database locale not found' , OBSIDIAN_LOCALE ) ;
6872 }
6973 const translated = ( locale && locale [ str ] ) || en [ str ] ;
7074
@@ -76,3 +80,25 @@ export function t(str: keyof typeof en, ...args: string[]): string {
7680 // Replace any arguments in the string
7781 return args . reduce ( ( acc , arg , i ) => acc . replace ( `{${ i } }` , arg ) , translated ) ;
7882}
83+
84+ /**
85+ * If you trust the string to be in the current language (e.g. variable names) then use this.
86+ * @param str
87+ * @param args
88+ * @returns
89+ */
90+ export function dynamic_t ( str : string , ...args : string [ ] ) : string {
91+ return t ( str as keyof typeof en , ...args ) ;
92+ }
93+
94+ /**
95+ * Looks up a date-fns locale from the Expo localization object. This falls back to `en-US`
96+ * @param localization Expo Localization object containing the locale and region.
97+ * @returns date-fns locale.
98+ */
99+ export function registerDateFnLocale ( ) {
100+ const dynamicLocale =
101+ Locales [ OBSIDIAN_LOCALE as keyof typeof Locales ] || Locales . enUS ;
102+
103+ registerLocale ( OBSIDIAN_LOCALE , dynamicLocale ) ;
104+ }
0 commit comments