Skip to content

Commit

Permalink
feat(Provider): deprecate locales in favour of translations
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed Apr 8, 2024
1 parent 302f4d6 commit 4f218c7
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ type CustomLocales = keyof typeof customTranslation
type CustomTranslation = (typeof customTranslation)[CustomLocales]

render(
<Provider locales={customTranslation} locale="en-GB">
<Provider translations={customTranslation} locale="en-GB">
<MyApp>
<MyComponent />
</MyApp>
Expand Down Expand Up @@ -284,7 +284,7 @@ import Provider from '@dnb/eufemia/shared/Provider'
import customTranslation from './locales/sv-SE'

render(
<Provider locales={customTranslation}>
<Provider translations={customTranslation}>
<MyApp>Eufemia components</MyApp>
</Provider>,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ describe('Password component', () => {
}

render(
<Provider locales={tr}>
<Provider translations={tr}>
<Password />
</Provider>
)
Expand Down
24 changes: 18 additions & 6 deletions packages/dnb-eufemia/src/shared/Context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ export type ContextProps = ContextComponents & {
/**
* Overwrite existing internal translation strings or define new strings via the Provider
*/
translations?: Locales | TranslationCustomLocales

/**
* @deprecated Use `translations` instead
*/
locales?: Locales | TranslationCustomLocales

// -- For internal use --
Expand Down Expand Up @@ -217,9 +222,10 @@ export type TranslationFlat = Record<
export function prepareContext<Props>(
props: ContextProps = {}
): Props & ContextProps {
const locales: Locales = props.locales
? extend(defaultLocales, props.locales)
: defaultLocales
const locales: Locales =
props.translations || props.locales
? extend(defaultLocales, props.translations || props.locales)
: defaultLocales

if (props.__context__) {
Object.assign(props, props.__context__)
Expand All @@ -242,13 +248,19 @@ export function prepareContext<Props>(
const context = {
// We may use that in future
updateTranslation: (locale, translation) => {
context.translation = context.locales[locale] = translation
context.translation = (context.translations || context.locales)[
locale
] = translation
},
getTranslation: (props) => {
if (props) {
const lang = props.lang || props.locale
if (lang && context.locales[lang] && lang !== key) {
return context.locales[lang]
if (
lang &&
(context.translations || context.locales)[lang] &&
lang !== key
) {
return (context.translations || context.locales)[lang]
}
}
return context.translation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('Provider', () => {
}

const LocalProvider = (props: ProviderProps) => {
return <Provider locales={defaultLocales} {...props} />
return <Provider translations={defaultLocales} {...props} />
}

const ChangeLocale = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('Translation', () => {

it('"getTranslation" should return requested string inside render', () => {
render(
<Provider locales={defaultLocales}>
<Provider translations={defaultLocales}>
<span className="getTranslation">
{getTranslation('other.string', {
foo: 'foo',
Expand All @@ -47,7 +47,7 @@ describe('Translation', () => {

it('"Translation" should return requested string inside render', () => {
render(
<Provider locales={defaultLocales}>
<Provider translations={defaultLocales}>
<span className="Translation">
<Translation id="other.string" foo="foo" bar="bar" max="max" />
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('useTranslation without an ID', () => {
}

const { rerender } = render(
<Provider locales={customTranslation} locale="en-GB">
<Provider translations={customTranslation} locale="en-GB">
<MyComponent />
</Provider>
)
Expand All @@ -131,7 +131,7 @@ describe('useTranslation without an ID', () => {
)

rerender(
<Provider locales={customTranslation} locale="nb-NO">
<Provider translations={customTranslation} locale="nb-NO">
<MyComponent />
</Provider>
)
Expand Down Expand Up @@ -161,7 +161,7 @@ describe('useTranslation without an ID', () => {

const result = renderHook(() => useTranslation(), {
wrapper: ({ children }) => (
<Provider locales={customTranslation} locale="en-GB">
<Provider translations={customTranslation} locale="en-GB">
{children}
</Provider>
),
Expand Down Expand Up @@ -249,7 +249,7 @@ describe('useTranslation with an ID', () => {

it('"useTranslation" should have valid strings inside render', () => {
render(
<Provider locales={defaultLocales}>
<Provider translations={defaultLocales}>
<output>
<RenderGetTranslation />
</output>
Expand All @@ -263,7 +263,7 @@ describe('useTranslation with an ID', () => {

it('should change to requested locale', () => {
render(
<Provider locales={defaultLocales}>
<Provider translations={defaultLocales}>
<output>
<RenderGetTranslation />
</output>
Expand All @@ -284,12 +284,12 @@ describe('useTranslation with an ID', () => {

it('should have valid strings inside render', () => {
render(
<Provider locales={defaultLocales}>
<Provider translations={defaultLocales}>
<span className="root">
<Translation id="other.string" foo="foo" bar="bar" max="max" />
</span>

<Provider locales={nestedLocales}>
<Provider translations={nestedLocales}>
<span className="nested">
<RenderGetTranslation />
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function MyComponent() {

export function CustomTranslation() {
return (
<Provider locales={customTranslation} locale="en-GB">
<Provider translations={customTranslation} locale="en-GB">
<MyComponent />
</Provider>
)
Expand Down

0 comments on commit 4f218c7

Please sign in to comment.