Skip to content

Commit

Permalink
fix: improve failed locale, fallback to en-US
Browse files Browse the repository at this point in the history
  • Loading branch information
felixmosh committed Oct 23, 2024
1 parent 431bfab commit 6ecc79d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
3 changes: 1 addition & 2 deletions packages/ui/src/components/JobCard/Timeline/Timeline.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { formatDistance, isToday, differenceInMilliseconds, format, isSameYear } from 'date-fns';
import enLocale from 'date-fns/locale/en-US';
import { TFunction } from 'i18next';
import React from 'react';
import { useTranslation } from 'react-i18next';
Expand Down Expand Up @@ -57,7 +56,7 @@ const formatDuration = (finishedTs: TimeStamp, processedTs: TimeStamp, t: TFunct
if (durationInSeconds > 5) {
return formatDistance(finishedTs, processedTs, {
includeSeconds: true,
locale: dateFnsLocale || enLocale,
locale: dateFnsLocale,
});
}
if (durationInSeconds >= 1) {
Expand Down
12 changes: 8 additions & 4 deletions packages/ui/src/services/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ import i18n from 'i18next';
import HttpBackend from 'i18next-http-backend';
import * as process from 'process';
import { initReactI18next } from 'react-i18next';
import enLocale from 'date-fns/locale/en-US';

export let dateFnsLocale = undefined;
export let dateFnsLocale = enLocale;
const dateFnsLocaleMap = {
'es-ES': 'es',
'fr-FR': 'fr',
} as const;

async function setDateFnsLocale(lng: string) {
const languageToLoad = dateFnsLocaleMap[lng as keyof typeof dateFnsLocaleMap] || lng;
dateFnsLocale = await import(`date-fns/locale/${languageToLoad}/index.js`).catch((e) =>
console.error(e)
);
dateFnsLocale = await import(`date-fns/locale/${languageToLoad}/index.js`).catch((e) => {
// eslint-disable-next-line no-console
console.info(e);

return enLocale;
});
}

export async function initI18n({ lng, basePath }: { lng: string; basePath: string }) {
Expand Down

0 comments on commit 6ecc79d

Please sign in to comment.