From: Dessalines Date: Sat, 17 Aug 2019 17:22:38 +0000 (-0700) Subject: Fix moment locale issue. X-Git-Url: http://these/git/%22%7Burl%7D/%7B%60%24%7BghostArchiveUrl%7D/static/%7B%7D/readmes/%7BdonateLemmyUrl%7D?a=commitdiff_plain;h=c69f1722ac1400dba8c8a89acccc1eba43823f1f;p=lemmy.git Fix moment locale issue. - Fixes #206 --- diff --git a/ui/src/components/moment-time.tsx b/ui/src/components/moment-time.tsx index 2179499f..83c97fb3 100644 --- a/ui/src/components/moment-time.tsx +++ b/ui/src/components/moment-time.tsx @@ -4,7 +4,7 @@ import * as moment from 'moment'; import 'moment/locale/zh-cn'; import 'moment/locale/fr'; import 'moment/locale/sv'; -import { getLanguage } from '../utils'; +import { getMomentLanguage } from '../utils'; import { i18n } from '../i18next'; interface MomentTimeProps { @@ -20,11 +20,7 @@ export class MomentTime extends Component { constructor(props: any, context: any) { super(props, context); - // Moment doesnt have zh, only zh-cn - let lang = getLanguage(); - if (lang == 'zh') { - lang = 'zh-cn'; - } + let lang = getMomentLanguage(); moment.locale(lang); } diff --git a/ui/src/utils.ts b/ui/src/utils.ts index c6f43c94..c0a62114 100644 --- a/ui/src/utils.ts +++ b/ui/src/utils.ts @@ -170,6 +170,20 @@ export function debounce(func: any, wait: number = 500, immediate: boolean = fal } } -export function getLanguage() { +export function getLanguage(): string { return (navigator.language || navigator.userLanguage); } + +export function getMomentLanguage(): string { + let lang = getLanguage(); + if (lang.startsWith('zh')) { + lang = 'zh-cn'; + } else if (lang.startsWith('sv')) { + lang = 'sv'; + } else if (lang.startsWith('fr')) { + lang = 'fr'; + } else { + lang = 'en'; + } + return lang; +}