import type { VueI18n } from 'vue-i18n' import type { CountryCode } from 'libphonenumber-js' import { parsePhoneNumber } from 'libphonenumber-js' import type { EnumChoice, EnumChoices } from '~/types/interfaces' import ArrayUtils from '~/services/utils/arrayUtils' export default 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.label) as string } }) if (sort) { enum_ = ArrayUtils.sortObjectsByProp(enum_, 'label') as EnumChoices } return enum_ } public formatPhoneNumber( number: string, defaultCountry?: CountryCode, ): string { const parsed = parsePhoneNumber(number, defaultCountry) return parsed ? parsed.formatNational() : '' } }