Commit aeef1ef1 authored by Adam Parák's avatar Adam Parák 💬
Browse files

add: i18n library

parent 48cc640c
Loading
Loading
Loading
Loading

locale/LICENSE

0 → 100644
+21 −0
Original line number Diff line number Diff line
MIT License

Copyright 2024 Masaryk University

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+4 −0
Original line number Diff line number Diff line
import { config } from '@inject/eslint-config'

/** @type {import("eslint").Linter.Config} */
export default config

locale/i18n.ts

0 → 100644
+71 −0
Original line number Diff line number Diff line
import i18n from 'i18next'
import './i18next.d.ts'
import { initReactI18next } from 'react-i18next'
import resourcesToBackend from 'i18next-resources-to-backend';

// the translations
// (tip move them in a JSON file and import them,
// or even better, manage them separated from your code: https://react.i18next.com/guides/multiple-translation-files)
export const DEFAULT_LOCALES = [
  { name: 'English', code: 'en' },
  { name: 'Čeština', code: 'cz' },
]

const DEFAULT_LANGUAGE = 'en'

// const AVAILABLE_LOCALES = DEFAULT_LOCALES
// window.__AVAILABLE_LOCALES__ || DEFAULT_LOCALES;

// const defaultBundle = (locale: string, ns: string) =>
//   import(`./lang/${locale}/${ns}.json`).then(res => res.default)

// function loadLocaleBundle(locale: string, ns: string) {
//   // Load default locale using dynamic imports
//   return defaultBundle(locale, ns).catch(err => {
//     console.error(err)
//   })
// }

i18n
  .use(initReactI18next) // passes i18n down to react-i18next
  //@ts-ignore
  .use(resourcesToBackend((language, namespace) => import(`./lang/${language}/${namespace}.json`)))
  .init({
    lng: DEFAULT_LANGUAGE,
    supportedLngs: ['en', 'cz'],
    defaultNS: 'common',
    ns: ['common', 'editor'],
    fallbackLng: DEFAULT_LANGUAGE,
    react: {
      useSuspense: true,
    },
    interpolation: {
      escapeValue: false,
    },
    // backend: {
    //   loadPath: '{{lng}}|{{ns}}',
    //   //@ts-ignore
    //   request: (_options, url, __payload, callback) => {
    //     try {
    //       const [lng, ns] = url.split('|')
    //       console.warn(url, __payload)

    //       // this mocks the HTTP fetch plugin behavior so it works with the backend AJAX pattern in this XHR library

    //       loadLocaleBundle(lng, ns).then(data => {
    //         callback(null, {
    //           data: JSON.stringify(data),
    //           status: 200, // status code is required by XHR plugin to determine success or failure
    //         })
    //       })
    //     } catch (e) {
    //       console.error(e)
    //       callback(null, {
    //         status: 500,
    //       })
    //     }
    //   },
    // },
  })

export default i18n

locale/i18next.d.ts

0 → 100644
+18 −0
Original line number Diff line number Diff line
import "i18next";

import common from "./lang/en/common.json";
import editor from "./lang/en/editor.json";

declare module "i18next" {
  // Extend CustomTypeOptions
  interface CustomTypeOptions {
    // custom namespace type, if you changed it
    defaultNS: "common";
    // custom resources type
    resources: {
      common: typeof common;
      editor: typeof editor;
    };
    // other
  }
}
 No newline at end of file

locale/index.tsx

0 → 100644
+9 −0
Original line number Diff line number Diff line
import i18n from "./i18n";
import './i18next.d.ts'
import { I18nextProvider } from "react-i18next";
import type { FC, PropsWithChildren } from "react"

export const TranslationProvider: FC<PropsWithChildren> = ({children}) => <I18nextProvider i18n={i18n}>
    {children}
</I18nextProvider>
export { Trans, useTranslation } from 'react-i18next'
 No newline at end of file
Loading