Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly introduce i18n and l10n support #199

Merged
merged 15 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Extract getLoaderConfig function
  • Loading branch information
laymonage committed Oct 19, 2021
commit b57796919a0f5f78316b47b4af0c389bea8134cd
31 changes: 29 additions & 2 deletions lib/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { LoaderConfig } from 'next-translate';
import useTranslation from 'next-translate/useTranslation';
import { useCallback } from 'react';

Expand Down Expand Up @@ -35,6 +36,32 @@ interface GiscusTranslate {
(i18nKey: I18nKeysNoCount, query?: TranslationQuery): string;
}

export const availableLanguages = {
en: 'English',
pl: 'Polish',
ro: 'Romanian',
} as const;

export type AvailableLanguage = keyof typeof availableLanguages;

const availableLocales = Object.keys(availableLanguages);

export function getLoaderConfig(lang: AvailableLanguage, pathname: string): LoaderConfig {
return {
locale: lang,
locales: availableLocales,
loader: false,
defaultLocale: 'en',
pathname,
pages: {
'*': ['common'],
},
async loadLocaleFrom(language, namespace) {
return import(`../locales/${language}/${namespace}.json`).then((m) => m.default);
},
};
}

export const useGiscusTranslation = () => {
const { t, lang } = useTranslation('common');
return { t: t as GiscusTranslate, lang };
Expand All @@ -49,11 +76,11 @@ const dateFormat: Intl.DateTimeFormatOptions = {
timeZoneName: 'short',
};

const dateFormatters = {
const dateFormatters: Record<AvailableLanguage, Intl.DateTimeFormat> = {
en: new Intl.DateTimeFormat('en', dateFormat),
pl: new Intl.DateTimeFormat('pl', dateFormat),
ro: new Intl.DateTimeFormat('ro', dateFormat),
} as const;
};

export const useDateFormatter = () => {
const { lang } = useTranslation('common');
Expand Down
17 changes: 4 additions & 13 deletions pages/widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { env, Theme } from '../lib/variables';
import { getAppAccessToken } from '../services/github/getAppAccessToken';
import { getRepoConfig } from '../services/github/getConfig';
import { I18nDictionary } from 'next-translate';
import { AvailableLanguage, availableLanguages, getLoaderConfig } from '../lib/i18n';

export async function getServerSideProps({ query, res }: GetServerSidePropsContext) {
const origin = (query.origin as string) || '';
Expand Down Expand Up @@ -51,18 +52,8 @@ export async function getServerSideProps({ query, res }: GetServerSidePropsConte
res.setHeader('Content-Security-Policy', `frame-ancestors 'self' ${originsStr};`);
}

const { __lang, __namespaces } = await loadNamespaces({
locale: lang,
locales: ['en', 'pl'],
defaultLocale: 'en',
pathname: '/widget',
pages: {
'*': ['common'],
},
async loadLocaleFrom(language, namespace) {
return import(`../locales/${language}/${namespace}.json`).then((m) => m.default);
},
});
const i18nLoaderConfig = getLoaderConfig(lang as AvailableLanguage, '/widget');
const { __lang, __namespaces } = await loadNamespaces(i18nLoaderConfig);

return {
props: {
Expand All @@ -79,7 +70,7 @@ export async function getServerSideProps({ query, res }: GetServerSidePropsConte
emitMetadata,
theme,
originHost,
lang: __lang,
lang: __lang as AvailableLanguage,
namespaces: __namespaces as Record<string, I18nDictionary>,
},
};
Expand Down