Skip to content

Instantly share code, notes, and snippets.

@hyochan
Created November 8, 2020 12:23
Show Gist options
  • Save hyochan/9d30ad1b8116ea6882b60865720ffb7b to your computer and use it in GitHub Desktop.
Save hyochan/9d30ad1b8116ea6882b60865720ffb7b to your computer and use it in GitHub Desktop.
FBT initializing util
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';
};
@hyochan
Copy link
Author

hyochan commented Nov 8, 2020

"scripts": {
  "start": "webpack serve --config webpack.config.js",
  "tsc": "tsc",
  "test": "jest",
  "production": "NODE_ENV=production webpack -p --config webpack.config.prod.js",
  "lint": "eslint src --ext .ts,.tsx,.js,.jsx",
  "manifest": "babel-node node_modules/.bin/fbt-manifest --src src",
  "collect-fbts": "babel-node node_modules/.bin/fbt-collect --pretty --manifest < .src_manifest.json > .source_strings.json",
  "test-collect-fbts": "babel-node node_modules/.bin/fbt-collect --plugins @babel/plugin-syntax-flow --pretty --manifest < .src_manifest.json > .test_source_strings.json",
  "translate-fbts": "babel-node node_modules/.bin/fbt-translate --translations assets/translations/*.json --jenkins > assets/translatedFbts.json",
  "translate-fbts-single-file": "babel-node node_modules/.bin/fbt-translate --jenkins --stdin < translation_input.json > assets/translatedFbts.json",
  "test-translate-fbts-into-output-dir": "babel-node node_modules/.bin/fbt-translate --translations assets/translations/*.json --jenkins --output-dir assets/translatedFbts",
  "clean-fbts": "rm .enum_manifest.json .src_manifest.json .source_strings.json assets/translatedFbts.json 2&> /dev/null || exit 0",
  "fbt-all": "yarn manifest && yarn collect-fbts && yarn translate-fbts"
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment