{"version":3,"sources":["../src/index.ts","../src/init/createI18n.ts","../src/provider/I18nProvider.tsx","../src/store/I18nStore.ts","../src/context/contextHelper.tsx","../src/context/translatableContext.ts","../src/translatable/hookTranslatable.tsx","../src/translatable/hocTranslatable.tsx","../src/translatable/decoratorTranslatable.tsx"],"sourcesContent":["export { observer } from 'mobx-react-lite'\n\nexport * from './init/createI18n'\nexport * from './provider/I18nProvider'\nexport * from './store/I18nStore'\nexport * from './context/contextHelper'\nexport * from './context/translatableContext'\nexport { decoratorTranslatable as translatable } from './translatable/decoratorTranslatable'\nexport { hocTranslatable as withTranslatable } from './translatable/hocTranslatable'\nexport { hookTranslatable as useTranslatable } from './translatable/hookTranslatable'","import i18next, { i18n } from 'i18next'\nimport { initReactI18next } from 'react-i18next'\n\nexport interface CreateI18nOptions {\n  lang?: string\n  fallbackLang?: string | string[]\n  resources?: any\n  debug?: boolean\n}\n\n/** Create and init a configured i18n instance. */\nexport function createI18n(opts: CreateI18nOptions = {}): Promise<i18n> {\n  const {\n    lang = 'en',\n    fallbackLang = lang,\n    resources,\n    debug = false,\n  } = opts\n\n  const instance = i18next.createInstance()\n\n  return instance\n    .use(initReactI18next)\n    .init({\n      lng: lang,\n      fallbackLng: fallbackLang,\n      resources,\n      interpolation: { escapeValue: false },\n      debug,\n      returnNull: false,\n    }).then(() => instance)\n\n  // return instance\n}","import { I18nextProvider } from 'react-i18next'\nimport type { i18n } from 'i18next'\n\nexport interface I18nProviderProps {\n  i18n: i18n\n  children: React.ReactNode\n}\n\nexport function I18nProvider({ i18n, children }: I18nProviderProps) {\n  return <I18nextProvider i18n={i18n}>{children}</I18nextProvider>\n}","import { observable } from 'mobx'\nimport type { i18n } from 'i18next'\n\n/**\n* MobX store for current language. Changing locale will call i18next.changeLanguage.\n*/\nexport class I18nStore {\n  @observable locale: string\n  @observable private i18n: i18n\n\n  constructor(i18n: i18n) {\n    this.i18n = i18n\n    this.locale = i18n.language || 'en'\n  }\n\n  setLocale = async (lang: string) => {\n    if (this.locale === lang) return this.locale = lang\n    await this.i18n.changeLanguage(lang)\n  }\n}","import React from 'react'\nimport type { HubsContexts } from '../types'\n\n/**\n * This is an internal merged context\n */\nconst MergedContext = React.createContext<any>({})\n\n/**\n * Decorator that merges multiple React contexts for a class component.\n * Accepts individual context, contexts array, or functions returning one context.\n *\n * Example:\n * @contextHubs(CtxA, () => CtxB, CtxD)\n */\nexport function contextHubs(...inputs: HubsContexts[]): ClassDecorator {\n  return function (Target: any): any {\n    // if no input, return Target directly\n    if (inputs.length === 0) return Target\n    // HOC that combine multiple contexts into one for class component\n    const Wrapped: React.FC<any> = (props) => {\n      // normalize all inputs into a flat array of contexts\n      const contexts: React.Context<any>[] = inputs.flatMap((input) => {\n        if (typeof input === 'function') return [input()] // function returning context\n        if (Array.isArray(input)) return input          // array of contexts\n        return [input]                                  // single context\n      })\n      // get all context values and merge into one object\n      const values = contexts.map((Ctx) => React.useContext(Ctx))\n      const merged = Object.assign({}, ...values)\n      // provide merged context to Target\n      return (\n        <MergedContext.Provider value={merged}>\n          <Target {...props} />\n        </MergedContext.Provider>\n      )\n    }\n    // rename display name for debug\n    Wrapped.displayName = `contextHubs(${Target.displayName || Target.name || 'Component'})`\n    // return HOC\n    return Wrapped\n  }\n}\n\n\n/**\n * Class decorator for setting MergedContext as contextType.\n * Usage:\n *   @contextInjector()\n *   @observer\n *   class App extends React.Component {...}\n */\nexport function contextInjector(): ClassDecorator {\n  return function (Target: any): any {\n    // set merged context as contextType for Target\n    (Target as any).contextType = MergedContext;\n    // rename display name for debug\n    (Target as any).displayName = `contextInjector(${Target.displayName || Target.name})`\n    // call HOC to wrap Target\n    return Target\n  }\n}","import React from 'react'\nimport { hookTranslatable } from '../translatable/hookTranslatable'\nimport type { NamespaceOption, TranslatableOptions, TranslatableValue } from '../types'\n\nexport const TranslatableContext = (ns?: NamespaceOption, options?: TranslatableOptions): React.Context<TranslatableValue> => React.createContext(hookTranslatable(ns, options))","import { useMemo } from 'react'\nimport { useTranslation } from 'react-i18next'\nimport type { NamespaceOption, TranslatableOptions, TranslatableValue } from '../types'\n\n\n/**\n * Translations Hook.\n * Usage in function component:\n * const Hello = observer((props) => {\n *   const { i18n, t, ready } = useTranslatable('common')\n *   return <h1>{ t('hello', { name: 'World' }) } </h1>\n * })\n */\nexport function hookTranslatable(ns?: NamespaceOption, options?: TranslatableOptions): TranslatableValue {\n  // get translation related from react-i18next api\n  const { i18n, t, ready } = useTranslation(ns, { useSuspense: false, ...options })\n  // use memo to avoid unnecessary re-render\n  return useMemo(() => ({ t, i18n, ready }), [t, i18n, ready])\n}","import React from 'react'\nimport { hookTranslatable } from './hookTranslatable'\nimport type { NamespaceOption, TranslatableOptions, TranslatableValue } from '../types'\n\n\n/*\n * Usage - ONLY Class Component:\n *   class App extends React.Component {\n *     render() {\n *       return <div>{this.t('hello')}</div>\n *     }\n *   }\n *  export default withTranslatable('common')(observer(App))\n */\nexport const hocTranslatable = (ns?: NamespaceOption, options?: TranslatableOptions) =>\n  <P extends object>(Component: React.ComponentType<P>): React.FC<P> => {\n    // HOC that inject translatable context\n    const Wrapped: React.FC<P> = (props) => {\n      // Get translatable values from hookTranslatable\n      const value = hookTranslatable(ns, options);\n      // Construct a new class component that extends the input Component\n      const Base = Component as React.ComponentClass<P>\n      // Create a new class that extends the original component\n      class WithTranslatable extends Base {\n        // inject translatable values into class component instance\n        constructor(props: P) { super(props); Object.assign(this, value); }\n      }\n      // render the new class component\n      return <WithTranslatable {...props} />\n    }\n    // rename display name for debug\n    Wrapped.displayName = `hocTranslatable(${Component.displayName || Component.name || 'Component'})`\n    // return HOC\n    return Wrapped\n  }","import { hocTranslatable } from './hocTranslatable'\nimport type { NamespaceOption, TranslatableOptions } from '../types'\n\n/*\n * Usage - ONLY Class Component:\n *   @translatable('common')\n *   @obeserver\n *   class App extends React.Component {\n *     render() {\n *       return <div>{this.t('hello')}</div>\n *     }\n *   }\n *  export default App\n */\nexport function decoratorTranslatable(ns?: NamespaceOption, options?: TranslatableOptions): ClassDecorator {\n  return function (Target: any): any {\n    // call HOC to wrap Target\n    return hocTranslatable(ns, options)(Target)\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,6BAAyB;;;ACAzB,qBAA8B;AAC9B,2BAAiC;AAU1B,SAAS,WAAW,OAA0B,CAAC,GAAkB;AACtE,QAAM;AAAA,IACJ,OAAO;AAAA,IACP,eAAe;AAAA,IACf;AAAA,IACA,QAAQ;AAAA,EACV,IAAI;AAEJ,QAAM,WAAW,eAAAA,QAAQ,eAAe;AAExC,SAAO,SACJ,IAAI,qCAAgB,EACpB,KAAK;AAAA,IACJ,KAAK;AAAA,IACL,aAAa;AAAA,IACb;AAAA,IACA,eAAe,EAAE,aAAa,MAAM;AAAA,IACpC;AAAA,IACA,YAAY;AAAA,EACd,CAAC,EAAE,KAAK,MAAM,QAAQ;AAG1B;;;ACjCA,IAAAC,wBAAgC;AASvB;AADF,SAAS,aAAa,EAAE,MAAAC,OAAM,SAAS,GAAsB;AAClE,SAAO,4CAAC,yCAAgB,MAAMA,OAAO,UAAS;AAChD;;;ACVA,kBAA2B;AAMpB,IAAM,YAAN,MAAgB;AAAA,EAIrB,YAAYC,OAAY;AAKxB,qBAAY,OAAO,SAAiB;AAClC,UAAI,KAAK,WAAW,KAAM,QAAO,KAAK,SAAS;AAC/C,YAAM,KAAK,KAAK,eAAe,IAAI;AAAA,IACrC;AAPE,SAAK,OAAOA;AACZ,SAAK,SAASA,MAAK,YAAY;AAAA,EACjC;AAMF;AAZc;AAAA,EAAX;AAAA,GADU,UACC;AACQ;AAAA,EAAnB;AAAA,GAFU,UAES;;;ACRtB,mBAAkB;AAiCR,IAAAC,sBAAA;AA3BV,IAAM,gBAAgB,aAAAC,QAAM,cAAmB,CAAC,CAAC;AAS1C,SAAS,eAAe,QAAwC;AACrE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AM,UAAuB,CAAC,UAAU;AAEtC,UAAM,QAAQ,iBAAiB,IAAI,OAAO;AAE1C,UAAM,OAAO;AAAA,IAEb,MAAM,yBAAyB,KAAK;AAAA;AAAA,MAElC,YAAYC,QAAU;AAAE,cAAMA,MAAK;AAAG,eAAO,OAAO,MAAM,KAAK;AAAA,MAAG;AAAA,IACpE;AAEA,WAAO,6CAAC,oBAAkB,GAAG,OAAO;AAAA,EACtC;AAEA,UAAQ,cAAc,mBAAmB,UAAU,eAAe,UAAU,QAAQ,WAAW;AAE/F,SAAO;AACT;;;ACpBK,SAAS,sBAAsB,IAAsB,SAA+C;AACzG,SAAO,SAAU,QAAkB;AAEjC,WAAO,gBAAgB,IAAI,OAAO,EAAE,MAAM;AAAA,EAC5C;AACF;","names":["i18next","import_react_i18next","i18n","i18n","import_jsx_runtime","React","import_react","import_react","import_react_i18next","i18n","React","import_jsx_runtime","props"]}