Skip to content

Commit

Permalink
Merge pull request #387 from chrismayer/i18n-module
Browse files Browse the repository at this point in the history
Refactor VueI18n creation to own module
  • Loading branch information
chrismayer authored May 6, 2024
2 parents a165074 + d28ef9f commit 35bf846
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
18 changes: 18 additions & 0 deletions src/locales/wgu-i18n.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Creates the VueI18n object used for internationalization.
*/
import Vue from 'vue';
import VueI18n from 'vue-i18n';
import LocaleUtil from '@/util/Locale';

Vue.use(VueI18n);

const appConfig = Vue.prototype.$appConfig;

const preset = {
locale: LocaleUtil.getPreferredLanguage(appConfig),
fallbackLocale: LocaleUtil.getFallbackLanguage(appConfig),
messages: LocaleUtil.importVueI18nLocales()
};

export const i18n = new VueI18n(preset);
25 changes: 6 additions & 19 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import Vue from 'vue';
import Vuetify from 'vuetify/lib/framework';
import PortalVue from 'portal-vue'
import VueI18n from 'vue-i18n';
import 'roboto-fontface/css/roboto/roboto-fontface.css'
import '@mdi/font/css/materialdesignicons.css'
import 'material-icons/iconfont/material-icons.css'
Expand All @@ -17,7 +16,6 @@ import axios from 'axios';

Vue.use(Vuetify);
Vue.use(PortalVue);
Vue.use(VueI18n);

require('./assets/css/wegue.css');

Expand Down Expand Up @@ -62,21 +60,6 @@ const createVuetify = function (appConfig) {
return new Vuetify(preset);
}

/**
* Creates the VueI18n object used for internationalization.
*
* @param {Object} appConfig Global application context.
* @returns The active I18n instance.
*/
const createVueI18n = function (appConfig) {
const preset = {
locale: LocaleUtil.getPreferredLanguage(appConfig),
fallbackLocale: LocaleUtil.getFallbackLanguage(appConfig),
messages: LocaleUtil.importVueI18nLocales()
};
return new VueI18n(preset);
}

/**
* Backwards compatibility layer for legacy features in app-conf.json.
*
Expand Down Expand Up @@ -181,13 +164,17 @@ const migrateAppConfig = function (appConfig) {
*
* @param {Object} appConfig Global application context.
*/
const createApp = function (appConfig) {
const createApp = async function (appConfig) {
// make app config accessible for all components
Vue.prototype.$appConfig = migrateAppConfig(appConfig);

// dynamic import, otherwise Vue.prototype.$appConfig won't be set yet on
// static import
const { i18n } = await import('./locales/wgu-i18n.js');

new Vue({
vuetify: createVuetify(appConfig),
i18n: createVueI18n(appConfig),
i18n,
render: h => h(WguApp)
}).$mount('#app');
};
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/specs/util/Locale.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import LocaleUtil from '@/util/Locale'
import Vue from 'vue';

const appConfig = {
lang: {
Expand All @@ -11,6 +12,8 @@ const appConfig = {
}
};

Vue.prototype.$appConfig = appConfig;

describe('LocaleUtil', () => {
it('is defined', () => {
expect(typeof LocaleUtil).to.not.equal(undefined);
Expand Down

0 comments on commit 35bf846

Please sign in to comment.