12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
- import { reactive } from 'vue'
- import { useReactiveNow } from '#shared/composables/useReactiveNow.ts'
- import * as dates from './dates.ts'
- import { Translator } from './translator.ts'
- import type { TranslationMap } from './translator.ts'
- const reactiveNow = useReactiveNow()
- export class I18N {
- private translator = new Translator()
- t(
- source: string | undefined,
- ...args: Array<string | number | undefined | null | boolean>
- ): string {
- if (typeof source === 'undefined') return ''
- return this.translator.translate(source, ...args)
- }
- // eslint-disable-next-line class-methods-use-this
- locale() {
- return document.documentElement.getAttribute('lang') || 'en-US'
- }
- date(dateString: string): string {
- const template = dates.getDateFormat(this.translator)
- return dates.absoluteDateTime(dateString, template)
- }
- dateTime(dateTimeString: string): string {
- const template = dates.getDateTimeFormat(this.translator)
- return dates.absoluteDateTime(dateTimeString, template)
- }
- dateTimeISO(dateTimeString: string): string {
- return dates.getISODatetime(dateTimeString)
- }
- relativeDateTime(dateTimeString: string, baseDate?: Date): string {
- return dates.relativeDateTime(
- dateTimeString,
- baseDate || reactiveNow.value,
- this.translator,
- )
- }
- setTranslationMap(map: TranslationMap): void {
- this.translator.setTranslationMap(map)
- }
- getDateFormat(): string {
- return dates.getDateFormat(this.translator)
- }
- getDateTimeFormat(): string {
- return dates.getDateTimeFormat(this.translator)
- }
- getTimeFormatType() {
- const time24hour = !this.getDateTimeFormat().includes('P') // P means AM/PM
- return time24hour ? '24hour' : '12hour'
- }
- }
- export const i18n = reactive(new I18N()) as I18N
- declare module '@vue/runtime-core' {
- export interface ComponentCustomProperties {
- i18n: I18N
- $t: I18N['t']
- __(source: string): string
- }
- }
|