Files
bsky-social-app/src/components/hooks/dates.ts
T
Yiannis Mertzanis 2300410b4d Add Greek Language ('el') Support and Internationalization (#6677)
* Add Greek language support to the application

* Add support for Greek language in date handling and tests

* Update Greek locale file with translator and team information

* Add support for Greek language in date handling and tests

* Add support for Greek language in app configuration, and updated translation from the reviewers' comments

* Update src/locale/i18n.ts

Co-authored-by: surfdude29 <149612116+surfdude29@users.noreply.github.com>

* Update src/locale/i18n.ts

Co-authored-by: surfdude29 <149612116+surfdude29@users.noreply.github.com>

---------

Co-authored-by: Paul Frazee <pfrazee@gmail.com>
Co-authored-by: surfdude29 <149612116+surfdude29@users.noreply.github.com>
2024-12-31 12:28:17 -08:00

97 lines
1.4 KiB
TypeScript

/**
* Hooks for date-fns localized formatters.
*
* Our app supports some languages that are not included in date-fns by
* default, in which case it will fall back to English.
*
* {@link https://github.com/date-fns/date-fns/blob/main/docs/i18n.md}
*/
import React from 'react'
import {formatDistance, Locale} from 'date-fns'
import {
ca,
de,
el,
enGB,
es,
fi,
fr,
gl,
hi,
hu,
id,
it,
ja,
km,
ko,
nl,
pl,
ptBR,
ro,
ru,
th,
tr,
uk,
vi,
zhCN,
zhHK,
zhTW,
} from 'date-fns/locale'
import {AppLanguage} from '#/locale/languages'
import {useLanguagePrefs} from '#/state/preferences'
/**
* {@link AppLanguage}
*/
const locales: Record<AppLanguage, Locale | undefined> = {
en: undefined,
an: undefined,
ast: undefined,
ca,
de,
el,
['en-GB']: enGB,
es,
fi,
fr,
ga: undefined,
gl,
hi,
hu,
id,
it,
ja,
km,
ko,
ne: undefined,
nl,
pl,
['pt-BR']: ptBR,
ro,
ru,
th,
tr,
uk,
vi,
['zh-Hans-CN']: zhCN,
['zh-Hant-HK']: zhHK,
['zh-Hant-TW']: zhTW,
}
/**
* Returns a localized `formatDistance` function.
* {@link formatDistance}
*/
export function useFormatDistance() {
const {appLanguage} = useLanguagePrefs()
return React.useCallback<typeof formatDistance>(
(date, baseDate, options) => {
const locale = locales[appLanguage as AppLanguage]
return formatDistance(date, baseDate, {...options, locale: locale})
},
[appLanguage],
)
}