@intlify/vite-plugin-vue-i18n locale translation files splitting
See original GitHub issueClear and concise description of the problem
- for some projects custom block code is not the right way
- if we want place locale files in separate directory, we need to use “global” access, so everything is in one file per locale, example (these files can really grow in size and be really unclear):
locales/en.yaml
locales/fr.yaml
- if we split locale translation files into multiple files, import all locales at once is not working, because its not correctly merged
Suggested solution
- add possibility to split each locale to multiple files, so our structure can look like this:
locales/en/group1/subject1.yaml
locales/en/group1/subject2.yaml
locales/en/group2/subject3.yaml
locales/fr/group1/subject1.yaml
locales/fr/group1/subject2.yaml
locales/fr/group2/subject3.yaml
There are to possibilities how to achieve this:
- more complex way - first directory will define the locale and each directory is a subject path, so if
locales/en/group1/subject1.yaml
content is this:
foo: foo
bar:
baz: baz
the “generated” file for en locale will be:
group1:
subject1:
foo: foo
bar:
baz: baz
- more simple way - just merge the content, use root subject as locale - we will need to define whole subject in each file, the plugin will just merge everything in one
locales/en/group1/subject1.yaml
en:
group1:
subject1:
foo: foo
bar:
baz: baz
locales/fr/group1/subject1.yaml
fr:
group1:
subject1:
foo: foo
bar:
baz: baz
Alternative
If there is any existing way how to achieve similar code spliting right now without need to implement it, please add it to documentation, because now there is nothing about it.
Additional context
I tried several ways how to structure the dirs, files and content of yaml/json files and nothing was correctly merged, always only one file worked and other were ignored.
Validations
- Read the Contributing Guidelines.
- Read the README
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:9
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Can locales files be splitting? · Issue #720 · formatjs ... - GitHub
Hello, i am adding react-intl to a big application , the translation file would be huge and also it is redundant to load...
Read more >Code-splitting react-i18n locales using dynamic imports
A simple react-i18n to code-split locale files using dynamic imports. npm install npm run serve // Open… github.com. Photo by Tim Mossholder on...
Read more >Multiple Translation Files - react-i18next documentation
One of the advantages of react-i18next is based on i18next it supports the separation of translations into multiple files - which are called...
Read more >using multiple translation files in aurelia i18N - Stack Overflow
I would like to split translation.json file into multiple files like nav.json, message.json, etc but I am not sure how to do it....
Read more >The Ultimate Vue Localization Guide | Phrase
loadMessagesFor () uses Webpack's async code splitting and dynamic imports to asynchronously load the translation file for the given locale. Once ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The yaml parser for
import.meta.glob
behaves poorly.Specifically, the following YAML fails to parse (checked with YAML Lint and eslint-plugin-yaml):
Therefore, I implemented parsing with js-yaml.
index.yml is assigned to root.
It’s not very clean code, but I think it will work.
I use ts and vite import to manage all translation files. So, I can do a lots of things.
//configuration/locale.ts export const supportedLocales = { en: { name: “English” }, “zh-CN”: { name: “中文简体” }, “zh-TW”: { name: “中文繁體” }, };
export const defaultLocale = “en”;
//locale/index.ts import { supportedLocales } from “@/configuration/locale”; import { merge } from “lodash”;
const autoImportedLangs: Record<string, () => Promise<any>> = import.meta.glob( [“./.ts", "/src/components/**/lang/.ts”], { import: “default”, eager: true, } );
for (const path in autoImportedLangs) { const lang: string = path.substring( path.lastIndexOf(“/”) + 1, path.lastIndexOf(“.”) ); if (lang in usedLangs) { merge(usedLangs[lang], autoImportedLangs[path]); } }
export default usedLangs;
install the plugin:
import { createI18n } from “vue-i18n”; import { defaultLocale } from “@/configuration/locale”; import messages from “@/locales”;
export default createI18n({ locale: defaultLocale, fallbackWarn: false, missingWarn: false, legacy: false, fallbackLocale: defaultLocale, messages, });