From e0c5ec43f5c2a105dc3c626d7c4bc74b512662f4 Mon Sep 17 00:00:00 2001 From: Joe Carstairs <65492573+Sycamost@users.noreply.github.com> Date: Sat, 12 Aug 2023 17:22:33 +0100 Subject: Parameterises translation --- src/i18n/translate.ts | 17 ++++++++++++++--- src/i18n/translations/pages/furthsettins.ts | 8 ++++---- src/i18n/translations/pages/home.ts | 8 ++++---- src/i18n/translations/site.ts | 20 ++++++++++---------- 4 files changed, 32 insertions(+), 21 deletions(-) (limited to 'src/i18n') diff --git a/src/i18n/translate.ts b/src/i18n/translate.ts index b407e3b..b911547 100644 --- a/src/i18n/translate.ts +++ b/src/i18n/translate.ts @@ -1,9 +1,20 @@ import type Locale from '../types/Locale'; -import type { TranslationsDictionary } from '../types/TranslationsDictionary'; +import type { TranslationsDictionary, Translation } from '../types/TranslationsDictionary'; import { defaultLocale } from './locales'; +type TranslationsDictionaryWith = TranslationsDictionary<{ + [key in Key]: { [locale in typeof defaultLocale]: Translation } & { + [locale in Locale]?: Translation; + }; +}>; + export default function (locale: Locale) { - return function (dict: TranslationsDictionary, key: T) { - return dict[key][locale] ?? dict[key][defaultLocale]; + return function < + Key extends string, + Params, + Dict extends TranslationsDictionaryWith, + >(dict: Dict, params: { key: Key } & { [Param in keyof Params]: string }) { + const translation = dict[params.key][locale] ?? dict[params.key][defaultLocale]; + return translation(params); }; } diff --git a/src/i18n/translations/pages/furthsettins.ts b/src/i18n/translations/pages/furthsettins.ts index 41da32b..4b87311 100644 --- a/src/i18n/translations/pages/furthsettins.ts +++ b/src/i18n/translations/pages/furthsettins.ts @@ -2,10 +2,10 @@ import type { TranslationsDictionary } from '../../../types/TranslationsDictiona const furthsettinsTranslations = { title: { - sco: 'Furthsettins', - 'en-GB': 'Publications', + sco: () => 'Furthsettins', + 'en-GB': () => 'Publications', }, }; -type Keys = keyof typeof furthsettinsTranslations; -export default furthsettinsTranslations as TranslationsDictionary; +type Raw = typeof furthsettinsTranslations; +export default furthsettinsTranslations as TranslationsDictionary; diff --git a/src/i18n/translations/pages/home.ts b/src/i18n/translations/pages/home.ts index 0dff80d..dda30b8 100644 --- a/src/i18n/translations/pages/home.ts +++ b/src/i18n/translations/pages/home.ts @@ -2,10 +2,10 @@ import type { TranslationsDictionary } from '../../../types/TranslationsDictiona const homeTranslations = { title: { - sco: 'Hame', - 'en-GB': 'Home', + sco: () => 'Hame', + 'en-GB': () => 'Home', }, }; -type Keys = keyof typeof homeTranslations; -export default homeTranslations as TranslationsDictionary; +type Raw = typeof homeTranslations; +export default homeTranslations as TranslationsDictionary; diff --git a/src/i18n/translations/site.ts b/src/i18n/translations/site.ts index 89e9ed5..b414499 100644 --- a/src/i18n/translations/site.ts +++ b/src/i18n/translations/site.ts @@ -2,22 +2,22 @@ import type { TranslationsDictionary } from '../../types/TranslationsDictionary' const siteTranslations = { title: { - sco: 'Scots Leid Associe', - 'en-GB': 'Scots Language Society', + sco: () => 'Scots Leid Associe', + 'en-GB': () => 'Scots Language Society', }, description: { - sco: 'The wabsteid o the Scots Leid Associe', - 'en-GB': 'The website of the Scots Language Society', + sco: () => 'The wabsteid o the Scots Leid Associe', + 'en-GB': () => 'The website of the Scots Language Society', }, lallans: { - sco: 'Lallans', - 'en-GB': 'Lallans', + sco: () => 'Lallans', + 'en-GB': () => 'Lallans', }, by: { - sco: 'bi', - 'en-GB': 'by', + sco: () => 'bi', + 'en-GB': () => 'by', }, }; -type Keys = keyof typeof siteTranslations; -export default siteTranslations as TranslationsDictionary; +type Raw = typeof siteTranslations; +export default siteTranslations as TranslationsDictionary; -- cgit v1.2.3