-
-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attempt at exporting individual modules
- Loading branch information
Showing
16 changed files
with
203 additions
and
71 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 @@ | ||
export * from './client/index'; |
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 @@ | ||
export {default} from './link/index'; |
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 @@ | ||
export {default} from './middleware/index'; |
119 changes: 119 additions & 0 deletions
119
packages/next-intl/src/react-server/getBaseTranslator.tsx
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,119 @@ | ||
import {ReactElement, ReactNodeArray, cache} from 'react'; | ||
import { | ||
Formats, | ||
TranslationValues, | ||
RichTranslationValues, | ||
MessageKeys, | ||
NamespaceKeys, | ||
NestedKeyOf, | ||
NestedValueOf, | ||
createBaseTranslator | ||
} from 'use-intl/core'; | ||
import getConfig from '../server/getConfig'; | ||
|
||
let hasWarned = false; | ||
|
||
async function getTranslatorImpl< | ||
NestedKey extends NamespaceKeys< | ||
IntlMessages, | ||
NestedKeyOf<IntlMessages> | ||
> = never | ||
>( | ||
locale: | ||
| string | ||
| { | ||
namespace?: NestedKey; | ||
locale: string; | ||
}, | ||
namespace?: NestedKey | ||
): // Explicitly defining the return type is necessary as TypeScript would get it wrong | ||
Promise<{ | ||
// Default invocation | ||
< | ||
TargetKey extends MessageKeys< | ||
NestedValueOf< | ||
{'!': IntlMessages}, | ||
[NestedKey] extends [never] ? '!' : `!.${NestedKey}` | ||
>, | ||
NestedKeyOf< | ||
NestedValueOf< | ||
{'!': IntlMessages}, | ||
[NestedKey] extends [never] ? '!' : `!.${NestedKey}` | ||
> | ||
> | ||
> | ||
>( | ||
key: TargetKey, | ||
values?: TranslationValues, | ||
formats?: Partial<Formats> | ||
): string; | ||
|
||
// `rich` | ||
rich< | ||
TargetKey extends MessageKeys< | ||
NestedValueOf< | ||
{'!': IntlMessages}, | ||
[NestedKey] extends [never] ? '!' : `!.${NestedKey}` | ||
>, | ||
NestedKeyOf< | ||
NestedValueOf< | ||
{'!': IntlMessages}, | ||
[NestedKey] extends [never] ? '!' : `!.${NestedKey}` | ||
> | ||
> | ||
> | ||
>( | ||
key: TargetKey, | ||
values?: RichTranslationValues, | ||
formats?: Partial<Formats> | ||
): string | ReactElement | ReactNodeArray; | ||
|
||
// `raw` | ||
raw< | ||
TargetKey extends MessageKeys< | ||
NestedValueOf< | ||
{'!': IntlMessages}, | ||
[NestedKey] extends [never] ? '!' : `!.${NestedKey}` | ||
>, | ||
NestedKeyOf< | ||
NestedValueOf< | ||
{'!': IntlMessages}, | ||
[NestedKey] extends [never] ? '!' : `!.${NestedKey}` | ||
> | ||
> | ||
> | ||
>( | ||
key: TargetKey | ||
): any; | ||
}> { | ||
if (typeof locale === 'object') { | ||
const opts = locale; | ||
namespace = opts.namespace; | ||
locale = opts.locale; | ||
if (!hasWarned) { | ||
console.warn( | ||
` | ||
DEPRECATION WARNING: Calling \`getTranslator\` with an object argument is deprecated, please update your call site accordingly. | ||
// Previously | ||
getTranslator({locale: 'en', namespace: 'About'}); | ||
// Now | ||
getTranslator('en', 'About'); | ||
See also https://next-intl-docs.vercel.app/docs/environments/metadata-route-handlers | ||
` | ||
); | ||
hasWarned = true; | ||
} | ||
} | ||
|
||
const config = await getConfig(locale); | ||
return createBaseTranslator({ | ||
...config, | ||
namespace, | ||
messages: config.messages | ||
}); | ||
} | ||
|
||
export default cache(getTranslatorImpl); |
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,15 +1,17 @@ | ||
import type {useTranslations as useTranslationsType} from 'use-intl'; | ||
import getTranslator from '../server/getTranslator'; | ||
import getBaseTranslator from './getBaseTranslator'; | ||
import useHook from './useHook'; | ||
import useLocale from './useLocale'; | ||
|
||
export default function useTranslations( | ||
...[namespace]: Parameters<typeof useTranslationsType> | ||
): ReturnType<typeof useTranslationsType> { | ||
const locale = useLocale(); | ||
const result = useHook('useTranslations', getTranslator(locale, namespace)); | ||
|
||
// The types are slightly off here and indicate that rich text formatting | ||
// doesn't integrate with React - this is not the case. | ||
return result as any; | ||
const result = useHook( | ||
'useTranslations', | ||
getBaseTranslator(locale, namespace) | ||
); | ||
|
||
return result; | ||
} |
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 @@ | ||
export * from './server/index'; |
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
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 |
---|---|---|
@@ -1,16 +1,22 @@ | ||
export type {default as AbstractIntlMessages} from './AbstractIntlMessages'; | ||
export type { | ||
default as TranslationValues, | ||
RichTranslationValues | ||
RichTranslationValues, | ||
RichTranslationValuesPlain | ||
} from './TranslationValues'; | ||
export type {default as Formats} from './Formats'; | ||
export type {default as IntlConfig} from './IntlConfig'; | ||
export type {default as DateTimeFormatOptions} from './DateTimeFormatOptions'; | ||
export type {default as NumberFormatOptions} from './NumberFormatOptions'; | ||
export {default as IntlError, IntlErrorCode} from './IntlError'; | ||
export {default as createTranslator} from './createTranslator'; | ||
export {default as createBaseTranslator} from './createBaseTranslator'; | ||
export {default as createFormatter} from './createFormatter'; | ||
export {default as initializeConfig} from './initializeConfig'; | ||
export {default as MessageKeys} from './utils/MessageKeys'; | ||
export {default as NamespaceKeys} from './utils/NamespaceKeys'; | ||
export {default as NestedKeyOf} from './utils/NestedKeyOf'; | ||
export {default as NestedValueOf} from './utils/NestedValueOf'; | ||
|
||
// TODO: Remove in next major version | ||
export {default as createIntl} from './createIntl'; |
Oops, something went wrong.