| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- import {VueI18n} from "vue-i18n";
- import {EnumChoice, EnumChoices} from "~/types/interfaces";
- class I18nUtils {
- private i18n!: VueI18n
- public constructor(i18n: VueI18n) {
- this.i18n = i18n
- }
- /**
- * Return the given enum with its labels translated
- *
- * If sort is true, the enum is also sorted by its newly translated labels
- *
- * @param enum_
- * @param sort
- */
- public translateEnum(enum_: EnumChoices, sort: boolean = true): EnumChoices {
- enum_ = enum_.map(
- (item: EnumChoice) => {
- return {value: item.value, label: this.i18n.t(item.value) as string}
- }
- )
- if (sort) {
- enum_ = enum_.sort((a, b) => {
- if (a.label > b.label) {
- return 1
- }
- if (a.label < b.label) {
- return -1
- }
- return 0
- })
- }
- return enum_
- }
- }
|