xref: /openbmc/webui-vue/src/i18n.js (revision f833c7e3)
1import Vue from 'vue';
2import VueI18n from 'vue-i18n';
3
4Vue.use(VueI18n);
5
6function loadLocaleMessages() {
7  const locales = require.context(
8    './locales',
9    true,
10    /[A-Za-z0-9-_,\s]+\.json$/i
11  );
12  const messages = {};
13  locales.keys().forEach((key) => {
14    const matched = key.match(/([A-Za-z0-9-_]+)\./i);
15    if (matched && matched.length > 1) {
16      const locale = matched[1];
17      messages[locale] = locales(key);
18    }
19  });
20  return messages;
21}
22
23export default new VueI18n({
24  // Get default locale from local storage
25  locale: window.localStorage.getItem('storedLanguage'),
26  // Locales that don't exist will fallback to English
27  fallbackLocale: 'en-US',
28  // Falling back to fallbackLocale generates two console warnings
29  // Silent fallback suppresses console warnings when using fallback
30  silentFallbackWarn: true,
31  messages: loadLocaleMessages(),
32});
33