Usage outside a next.js app (in shared/ui
)
#1216
-
I have a monorepo like this:
In my next.js apps I have configured But the components I'm think that's because the shared/ui is not really a next.js app. But how can I make the translations work while inside I don't want to abstract the texts from the shared/ui and just pass it as props because that will be a lot of props. I'd love if I could make the translations as part of the shared/ui so that I only have to pass the important props <CommonHeader
backRoute={'/home"}
onSaveButtonClick={...}
/> and this way I don't have to worry about the translation of the words "back" and the word "save" because it's already happening inside The |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 6 replies
-
You can load translations from where you want using {
// ...rest of config
"loadLocaleFrom": (lang, ns) =>
// You can use a dynamic import, fetch, whatever. You should
// return a Promise with the JSON file.
import(`./myTranslationsFiles/${lang}/${ns}.json`).then((m) => m.default),
} |
Beta Was this translation helpful? Give feedback.
-
I made this quick test component in import React from "react";
import useTranslation from "next-translate/useTranslation";
import { Button } from "@sikka/hawa/button";
export const TranslatedComponent = () => {
const { t } = useTranslation("common");
return (
<div>
<Button>{t("common:back")}</Button>
<Button>{t("common:home")}</Button>
<Button>{t("common:save")}</Button>
</div>
);
}; And when I bundle |
Beta Was this translation helpful? Give feedback.
-
I tried using |
Beta Was this translation helpful? Give feedback.
-
I just tried using the <Button>
<Trans i18nKey="common:back" />
</Button> The result is similar to the screenshot above where the button text is "common:back" instead of "Back" |
Beta Was this translation helpful? Give feedback.
-
You can pass the export const TranslatedComponent = ({ t }) => {
return (
<div>
<Button>{t("common:back")}</Button>
<Button>{t("common:home")}</Button>
<Button>{t("common:save")}</Button>
</div>
);
}; And in your next.js app: export const SomePage = () => {
const { t } = useTranslation('common')
return (
<TranslatedComponent t={t}/>
)
} |
Beta Was this translation helpful? Give feedback.
You can pass the
t
as prop to yourshared/ui
componentsAnd in your next.js app: