Skip to content

Commit

Permalink
Setup of i18next
Browse files Browse the repository at this point in the history
  • Loading branch information
asimonok committed Oct 23, 2023
1 parent 0475934 commit 172e917
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 21 deletions.
63 changes: 54 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
"@grafana/ui": "^10.1.4",
"@types/react-virtualized-auto-sizer": "^1.0.1",
"dayjs": "^1.11.10",
"i18next": "^23.6.0",
"react": "18.2.0",
"react-big-calendar": "^1.8.4",
"react-dom": "18.2.0",
"react-i18next": "^13.3.1",
"react-virtualized-auto-sizer": "^1.0.20",
"tslib": "^2.6.2"
},
Expand Down
8 changes: 8 additions & 0 deletions src/@types/i18next.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defaultNS, resources } from '../i18n';

declare module 'i18next' {
interface CustomTypeOptions {
defaultNS: typeof defaultNS;
resources: typeof resources.en;
}
}
6 changes: 3 additions & 3 deletions src/constants/default.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CalendarOptions, CalendarType, View } from '../types';
import { CalendarOptions, CalendarType, SupportedLanguage, View } from '../types';
import { AnnotationsType, Colors } from './options';

/**
* Language
* Default Language
*/
export const DefaultLanguage = 'en-US';
export const DefaultLanguage: SupportedLanguage = 'en';

/**
* Default Options
Expand Down
16 changes: 7 additions & 9 deletions src/hooks/useLocalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import zhLocale from 'dayjs/locale/zh';
import { useEffect, useMemo, useState } from 'react';
import { dayjsLocalizer } from 'react-big-calendar';
import { getLocaleData } from '@grafana/data';
import { config } from '@grafana/runtime';
import { DefaultLanguage, LanguageMessages } from '../constants';

import { LanguageMessages } from '../constants';
import { getUserLanguage } from '../utils';
/**
* Dayjs locales per each grafana language
* Dynamic import is not needed until there is too many locales
Expand All @@ -28,13 +27,12 @@ const dayjsLocales = {
*/
export const useLocalizer = () => {
const localeDate = getLocaleData();
const language = config.bootData.user.language || DefaultLanguage;
const localeName = language.split('-')[0];
const language = getUserLanguage();
const [dayjsLocale, setDayjsLocale] = useState(dayjsLocales.en);

useEffect(() => {
setDayjsLocale(dayjsLocales[localeName as keyof typeof dayjsLocales] || dayjsLocales.en);
}, [localeName]);
setDayjsLocale(dayjsLocales[language as keyof typeof dayjsLocales] || dayjsLocales.en);
}, [language]);

return useMemo(() => {
/**
Expand All @@ -56,6 +54,6 @@ export const useLocalizer = () => {
(localizer.formats as any).yearWeekFormat = 'dd';
(localizer.formats as any).yearDateFormat = 'D';

return { localizer, messages: LanguageMessages[localeName] };
}, [dayjsLocale, localeDate, localeName]);
return { localizer, messages: LanguageMessages[language] };
}, [dayjsLocale, localeDate, language]);
};
24 changes: 24 additions & 0 deletions src/i18n/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import i18next from 'i18next';
import { initReactI18next } from 'react-i18next';
import { resources } from './translations';
import { getUserLanguage } from '../utils';

/**
* Default Namespace
*/
export const defaultNS = 'translation';

/**
* Init i18next
*/
i18next.use(initReactI18next).init({
lng: getUserLanguage(),
debug: true,
resources,
defaultNS,
});

/**
* I18Next instance
*/
export { i18next };
9 changes: 9 additions & 0 deletions src/i18n/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Formats
*/
export const Formats = {
dateWithTime: {
dateStyle: 'short',
timeStyle: 'medium',
},
};
3 changes: 3 additions & 0 deletions src/i18n/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './config';
export * from './constants';
export * from './translations';
1 change: 1 addition & 0 deletions src/i18n/translations/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
16 changes: 16 additions & 0 deletions src/i18n/translations/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import en from './en.json';

/**
* Translation Resources
*/
export const resources = {
/**
* English
*/
en: {
/**
* Translation
*/
translation: en,
},
} as const;
1 change: 1 addition & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Field, FieldConfigProperty, FieldType, PanelPlugin } from '@grafana/data';
import './i18n';
import { CalendarPanel, MultiFieldEditor } from './components';
import {
AnnotationsOptions,
Expand Down
5 changes: 5 additions & 0 deletions src/types/panel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { AnnotationsType, Colors } from '../constants';
import { CalendarType, View } from './calendar';

/**
* Supported Language
*/
export type SupportedLanguage = 'en' | 'es' | 'fr' | 'de' | 'zh';

/**
* Calendar Options
*/
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './annotations';
export * from './hooks';
export * from './time';
export * from './calendarEvents';
export * from './locale';
29 changes: 29 additions & 0 deletions src/utils/locale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { config } from '@grafana/runtime';
import { DefaultLanguage } from '../constants';
import { SupportedLanguage } from '../types';

/**
* Get User Language
* @param fallback
*/
export const getUserLanguage = (fallback = DefaultLanguage): SupportedLanguage => {
const locale = config.bootData.user.language;
const lang = locale?.split('-')?.[0];

/**
* Validate supported languages
*/
switch (lang) {
case 'en':
case 'es':
case 'fr':
case 'de':
case 'zh': {
return lang;
}

default: {
return fallback;
}
}
};

0 comments on commit 172e917

Please sign in to comment.