i18next / i18next-http-backend

i18next-http-backend is a backend layer for i18next using in Node.js, in the browser and for Deno.
MIT License
430 stars 67 forks source link

backend request doesn't work #140

Open xian107 opened 2 months ago

xian107 commented 2 months ago

i18next::translator: missingKey en translation K1 K1 image

` import i18next from "i18next"; import { initReactI18next } from "react-i18next"; import LanguageDetector from "i18next-browser-languagedetector"; import backend from "i18next-http-backend"; import axios from "axios"; i18next .use(LanguageDetector) .use(initReactI18next) .use(backend) .init( { backend: { //loadPath: "https:/xxx.json", request: async (options, url, payload, callback) => { const result = await axios.get("https:/xxx.json") if (result.status === 200 && result.data) { callback(null, { status: 200, data: { en: { translation: { 'K1': "YES" } }, zh: { translation: { 'K1': "是" } //translation: result.data["CN"], }, }, }); }

            },
        },
        fallbackLng: "en",
        lng: "en",
        debug: true,
        ns: ['translation'],
        defaultNS: 'translation',
        interpolation: {
            escapeValue: false, // not needed for react!!
        },
    },
    (err, t) => {
        if (err) {
            console.error("i18next error:", err);
        } else {
            console.log("i18 success");
        }
    }
);

export default i18next `

page

` import {useTranslation} from 'react-i18next';

export default function Home() { const {t} = useTranslation(); return (

{t("K1")}

) } ` The page shows K1,doesn't work。i18next::translator: missingKey en translation K1 K1

adrai commented 2 months ago

Please provide a minimal reproducible example repository, not just code snippets.

xian107 commented 2 months ago

The page shows K1,doesn't work。i18next::translator: missingKey en translation K1 K1

@adrai excuse me https://github.com/xian107/texti18.git

adrai commented 2 months ago

Your response data is wrong. You need to return only the corresponding translations for an individual namespace and language:

Only: data: { 'K1': "Yes" }

xian107 commented 2 months ago

Your response data is wrong. You need to return only the corresponding translations for an individual namespace and language:

Only: data: { 'K1': "Yes" }

Only: data: { 'K1': "Yes" } , How to modify the language? The callback does not pass in the CN data.

adrai commented 2 months ago

i18next is responsible for loading the appropriate language and namespace... your custom request method, only needs to return the corresponding language<->namespace data...