Skip to content

Commit

Permalink
Introduce global app language lookup
Browse files Browse the repository at this point in the history
This introduces a global application language lookup, which allows to
access the current language in a static manner from normal JS-/ES-
resources, without being in a Vue component.
  • Loading branch information
chrismayer committed Nov 9, 2023
1 parent e5f69df commit ce313b8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
16 changes: 12 additions & 4 deletions app-starter/WguAppTemplate.vue
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export default {
}
},
created () {
this.setGlobalAppLang(); // initially set global app language lookup
this.setDocumentTitle();
},
mounted () {
Expand Down Expand Up @@ -180,17 +181,24 @@ export default {
return moduleWins;
},
/**
* Sets the document title from language file.
*/
* Sets the document title from language file.
*/
setDocumentTitle () {
document.title = this.$t('app.browserTitle') || document.title;
},
/**
* Sets the current i18n language to the global app language lookup.
*/
setGlobalAppLang () {
Vue.prototype.appLanguage = this.$i18n.locale;
}
},
watch: {
/**
* Watch for locale changes.
*/
* Watch for locale changes.
*/
'$i18n.locale': function () {
this.setGlobalAppLang();
this.setDocumentTitle();
}
}
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/specs/WguAppTemplate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,26 @@ describe('WguAppTpl.vue', () => {
expect(moduleData[0].type).to.equal('wgu-infoclick-win');
expect(moduleData[0].target).to.equal('menu');
});

it('has a method setGlobalAppLang', () => {
expect(vm.setGlobalAppLang).to.be.a('function');
});
});

describe('global app language lookup', () => {
let comp;
let vm;
beforeEach(() => {
comp = shallowMount(WguAppTpl);
vm = comp.vm;
});

it('is set correctly', () => {
expect(vm.$i18n.locale).to.equal(Vue.prototype.appLanguage);
});

afterEach(() => {
comp.destroy();
});
});
});

0 comments on commit ce313b8

Please sign in to comment.