I get a translated json-file with
const skillsJson = require("./locales/" + localStorage.lang + "/skills.json");
and it works perfectly fine when I run it with npm run serve.
But after building the project using npm run build I get this error in the console:
Uncaught Error: Cannot find module './undefined/skills.json'
at i (skills\.json$:13)
at s (skills\.json$:8)
at Module.56d7 (App.vue:225)
at r (bootstrap:79)
at Object.0 (app.5886208a.js:1)
at r (bootstrap:79)
at n (bootstrap:45)
at bootstrap:152
at app.5886208a.js:1
Seems like the local language didn't get found and the fallback language didn't work. But I think my i18n.js file is fine. Here it is:
import Vue from "vue";
import VueI18n from "vue-i18n";
const moment = require("moment");
Vue.use(VueI18n);
function loadLocaleMessages() {
const locales = require.context(
"./locales",
true,
/[A-Za-z0-9-_,\s]+\.json$/i
);
const messages = {};
locales.keys().forEach(key => {
const matched = key.match(/([A-Za-z0-9-_]+)\./i);
if (matched && matched.length > 1) {
const locale = matched[1];
messages[locale] = locales(key);
}
});
return messages;
}
let lang = "de";
if (localStorage.getItem("lang")) {
lang = localStorage.getItem("lang");
} else {
localStorage.setItem("lang", lang);
moment.locale(localStorage.getItem("lang"));
}
export default new VueI18n({
locale: lang,
fallbackLocale: process.env.VUE_APP_I18N_FALLBACK_LOCALE || "de",
messages: loadLocaleMessages()
});
Any guesses on what could be the problem? If you need any additional informations tell me.