Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backend request doesn't work #140

Open
xian107 opened this issue May 28, 2024 · 5 comments
Open

backend request doesn't work #140

xian107 opened this issue May 28, 2024 · 5 comments

Comments

@xian107
Copy link

xian107 commented May 28, 2024

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
Copy link
Member

adrai commented May 28, 2024

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

@xian107
Copy link
Author

xian107 commented May 28, 2024

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

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

@adrai
Copy link
Member

adrai commented May 28, 2024

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

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

@xian107
Copy link
Author

xian107 commented May 28, 2024

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
Copy link
Member

adrai commented May 28, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants