-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from aypineau/feat--implement-useTranslation-in…
…to-nextjs-metadata feat/implement typing translation
- Loading branch information
Showing
25 changed files
with
299 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { getTranslationContent } from 'intlayer'; | ||
import type { Metadata } from 'next'; | ||
import type { LocalParams } from 'next-intlayer'; | ||
|
||
export const generateMetadata = ({ | ||
params: { locale }, | ||
}: LocalParams): Metadata => { | ||
return { | ||
title: getTranslationContent<string>( | ||
{ | ||
en: 'page title', | ||
fr: 'titre de la page', | ||
es: 'título de la página', | ||
}, | ||
locale | ||
), | ||
description: getTranslationContent( | ||
{ | ||
en: 'page description', | ||
es: 'descripción de la página', | ||
fr: '', | ||
}, | ||
locale | ||
), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/@intlayer/config/src/defaultValues/internationalization.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
packages/@intlayer/config/src/envVariables/getConfiguration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/@intlayer/config/src/types/index.ts → ...ages/@intlayer/config/src/types/config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// /* eslint-disable */ | ||
// import 'intlayer'; | ||
// import { Locales } from '@intlayer/config'; | ||
// import type { ServerComponentExampleContent as _TxBivAmuhkFBKLSJ03g4 } from '../.intlayer/types/server-component-example.d.ts'; | ||
// import type { PageContent as _PBE9SfN3Jt6Sjy1nlqxG } from '../.intlayer/types/page.d.ts'; | ||
// import type { NestedServerComponentExampleContent as _rBCBdYFsnB0FVPPSPK97 } from '../.intlayer/types/nested-server-component-example.d.ts'; | ||
// import type { LangSwitcherContent as _PNzYE5o0D8MsoRVAp0uu } from '../.intlayer/types/lang-switcher.d.ts'; | ||
// import type { EnumerationContent as _14pNbgQIFpuN7gw8mHyP } from '../.intlayer/types/enumeration.d.ts'; | ||
// import type { ClientComponentExampleContent as _XbCoKLjxSnL8cxPxyS5z } from '../.intlayer/types/client-component-example.d.ts'; | ||
|
||
// export {}; | ||
|
||
// declare module 'intlayer' { | ||
// interface IntlayerDictionaryTypesConnector { | ||
// 'server-component-example': _TxBivAmuhkFBKLSJ03g4; | ||
// page: _PBE9SfN3Jt6Sjy1nlqxG; | ||
// 'nested-server-component-example': _rBCBdYFsnB0FVPPSPK97; | ||
// 'lang-switcher': _PNzYE5o0D8MsoRVAp0uu; | ||
// enumeration: _14pNbgQIFpuN7gw8mHyP; | ||
// 'client-component-example': _XbCoKLjxSnL8cxPxyS5z; | ||
// } | ||
|
||
// enum ConfigLocales { | ||
// ENGLISH = 'en', | ||
// FRENCH = 'fr', | ||
// SPANISH = 'es', | ||
// } | ||
|
||
// interface IConfigLocales<Content> extends Record<ConfigLocales, Content> {} | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
packages/@intlayer/core/src/transpiler/content_transformers/translation/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './translations'; | ||
export * from './translation'; | ||
export * from './getTranslationContent'; | ||
export * from './types'; |
53 changes: 53 additions & 0 deletions
53
packages/@intlayer/core/src/transpiler/content_transformers/translation/translation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { intlayerConfiguration } from '@intlayer/config/client'; | ||
import { NodeType } from '../../../types/index'; | ||
import { getStackTraceInfo } from '../../../utils/getStackTraceInfo'; | ||
import type { CustomizableLanguageContent, TranslationContent } from './types'; | ||
|
||
const { defaultLocale } = intlayerConfiguration.internationalization; | ||
|
||
/** | ||
* Transpile multilingual dictionary content | ||
* | ||
* Usage | ||
* | ||
* translation<string>({ | ||
* "en": "test", | ||
* "fr": "test", | ||
* // ... any other available locale | ||
* }) | ||
* | ||
* translation<number, Locales.ENGLISH>({ | ||
* "en": 1 | ||
* }) | ||
* | ||
* translation<string, Locales.ENGLISH | Locales.FRENCH>({ | ||
* "fr": "test", | ||
* "en": "test" | ||
* }) | ||
* | ||
*/ | ||
const translation = <Content = string>( | ||
content?: CustomizableLanguageContent<Content> | ||
) => { | ||
const stackTraceInfo = getStackTraceInfo(); | ||
|
||
if (typeof content === 'string') { | ||
const result: TranslationContent<Content> = { | ||
nodeType: NodeType.Translation, | ||
...stackTraceInfo, | ||
[defaultLocale]: content, | ||
} as TranslationContent<Content>; | ||
|
||
return result; | ||
} | ||
|
||
const result: TranslationContent<Content> = { | ||
nodeType: NodeType.Translation, | ||
...stackTraceInfo, | ||
...(content as unknown as object), | ||
} as TranslationContent<Content>; | ||
|
||
return result; | ||
}; | ||
|
||
export { translation as t }; |
42 changes: 0 additions & 42 deletions
42
packages/@intlayer/core/src/transpiler/content_transformers/translation/translations.ts
This file was deleted.
Oops, something went wrong.
72 changes: 72 additions & 0 deletions
72
packages/@intlayer/core/src/transpiler/content_transformers/translation/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import type { LocalesValues } from '@intlayer/config/client'; | ||
// @ts-expect-error intlayer declared for module augmentation | ||
import type { IConfigLocales } from 'intlayer'; | ||
import type { NodeType } from '../../../types'; | ||
import type { NoteStackTraceInfo } from '../../../utils/getStackTraceInfo'; | ||
|
||
/** | ||
* If module augmented, it will return the configured locales such as Locales.ENGLISH | Locales.FRENCH | Locales.SPANISH | ... | ||
* If not, it will return never | ||
*/ | ||
export type ConfigLocales = keyof IConfigLocales<unknown>; | ||
|
||
/** | ||
* If module augmented, it will return the configured locales such as Locales.ENGLISH | Locales.FRENCH | Locales.SPANISH | ... | ||
* If not, it will return all available locales such as Locales.ENGLISH | Locales.FRENCH | Locales.SPANISH | ... | ||
*/ | ||
export type CustomLocales = ConfigLocales extends never | ||
? LocalesValues | ||
: ConfigLocales; | ||
|
||
/** | ||
* Record of locales and content | ||
* | ||
* const myVar1: LanguageContent<string> = { | ||
* "en": "", | ||
* "fr": "" | ||
* } | ||
* | ||
* const myVar2: LanguageContent<{age: number, name: string}> = { | ||
* "en": {age: 1, name: "test"}, | ||
* "fr": {age: 1, name: "test"} | ||
* } | ||
*/ | ||
export type LanguageContent<Content> = Partial<Record<LocalesValues, Content>>; | ||
export type ConfigLanguageContent<Content> = Record<ConfigLocales, Content>; | ||
|
||
/** | ||
* Valid | ||
* const test: CustomizableLanguageContent<string, Locales.ENGLISH | Locales.FRENCH> = { | ||
* "en": "test", | ||
* "fr": "test" | ||
* } | ||
* | ||
* const test: CustomizableLanguageContent<number> = { | ||
* "fr": 1, | ||
* "en": 1, | ||
* ... any other available locale | ||
* } | ||
* | ||
* Invalid | ||
* | ||
* const test: CustomizableLanguageContent<string> = { | ||
* "en": "test", | ||
* "fr": "test", | ||
* "sss": "test" // Does not exist in Locales | ||
* } | ||
* | ||
* const test: CustomizableLanguageContent<string, Locales.ENGLISH | Locales.FRENCH> = { | ||
* "fr": "test" | ||
* // Locales.ENGLISH is missing | ||
* } | ||
* | ||
*/ | ||
export type CustomizableLanguageContent<Content = string> = | ||
ConfigLocales extends never | ||
? LanguageContent<Content> | ||
: ConfigLanguageContent<Content>; | ||
|
||
export type TranslationContent<Content> = LanguageContent<Content> & | ||
NoteStackTraceInfo & { | ||
nodeType: NodeType.Translation; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1 @@ | ||
export type { | ||
LanguageContent, | ||
TranslationContent, | ||
EnumerationContent, | ||
QuantityContent, | ||
} from './content_transformers/index'; | ||
export { t, enu } from './content_transformers/index'; | ||
export * from './content_transformers/index'; |
Oops, something went wrong.