diff --git a/src/api/api.ts b/src/api/api.ts index 57b5eb8..5bad9bf 100644 --- a/src/api/api.ts +++ b/src/api/api.ts @@ -17,7 +17,8 @@ import { AICompatCategories, AICompats, AIRequests, - UserCallbacks + UserCallbacks, + Translations } from './resources' const api = { @@ -42,6 +43,7 @@ const api = { getAiRequest: createMethod(AIRequests.createRequest), createUserCallbacks: createMethod(UserCallbacks.createRequestPost), getUserCallbacks: createMethod(UserCallbacks.createRequestGet), + getTranslations: createMethod(Translations.createRequest), } export type ApiContextValue = typeof api diff --git a/src/api/resources/Translations.ts b/src/api/resources/Translations.ts new file mode 100644 index 0000000..21bd892 --- /dev/null +++ b/src/api/resources/Translations.ts @@ -0,0 +1,20 @@ +import routes from "@/routes"; +import { getBaseHeaders } from "../utils"; + +export type Payload = null + +export interface Response { + translations: Translation[]; + meta: string; +} + +export interface Translation { + key: string; + value: string; +} + +export const createRequest = (): Request => { + const url = new URL(routes.server.getTranslations()); + + return new Request(url, { method: "GET", headers: getBaseHeaders() }); +}; diff --git a/src/api/resources/index.ts b/src/api/resources/index.ts index 9d8e986..c963b5f 100644 --- a/src/api/resources/index.ts +++ b/src/api/resources/index.ts @@ -16,3 +16,4 @@ export * as AICompatCategories from './AICompatCategories' export * as AICompats from './AICompats' export * as AIRequests from './AIRequests' export * as UserCallbacks from './UserCallbacks' +export * as Translations from './Translations' diff --git a/src/init.tsx b/src/init.tsx index c2a885d..9d32f66 100644 --- a/src/init.tsx +++ b/src/init.tsx @@ -1,40 +1,52 @@ -import React from 'react' -import i18next from 'i18next' -import ReactPostprocessor from 'i18next-react-postprocessor' -import { BrowserRouter } from 'react-router-dom' -import { I18nextProvider, initReactI18next } from 'react-i18next' -import { Provider } from 'react-redux' -import { store } from './store' -import { AuthProvider } from './auth' -import { ApiContext, createApi } from './api' -import { LegalContext, buildLegal } from './legal' -import { PaymentContext } from './payment' -import { getClientLocale, buildResources, fallbackLng } from './locales' -import App from './components/App' +import React from "react"; +import i18next from "i18next"; +import ReactPostprocessor from "i18next-react-postprocessor"; +import { BrowserRouter } from "react-router-dom"; +import { I18nextProvider, initReactI18next } from "react-i18next"; +import { Provider } from "react-redux"; +import { store } from "./store"; +import { AuthProvider } from "./auth"; +import { ApiContext, createApi } from "./api"; +import { LegalContext, buildLegal } from "./legal"; +import { PaymentContext } from "./payment"; +import { getClientLocale, buildResources, fallbackLng } from "./locales"; +import App from "./components/App"; const init = async () => { - const api = createApi() - const lng = getClientLocale() - const [elementsResponse, configResponse] = await Promise.all([ - api.getElements({ locale: lng }), - api.getAppConfig({ bundleId: 'auraweb' }), - ]) - const resources = buildResources(elementsResponse) - const legal = buildLegal(elementsResponse) - const config = configResponse.data - const i18nextInstance = i18next.createInstance() - const options = { lng, resources, fallbackLng, postProcess: [ `reactPostprocessor` ] } - await i18nextInstance.use(initReactI18next).use(new ReactPostprocessor()).init(options) - window.Chargebee.init(config.chargebee) + const api = createApi(); + const lng = getClientLocale(); + const [translationsResponse, elementsResponse, configResponse] = + await Promise.all([ + api.getTranslations(null), + api.getElements({ locale: lng }), + api.getAppConfig({ bundleId: "auraweb" }), + ]); + const resources = buildResources(translationsResponse); + const legal = buildLegal(elementsResponse); + const config = configResponse.data; + const i18nextInstance = i18next.createInstance(); + const options = { + lng, + resources, + fallbackLng, + postProcess: [`reactPostprocessor`], + }; + await i18nextInstance + .use(initReactI18next) + .use(new ReactPostprocessor()) + .init(options); + window.Chargebee.init(config.chargebee); return ( - + - + @@ -43,8 +55,8 @@ const init = async () => { - - ) -} + + ); +}; -export default init +export default init; diff --git a/src/locales/index.ts b/src/locales/index.ts index e037fb7..65559ad 100644 --- a/src/locales/index.ts +++ b/src/locales/index.ts @@ -1,4 +1,5 @@ -import { Elements } from '../api' +import { Translation } from '@/api/resources/Translations.ts' +import { Translations } from '../api' import dev from './dev.ts' export const getClientLocale = () => navigator.language.split('-')[0] @@ -6,14 +7,14 @@ export const getClientTimezone = () => Intl.DateTimeFormat().resolvedOptions().t export const fallbackLng = 'dev' const omitKeys = ['href', 'title', 'url_slug', 'type'] -const isWeb = (group: Elements.ElementGroup) => group.name === 'web' -const cleanUp = (element: Partial = {}) => { - return Object.entries(element) - .filter(([key]) => !omitKeys.includes(key)) - .reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {}) +// const isWeb = (group: Elements.ElementGroup) => group.name === 'web' +const cleanUp = (translation: Partial = []) => { + return translation + .filter((trans) => !omitKeys.includes(trans?.key || '')) + .reduce((acc, trans) => ({ ...acc, [trans?.key || '']: trans?.value || '' }), {}) } -export const buildResources = (resp: Elements.Response) => { - const element = resp.data.groups.find(isWeb)?.items.at(0) +export const buildResources = (resp: Translations.Response) => { + const element = resp.translations const translation = cleanUp(element) const lng = getClientLocale() return { [lng]: { translation }, dev } diff --git a/src/routes.ts b/src/routes.ts index a7cc266..ddf6fc3 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -67,6 +67,8 @@ const routes = { [apiHost, prefix, "user", "callbacks.json"].join("/"), getUserCallbacks: (id: string) => [apiHost, prefix, "user", "callbacks", `${id}.json`].join("/"), + getTranslations: () => + [apiHost, "api/v2", "t.json"].join("/"), }, };