I do not understand how to reuse messages #1307
-
I think this is a very common way of structuring a localization file (en.json). {
"common": {
"close": "Close",
"save": "Save"
},
"dashboard": {
"title": "This is the title of the page"
}
} In such a case, how would I be able to get the common inside my Example 1: export default function DashboardPage() {
const t = useTranslations("dashboard");
const test = t('title')
// ???
const example = t('common.close')
return <>....</>
} another way I tried, which somewhat works is the following, however my editor does not like this (vscode with i18n ally). Example 2: export default function DashboardPage() {
const t = useTranslations("dashboard");
const tCommon = useTranslations('common');
const test = t('title');
const test2 = tCommon('save');
} Let me start off with sharing what the documentation says:
I don't see the need for a SaveButton component just to translate the word "Save." Following this approach would mean creating numerous components just for translating common words. For instance, if I want to use the "Save" translation for a link, would I need to create yet another component just for that? The most relevant information here seems to be that I can use translations from different namespaces via useTranslations. However, when I do this, I seem to lose type safety. Is this something with i18n Ally? Inside my settings.json I have added the following: "i18n-ally.regex.usageMatch": [
"[^\\w\\d](?:t|tCommon)\\(['\"`]({key})['\"`]"
], but now in the example 2 above, when I translate {
"common": {
"close": "Close",
"save": "Save",
"title": "......"
},
"dashboard": {
"title": "This is the title of the page"
}
} How would I solve his? Others must have had similar problems, how did you solve it? I have also opened a discussion on the i18n Ally github page: lokalise/i18n-ally#1196 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Maybe this section from the structuring messages docs is helpful to you:
You can use that to access all translations, from any namespace. |
Beta Was this translation helpful? Give feedback.
Maybe this section from the structuring messages docs is helpful to you:
You can use that to access all translations, from any namespace.