Replies: 1 comment 3 replies
-
I have been trying out some ways to dynamically pass custom headers to
export const getServerSideProps: GetServerSideProps = async ({ locale, req }) => {
const domain = req.headers.host;
// Assign the dynamic domain to customHeaders
i18nextConfig.backend.backendOption.customHeaders = {
"X-Academy-Domain": domain
};
return {
props: {
...(await serverSideTranslations(locale ?? i18nextConfig.i18n.defaultLocale, ["common", "titles"], i18nextConfig))
}
};
};
module.exports = {
// ...other config settings
backend: {
backend: HttpBackend,
backendOption: {
loadPath: "{{lng}}|{{ns}}",
customHeaders: {},
request: async (options, url, payload, callback) => {
const [lng, ns] = url.split("|");
await axios.get("/translations", {
params: { group: ns.split("+") },
headers: {
// access the customHeaders
"X-Academy-Domain": options.customHeaders["X-Academy-Domain"]
}
});
}
}
}
};
module.exports = function (domain) {
return {
// ...other config settings
backend: {
backend: HttpBackend,
backendOption: {
loadPath: "{{lng}}|{{ns}}",
request: async (options, url, payload, callback) => {
const [lng, ns] = url.split("|");
await axios.get("/translations", {
params: { group: ns.split("+") },
headers: {
"X-Academy-Domain": domain
}
});
}
}
}
};
};
export const getServerSideProps: GetServerSideProps = async ({ locale, req }) => {
const domain = req.headers.host;
const config = i18nextConfig(domain);
return {
props: {
...(await serverSideTranslations(locale ?? config.i18n.defaultLocale, ["common", "titles"], config))
}
};
}; but for the 2nd approach, I need to make adjustments to how
const { i18n } = require("./next-i18next.config.js")();
module.exports = {
// ...other settings
i18n: {
...i18n,
localeDetection: false
}
};
import i18nextConfig from "@/next-i18next.config.js";
export default appWithTranslation(App, i18nextConfig()); but I do not know if I would appreciate it if someone could confirm which method is better, or if anyone could suggest an alternative way to dynamically pass custom data to Thanks. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
hi, I am using
i18next-http-backend
to fetch translations from external API, and I have implemented a custom request for it, I need to pass with the request some headers that have a dynamic value based on the request host, but I was not able to find a way how to do it.my
next-i18next.config.js
:in
getServerSideProps
:Can anyone help me with the issue?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions