Created
November 8, 2020 12:23
-
-
Save hyochan/9d30ad1b8116ea6882b60865720ffb7b to your computer and use it in GitHub Desktop.
FBT initializing util
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { init } from 'fbt'; | |
import intl from '../../assets/translatedFbts.json'; | |
const DEFAULT_LOCALE = 'en'; | |
export const viewerContext = { | |
locale: DEFAULT_LOCALE, | |
}; | |
export const initFbt = (): void => { | |
if (navigator) { | |
viewerContext.locale = navigator.language.substr(0, 2); | |
} | |
init({ | |
translations: intl as FBT.Translations, | |
hooks: { | |
getViewerContext: (): { locale: string } => viewerContext, | |
}, | |
}); | |
}; | |
export const LOCALES = Object.freeze({ | |
ko: Object.freeze({ | |
bcp47: 'ko', | |
rtl: false, | |
}), | |
en: Object.freeze({ | |
bcp47: 'en', | |
rtl: false, | |
}), | |
}); | |
export enum Locale { | |
KO = 'ko', | |
EN = 'en', | |
} | |
export const changeLocale = (locale: Locale): void => { | |
viewerContext.locale = locale; | |
const html = document.getElementsByTagName('html')[0]; | |
if (html != null) { | |
html.lang = LOCALES[locale].bcp47; | |
} | |
document.body.className = LOCALES[locale].rtl ? 'rtl' : 'ltr'; | |
}; |
Author
hyochan
commented
Nov 8, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment