Skip to content

Commit

Permalink
docs: enhance localization docs
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed Nov 28, 2019
1 parent d20ea0e commit e62f55e
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
- [Migration](#migration)
- [New DNB font](#new-dnb-font) **(major change)**
- [Color changes](#color-changes) **(feature and major change)**
- [Icon changes](#icon-changes) **(feature)**
- [New Icons](#new-icons) **(feature)**
- [Localization](#localization) **(feature)**
- [Other features](#other-features) **(features)**
- [Resources package](#resources-package) **(feature)**
- [How to Install](#install)

Expand Down Expand Up @@ -83,6 +85,10 @@ import {
} from 'dnb-ui-lib/icons/secondary'
```

## Localization

With v6 the `dnb-ui-lib` has it's own localization to be used both for component translation and your app strings. Read [more about how to use localization](/uilib/usage/customisation/localization).

## Other features

- [FormLabel](/uilib/components/form-label) got a new prop `sr_only`. This way you still can provide a **label**, but available only for screen readers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ You can send down the `locale` as an application wide property (Context). More i
import Provider from 'dnb-ui-lib/shared/Provider'

render(
<Provider locale="en" currency_display="code">
<YourApp>
<Provider locale="en-US" currency_display="code">
<MyApp>
text <Number>123</Number> table etc.
</YourApp>
</MyApp>
</Provider>
)
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ draft: true

## Changing locale or currency

You can either set the locale as a properly e.g. `<Provider locale="en" ...` and handle the change from the app root, or change it inside the app, respective Context:
You can either set the locale as a properly e.g. `<Provider locale="en-US" ...` and handle the change from the app root, or change it inside the app, respective Context:

```jsx
import Provider from 'dnb-ui-lib/shared/Provider'
Expand All @@ -14,7 +14,7 @@ const ChangeLocale = () => {
const { update, locale } = React.useContext(Context)

// Change the locale
update({ locale: 'en' })
update({ locale: 'en-US' })

// Change the default currency
update({ currency: 'USD' })
Expand All @@ -24,10 +24,10 @@ const ChangeLocale = () => {

render(
<Provider>
<YourApp>
<MyApp>
<ChangeLocale />
text <Number>123</Number> table etc.
</YourApp>
</MyApp>
</Provider>
)
```
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ For details, have a look at the submenu for all the customizations topics:
- [Polyfill](/uilib/usage/customisation/styling/polyfill)
- [Colors](/uilib/usage/customisation/colors)
- [Theming](/uilib/usage/customisation/theming)
- [Theming](/uilib/usage/customisation/theming)
- [Locale / Translation](/uilib/usage/customisation/localization)

## Favicon and manifest

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
---
title: 'Locale / Translation'
draft: false
order: 8
---

# Localization

The default constants are defined in the `/shared/defaults.js` file.

- The default locale of all components texts is: `nb-NO`.
- The default currency is: `NOK`

## How set the locale

In React based apps, use the shared provider:

```jsx
import Provider from 'dnb-ui-lib/shared/Provider'

render(
<Provider locale="en-US">
<MyApp>Eufemia components</MyApp>
</Provider>
)
```

## How change the locale

You can even change the locale during runtime.

```jsx
import Provider from 'dnb-ui-lib/shared/Provider'
import Context from 'dnb-ui-lib/shared/Context'

const ChangeLocale = () => {
const { setLocale, locale } = React.useContext(Context)

React.useEffect(() => {
setLocale('en-US')
}, [])

return <>{/* e.g. a Dropdown */}</>
}

render(
<Provider ...>
<MyApp>
...
<ChangeLocale />
...
</MyApp>
</Provider>
)
```

## How add your own strings

It is possible to use the Eufemia shared Provider for your own project / App localization.

```js
import Provider from 'dnb-ui-lib/shared/Provider'
import enUS from 'dnb-ui-lib/shared/locales/en-US''
const myLocale = {
...enUS,
// and extend the translation
'en-US': {
myString: 'Custom string'
myGroup: {
subString: 'Second string'
}
}
}
render(
<Provider locales={myLocale} locale="en-US">
<MyApp>
<MyComponent />
</MyApp>
</Provider>
)
```
... and consume the strings in your components, like **MyComponent**:
```jsx
import Context from 'dnb-ui-lib/shared/Context'
export default function MyComponent() {
const { translation } = React.useContext(Context)
return <>{translation.myString}</>
}
```
## How to handle locales
Create a new file containing all the strings:
```js
export default {
'sv-SE': {
DatePicker: {
submit_button_text: 'OK'
},
Modal: {
close_title: 'Stänga'
},
Logo: {
alt: 'DNB Logo'
}
}
}
```
And add the file, like so:
```jsx
import Provider from 'dnb-ui-lib/shared/Provider'
import myLocale from './locales/sv-SE'
render(
<Provider locales={myLocale}>
<MyApp>Eufemia components</MyApp>
</Provider>
)
```
or add it on the fly:
```jsx
import Provider from 'dnb-ui-lib/shared/Provider'
import Context from 'dnb-ui-lib/shared/Context'
import myLocale from './locales/sv-SE'
const ChangeLocale = () => {
const { update, locale } = React.useContext(Context)
// Add new locales
update({ locales: myLocale, locale: 'sv-SE' })
return locale
}
render(
<Provider>
<MyApp>
...
<ChangeLocale />
...
</MyApp>
</Provider>
)
```
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ import { StyleSheetManager } from 'styled-components'

render(
<StyleSheetManager stylisPlugins={[stylisPlugin]}>
<YourApp />
<MyApp />
</StyleSheetManager>
)
```
Expand All @@ -94,7 +94,7 @@ const stylisPlugin = withProperties({

render(
<StyleSheetManager stylisPlugins={[stylisPlugin]}>
<YourApp />
<MyApp />
</StyleSheetManager>
)
```
Expand All @@ -112,7 +112,7 @@ const emotionCache = createEmotionCache({

render(
<CacheProvider value={emotionCache}>
<YourApp />
<MyApp />
</CacheProvider>
)
```

0 comments on commit e62f55e

Please sign in to comment.