6

I am new to vue and I am using vuetify. I want to add internationalization to my project. I succeeded in that but I am using the standard method in which we make a single file per language, as my project grows it will be very hard to maintain that file. So I want to breakdown that single file into multiple files for a single language. Can anyone suggest how to do that?

Nikhil
  • 175
  • 2
  • 10

2 Answers2

6

This is an old question, but I had the same question and found a solution I want to share. I hope this is helpfull.

My original language file "index.js" looked like this:

export default {
    "ok" : "OK",
    "cancel" : "Abbrechen",
    "back" : "Zurück",
    "yes" : "Ja",
    "no" : "Nein"
    ....
}

I've splitted the content into two new files and named them "mainview.js" and "badges.js". They are in the same directory as the original language file.

My languagefile "index.js" now only imports all other files and looks like this:

// import each language file
import mainview from './mainview'
import badges from './badges'

// add all imported language files
export default {
  ...mainview,
  ...badges,
}
kirschkern
  • 1,197
  • 10
  • 27
3

If you use the plugin (vue add i18n), you'll get a i18n.js file in your src/ folder. Change the line in loadLocaleMessages from:

messages[locale] = locales(key)

to

messages[locale] = {...messages[locale], ...locales(key)}

and you can have multiple files per language, e.g. de.menu.json, de.text.json, etc. The function will just load the translations from all files that start with locale name and end with .json.

I've written a bit longer text here https://medium.com/@robert.spendl/vue-i18n-translations-in-multiple-files-96a116017d89 but it's basically just about changing this one line.

Robert Špendl
  • 136
  • 1
  • 3
  • Litteraly a 2 second fix that solves this perfectly :) Thanks! – Christoffer May 12 '22 at 14:53
  • It does not solve the problem exactly. I cannot know from which file comes the key. If I have title key in 2 different locale.json file, then it overwrites it. – Birol Yılmaz Jan 27 '23 at 14:42
  • @BirolYılmaz, the files need to start with locale (de, tr, etc.) and end with `.json`. But you're right, keys need to be unique, this fix is only for human readability, but in effect it just concatenates content of all files as it was just one huge file. So no duplicate fields. You just have to use keys like "title_first_page", "title_profile" etc. – Robert Špendl Jan 29 '23 at 05:17