@commercetools-frontend/l10n 0.0.0-CRAFT-1791-20251006162610
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +377 -0
- package/data/countries/de.json +263 -0
- package/data/countries/en.json +264 -0
- package/data/countries/es.json +263 -0
- package/data/countries/fr-FR.json +263 -0
- package/data/countries/pt-BR.json +263 -0
- package/data/currencies/de.json +650 -0
- package/data/currencies/en.json +650 -0
- package/data/currencies/es.json +650 -0
- package/data/currencies/fr-FR.json +650 -0
- package/data/currencies/pt-BR.json +650 -0
- package/data/languages/de.json +1896 -0
- package/data/languages/en.json +1897 -0
- package/data/languages/es.json +1896 -0
- package/data/languages/fr-FR.json +1896 -0
- package/data/languages/pt-BR.json +1896 -0
- package/data/time-zones/core.json +341 -0
- package/data/time-zones/de.json +341 -0
- package/data/time-zones/en.json +341 -0
- package/data/time-zones/es.json +341 -0
- package/data/time-zones/fr-FR.json +341 -0
- package/data/time-zones/pt-BR.json +341 -0
- package/data/time-zones/translations-map.json +475 -0
- package/dist/commercetools-frontend-l10n.cjs.d.ts +2 -0
- package/dist/commercetools-frontend-l10n.cjs.dev.js +555 -0
- package/dist/commercetools-frontend-l10n.cjs.js +7 -0
- package/dist/commercetools-frontend-l10n.cjs.prod.js +555 -0
- package/dist/commercetools-frontend-l10n.esm.js +510 -0
- package/dist/de-2ff8958f.esm.js +1898 -0
- package/dist/de-389d94e2.cjs.dev.js +1900 -0
- package/dist/de-3b92f2f0.cjs.prod.js +654 -0
- package/dist/de-4a338eae.cjs.prod.js +267 -0
- package/dist/de-636e3d97.cjs.prod.js +1900 -0
- package/dist/de-833edb33.esm.js +343 -0
- package/dist/de-9b912643.cjs.prod.js +345 -0
- package/dist/de-a35429c3.cjs.dev.js +345 -0
- package/dist/de-a846d004.esm.js +652 -0
- package/dist/de-c86b6188.esm.js +265 -0
- package/dist/de-cabb1fa6.cjs.dev.js +654 -0
- package/dist/de-d39b5241.cjs.dev.js +267 -0
- package/dist/declarations/src/country-information.d.ts +12 -0
- package/dist/declarations/src/currency-information.d.ts +15 -0
- package/dist/declarations/src/index.d.ts +7 -0
- package/dist/declarations/src/language-information.d.ts +15 -0
- package/dist/declarations/src/localize.d.ts +74 -0
- package/dist/declarations/src/time-zone-information.d.ts +17 -0
- package/dist/declarations/src/types.d.ts +36 -0
- package/dist/declarations/src/utils.d.ts +10 -0
- package/dist/declarations/src/version.d.ts +2 -0
- package/dist/declarations/supported-locales.d.ts +3 -0
- package/dist/en-114d9c70.esm.js +652 -0
- package/dist/en-2431469e.cjs.prod.js +345 -0
- package/dist/en-261128d4.esm.js +266 -0
- package/dist/en-469854dc.cjs.prod.js +268 -0
- package/dist/en-845c7549.esm.js +1899 -0
- package/dist/en-a33b999a.cjs.prod.js +654 -0
- package/dist/en-c5d0a319.cjs.dev.js +345 -0
- package/dist/en-dd33a580.cjs.dev.js +654 -0
- package/dist/en-e789e674.cjs.dev.js +268 -0
- package/dist/en-e7e85084.cjs.dev.js +1901 -0
- package/dist/en-f1792b3d.cjs.prod.js +1901 -0
- package/dist/en-fb4b590b.esm.js +343 -0
- package/dist/es-260e98bc.esm.js +265 -0
- package/dist/es-32f7bc96.esm.js +1898 -0
- package/dist/es-47ef29f5.esm.js +652 -0
- package/dist/es-790657d9.cjs.dev.js +1900 -0
- package/dist/es-885fd104.cjs.prod.js +267 -0
- package/dist/es-8c811c5d.cjs.dev.js +267 -0
- package/dist/es-8eeea36b.cjs.prod.js +345 -0
- package/dist/es-98d11980.cjs.dev.js +654 -0
- package/dist/es-cd1940a4.esm.js +343 -0
- package/dist/es-dac5d844.cjs.dev.js +345 -0
- package/dist/es-e7423eff.cjs.prod.js +654 -0
- package/dist/es-eefa39e0.cjs.prod.js +1900 -0
- package/dist/fr-FR-06d6a3f4.cjs.dev.js +654 -0
- package/dist/fr-FR-06e1e6e2.cjs.prod.js +1900 -0
- package/dist/fr-FR-0bcee641.cjs.dev.js +345 -0
- package/dist/fr-FR-25212b0c.cjs.dev.js +267 -0
- package/dist/fr-FR-27ef287f.esm.js +343 -0
- package/dist/fr-FR-2ac6a985.cjs.prod.js +654 -0
- package/dist/fr-FR-42a1a059.esm.js +652 -0
- package/dist/fr-FR-8225f110.esm.js +265 -0
- package/dist/fr-FR-b689c6d2.cjs.dev.js +1900 -0
- package/dist/fr-FR-c2e0027c.esm.js +1898 -0
- package/dist/fr-FR-ef3a4606.cjs.prod.js +267 -0
- package/dist/fr-FR-f9bea42f.cjs.prod.js +345 -0
- package/dist/pt-BR-069d8608.cjs.dev.js +267 -0
- package/dist/pt-BR-0dac9697.cjs.dev.js +345 -0
- package/dist/pt-BR-186676b4.cjs.prod.js +1900 -0
- package/dist/pt-BR-23d7d3ea.esm.js +265 -0
- package/dist/pt-BR-2a6004db.esm.js +343 -0
- package/dist/pt-BR-499a25f3.cjs.prod.js +654 -0
- package/dist/pt-BR-5cec8e04.cjs.dev.js +1900 -0
- package/dist/pt-BR-b1791fcf.cjs.prod.js +267 -0
- package/dist/pt-BR-bf9aad03.cjs.dev.js +654 -0
- package/dist/pt-BR-e1b0935a.cjs.prod.js +345 -0
- package/dist/pt-BR-e32fe1fb.esm.js +652 -0
- package/dist/pt-BR-ee41cc2f.esm.js +1898 -0
- package/dist/translations-map-0e2bd91e.cjs.dev.js +941 -0
- package/dist/translations-map-4ec75e62.cjs.prod.js +941 -0
- package/dist/translations-map-a3b482d1.esm.js +939 -0
- package/package.json +65 -0
- package/supported-locales.d.ts +3 -0
- package/supported-locales.js +7 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) commercetools GmbH
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
# @commercetools-frontend/l10n
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<a href="https://www.npmjs.com/package/@commercetools-frontend/l10n"><img src="https://badgen.net/npm/v/@commercetools-frontend/l10n" alt="Latest release (latest dist-tag)" /></a> <a href="https://www.npmjs.com/package/@commercetools-frontend/l10n"><img src="https://badgen.net/npm/v/@commercetools-frontend/l10n/next" alt="Latest release (next dist-tag)" /></a> <a href="https://bundlephobia.com/result?p=@commercetools-frontend/l10n"><img src="https://badgen.net/bundlephobia/minzip/@commercetools-frontend/l10n" alt="Minified + GZipped size" /></a> <a href="https://github.com/commercetools/merchant-center-application-kit/blob/main/LICENSE"><img src="https://badgen.net/github/license/commercetools/merchant-center-application-kit" alt="GitHub license" /></a>
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
React bindings to load l10n data.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
$ npm install --save @commercetools-frontend/l10n
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Supported locales
|
|
16
|
+
|
|
17
|
+
- `en`
|
|
18
|
+
- `de`
|
|
19
|
+
- `es`
|
|
20
|
+
- `fr-FR`
|
|
21
|
+
|
|
22
|
+
## Hooks
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
import {
|
|
26
|
+
useCountries,
|
|
27
|
+
useCurrencies,
|
|
28
|
+
useLanguages,
|
|
29
|
+
useTimeZones,
|
|
30
|
+
} from '@commercetools-frontend/l10n';
|
|
31
|
+
|
|
32
|
+
const { isLoading, data, error } = useCountries('en');
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Higher Order Components
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
import { withCountries } from '@commercetools-frontend/l10n';
|
|
39
|
+
|
|
40
|
+
withCountries((ownProps) => ownProps.locale)(Component);
|
|
41
|
+
|
|
42
|
+
// format: { countryCode: countryLabel }
|
|
43
|
+
// { "de":"Germany" }
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
```js
|
|
47
|
+
import { withCurrencies } from '@commercetools-frontend/l10n';
|
|
48
|
+
|
|
49
|
+
withCurrencies((ownProps) => ownProps.locale)(Component);
|
|
50
|
+
|
|
51
|
+
// format: { currencyCode: { label, symbol } }
|
|
52
|
+
// { "EUR": { "label": "Euro", "symbol": "€" } }
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
```js
|
|
56
|
+
import { withLanguages } from '@commercetools-frontend/l10n';
|
|
57
|
+
|
|
58
|
+
withLanguages((ownProps) => ownProps.locale)(Component);
|
|
59
|
+
|
|
60
|
+
// format: { languageCode: { language, country? } }
|
|
61
|
+
// Case with main language
|
|
62
|
+
// { "es": { "language": "Spanish" } }
|
|
63
|
+
// Case with language of a region
|
|
64
|
+
// { "es-AR": { "language": "Spanish", "country": "Argentina" } }
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
```js
|
|
68
|
+
import { withTimeZones } from '@commercetools-frontend/l10n';
|
|
69
|
+
|
|
70
|
+
withTimeZones((ownProps) => ownProps.locale)(Component);
|
|
71
|
+
|
|
72
|
+
// format: { timezone: { name, abbr, offset } }
|
|
73
|
+
// Case with main language
|
|
74
|
+
// { "es": { "Europe/Berlin": { "name": "Europe/Berlin", "abbr": "CEST", "offset": "+02:00" } } }
|
|
75
|
+
// Case with language of a region
|
|
76
|
+
// { "fr-FR": { "Europe/Berlin": { "name": "Europe/Berlin", "abbr": "CEST", "offset": "+02:00" } } }
|
|
77
|
+
// { "es-AR": { "Europe/Berlin": { "name": "Europe/Berlin", "abbr": "CEST", "offset": "+02:00" } } }
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Generating localization data
|
|
81
|
+
|
|
82
|
+
The `data` directory contains all the localization data for the supported locales. The data is generated using the script `scripts/generate-l10n-data.js`, which uses the [`cldr` data](http://cldr.unicode.org).
|
|
83
|
+
|
|
84
|
+
#### Using a custom version of the cldr data
|
|
85
|
+
|
|
86
|
+
In case the npm library `cldr` does not contain the latest cldr data, we can manually download it and point the `cldr` library to use that data.
|
|
87
|
+
|
|
88
|
+
```js
|
|
89
|
+
// For example, using the v35 of cldr
|
|
90
|
+
const cldr = require('cldr').load(path.join(__dirname, '../cldr-v35'));
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
First, download the data (`core.zip`) for a specific version from the [downloads page](http://cldr.unicode.org/index/downloads).
|
|
94
|
+
|
|
95
|
+
Then extract the data and copy the `core` folder to this package, and rename it to e.g. `cldr-v35`. Then point the `cldr` library to the folder location.
|
|
96
|
+
|
|
97
|
+
Run the script, which uses the new data.
|
|
98
|
+
|
|
99
|
+
#### Generating Time Zone Data
|
|
100
|
+
|
|
101
|
+
Translations for time zones are located in `data/time-zones/`. There is a `core.json` and a file for each supported locale (`<locale>.json`). Transifex integration for these translations will be added in a subsequent PR.
|
|
102
|
+
|
|
103
|
+
Only some time zones have translations. For those time zones that have not been translated, each have been mapped to a time zone that does have a translation in `data/time-zones/translations-map.json`. This ensures that each IANA time zone identifier returned by `moment.tz.names()` will show an accurate translation.
|
|
104
|
+
|
|
105
|
+
The IANA [peridically updates](https://www.rfc-editor.org/rfc/rfc6557.html) its' [list of timezones](https://www.iana.org/time-zones). When the IANA releases a new set of time zones, [it will be added](https://momentjs.com/docs/#/-project-status/) to `moment-timezone`.
|
|
106
|
+
|
|
107
|
+
When `generate-l10n-data` is run, it gets the list of all time zones being returned by `moment-timezone` by running `moment.tz.names()`, and compares it to the list of translated (included) and mapped (excluded) time zones in `core.json` and `translations-map.json`.
|
|
108
|
+
|
|
109
|
+
For each unhandled time zone returned, CLI will prompt the user to either `translate`, `exclude`, or `ignore` it.
|
|
110
|
+
|
|
111
|
+
If the user selects `translate`, they must enter an english language translation string. That string will then be added to `core.json` and each `<locale>.json` for subsequent translation. It will also be added as a key in `translations-map.json` so that subsequent time zones can be mapped to its' translation if they are excluded.
|
|
112
|
+
|
|
113
|
+
If the user selects `exclude`, they must enter the IANA time zone id of a time zone that currently has a translation in order to map the time zone to a translation string. The `excluded` translation is then added to the array for the give translated time zone in `translations-map.json`, e.g.:
|
|
114
|
+
`"America/New_York": ["EST", "EDT"]`
|
|
115
|
+
|
|
116
|
+
If the user selects `ignore`, the unhandled time zone will be ignored until the `generate-l10n-data` script is run again.
|
|
117
|
+
|
|
118
|
+
## Utils
|
|
119
|
+
|
|
120
|
+
### `applyTransformedLocalizedFields`
|
|
121
|
+
|
|
122
|
+
> Transforms multiple `LocalizedField` -> `LocalizedString`, given `fieldNameTransformationMappings`
|
|
123
|
+
|
|
124
|
+
#### Context
|
|
125
|
+
|
|
126
|
+
Amongst [Common Types](https://docs.commercetools.com/api/types) in the commercetools platform API, we find [`LocalizedString`](https://docs.commercetools.com/api/types#localizedstring) type.
|
|
127
|
+
|
|
128
|
+
The `LocalizedString` is a type reserved for values found in a [Resource](https://docs.commercetools.com/api/types#referencetype), for example `Product`:
|
|
129
|
+
|
|
130
|
+
```js
|
|
131
|
+
// Product
|
|
132
|
+
{
|
|
133
|
+
// `name` is a `LocalizedString`
|
|
134
|
+
// as defined by https://docs.commercetools.com/api/projects/products#productdata
|
|
135
|
+
name: {
|
|
136
|
+
en: 'Milk';
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
The documented `LocalizedString` in [https://docs.commercetools.com](https://docs.commercetools.com/) is a specification of the HTTP API.
|
|
142
|
+
|
|
143
|
+
However, the commercetools platform `/graphql` API represents the `LocalizedString` as a list of the same name.
|
|
144
|
+
|
|
145
|
+
```js
|
|
146
|
+
// Product, returned from the `/graphql` API of commercetools platform
|
|
147
|
+
{
|
|
148
|
+
nameAllLocales: [
|
|
149
|
+
{
|
|
150
|
+
locale: 'en',
|
|
151
|
+
value: 'Milk',
|
|
152
|
+
},
|
|
153
|
+
];
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
To distinguish these in source code of the Merchant Center, we name the graphql shaped value `LocalizedField`. We offer `applyTransformedLocalizedFields` that is authored to transform these values from one to the other.
|
|
158
|
+
|
|
159
|
+
#### Example usage
|
|
160
|
+
|
|
161
|
+
```js
|
|
162
|
+
const product = {
|
|
163
|
+
nameAllLocales: [
|
|
164
|
+
{
|
|
165
|
+
locale: 'en',
|
|
166
|
+
value: 'Milk',
|
|
167
|
+
},
|
|
168
|
+
],
|
|
169
|
+
descriptionAllLocales: [
|
|
170
|
+
{
|
|
171
|
+
locale: 'en',
|
|
172
|
+
value: 'This is milk',
|
|
173
|
+
},
|
|
174
|
+
],
|
|
175
|
+
};
|
|
176
|
+
const fieldNameTransformationMappings = [
|
|
177
|
+
{
|
|
178
|
+
from: 'nameAllLocales',
|
|
179
|
+
to: 'name',
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
from: 'descriptionAllLocales',
|
|
183
|
+
to: 'description',
|
|
184
|
+
},
|
|
185
|
+
];
|
|
186
|
+
const transformedProduct = applyTransformedLocalizedFields(
|
|
187
|
+
fetchedProduct,
|
|
188
|
+
fieldNameTransformationMappings
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
console.log(transformedProduct);
|
|
192
|
+
// { name: { en: 'Milk' }, description: { en: 'This is milk' } }
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
#### When to use it
|
|
196
|
+
|
|
197
|
+
This transformation tool will be helpful when you consume the commercetools platform `/graphql` API in conjunction with authoring views consuming `@commercetools-frontend/ui-kit`.
|
|
198
|
+
|
|
199
|
+
Given that you consume:
|
|
200
|
+
|
|
201
|
+
- The commercetools platform `/graphql` API
|
|
202
|
+
- [`LocalizedTextInput`](https://github.com/commercetools/ui-kit/blob/main/packages/components/inputs/localized-text-input/src/localized-text-input.js)
|
|
203
|
+
|
|
204
|
+
This will be helpful transforming data from `response -> view`.
|
|
205
|
+
|
|
206
|
+
```js
|
|
207
|
+
// fetching product from the commercetools platform `/graphql` API
|
|
208
|
+
// returns a product with a `nameAllLocales` and `descriptionAllLocales`
|
|
209
|
+
const product = useMcQuery(ProductQuery, {
|
|
210
|
+
context: GRAPHQL_TARGETS.COMMERCETOOLS_PLATFORM,
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
// Given that LocalizedTextInput accepts a value of `{ [key: string]: string }`,
|
|
214
|
+
// we tranform our product to match the required shape
|
|
215
|
+
const transformedProduct = applyTransformedLocalizedFields(
|
|
216
|
+
product,
|
|
217
|
+
fieldNameTransformMappings
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
// Finally, we are ready to render our form with the correctly shaped `name` and `description`
|
|
221
|
+
return (
|
|
222
|
+
<>
|
|
223
|
+
<LocalizedTextInput name="name" value={transformedProduct.name} />
|
|
224
|
+
<LocalizedTextInput
|
|
225
|
+
name="description"
|
|
226
|
+
value={transformedProduct.description}
|
|
227
|
+
/>
|
|
228
|
+
</>
|
|
229
|
+
);
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### `formatLocalizedString` (DRAFT)
|
|
233
|
+
|
|
234
|
+
> Transforms a `LocalizedString` to a String
|
|
235
|
+
|
|
236
|
+
The `formatLocalizedString` util is a util we use internally inside the Merchant Center. This util is at the moment subject to change, hence marked as `Draft`.
|
|
237
|
+
|
|
238
|
+
#### Context
|
|
239
|
+
|
|
240
|
+
As discussed under `applyTransformedLocalizedFields`, a `LocalizedString` is a value type reserved for a `Resource`, e.g a `Product`.
|
|
241
|
+
|
|
242
|
+
```js
|
|
243
|
+
const product = {
|
|
244
|
+
name: {
|
|
245
|
+
en: 'Milk',
|
|
246
|
+
de: 'Milch',
|
|
247
|
+
},
|
|
248
|
+
};
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
When rendering a Product in a view, we would like to render the value of a localized string field, such as `name`, given the selected **data locale** of the UI.
|
|
252
|
+
The **data locale** is a value controlled by the Merchant Center User (MC User) by changing the **data locale switcher** in the top bar of the Merchant Center. The list of available options is derived by the list of languages specified in the project. The selected value can be read from the **application context**.
|
|
253
|
+
|
|
254
|
+
However, there might be a case where the selected **data locale** does not match any of the localized string values. In this case, it is recommended to display a "fallback" value using the `formatLocalizedString` function.
|
|
255
|
+
|
|
256
|
+
The `formatLocalizedString` util is authored with the following in mind:
|
|
257
|
+
|
|
258
|
+
1. Help deriving a value of `LocalizedString` given a specified locale.
|
|
259
|
+
2. Help rendering a value derived from `LocalizedString`.
|
|
260
|
+
|
|
261
|
+
Let us take a look at the examples below putting this to action.
|
|
262
|
+
|
|
263
|
+
#### Example usage
|
|
264
|
+
|
|
265
|
+
All examples below will use the following `Product`:
|
|
266
|
+
|
|
267
|
+
```
|
|
268
|
+
const product = {
|
|
269
|
+
name: {
|
|
270
|
+
en: 'Milk',
|
|
271
|
+
de: 'Milch,
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
##### Scenario 1
|
|
277
|
+
|
|
278
|
+
Given that selected **data locale** of `de`
|
|
279
|
+
|
|
280
|
+
```js
|
|
281
|
+
const translatedName = formatLocalizedString(product, {
|
|
282
|
+
key: 'name',
|
|
283
|
+
locale: 'de',
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
console.log(translatedName);
|
|
287
|
+
// 'Milch'
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
##### Scenario 2
|
|
291
|
+
|
|
292
|
+
Given that selected **data locale** of `sv`
|
|
293
|
+
|
|
294
|
+
```js
|
|
295
|
+
const translatedName = formatLocalizedString(product, {
|
|
296
|
+
key: 'name',
|
|
297
|
+
locale: 'sv',
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
console.log(translatedName);
|
|
301
|
+
// 'Milk (EN)'
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
**What happened?**
|
|
305
|
+
|
|
306
|
+
Our product has no value for the selected **data locale** `sv`. The `formatLocalizedString` function selects the "next available value" (more details on the order below) from `product.name`. In this case it's `en`, and returns it with a hint `(EN)` that this value refers to another locale.
|
|
307
|
+
|
|
308
|
+
##### Scenario 3
|
|
309
|
+
|
|
310
|
+
Given that selected **data locale** of `de-AT`
|
|
311
|
+
|
|
312
|
+
```js
|
|
313
|
+
const translatedName = formatLocalizedString(product, {
|
|
314
|
+
key: 'name',
|
|
315
|
+
locale: 'de-AT',
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
console.log(translatedName);
|
|
319
|
+
// 'Milch'
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
**What happened?**
|
|
323
|
+
|
|
324
|
+
Our product has no matching value for the selected **data locale** `de-AT` but we still got `"Milch"`.
|
|
325
|
+
This is because `formatLocalizedString` attempts to determine a **primary language tag** on the option `locale` and match that to the available values. Given that `de` is a [primary](https://en.wikipedia.org/wiki/IETF_language_tag) of `de-AT`, it determines that this is the _closest available value_ from `product.name`
|
|
326
|
+
|
|
327
|
+
#### Fallback
|
|
328
|
+
|
|
329
|
+
To provide even more freedom beyond cases mentioned above the `formatLocalizedString` allows specifying a `fallback` as a last resort:
|
|
330
|
+
|
|
331
|
+
```js
|
|
332
|
+
const translatedName = formatLocalizedString(product, {
|
|
333
|
+
obj: product,
|
|
334
|
+
key: 'name',
|
|
335
|
+
locale: 'sv',
|
|
336
|
+
fallback: '-',
|
|
337
|
+
});
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
The default value of `fallback` is a `""`.
|
|
341
|
+
|
|
342
|
+
#### Fallback order
|
|
343
|
+
|
|
344
|
+
In **Scenario 2** above, we discussed that `formatLocalizedString` will pick the "next available value" from `product.name` when there is no matching value. In our case, the next available value was the locale `en`.
|
|
345
|
+
Merchant Center customizations developers can take full control over the order of attempted lookups of the value while there is no match. Before `formatLocalizedString` eventually proceeds to rendering what is specified as `fallback`.
|
|
346
|
+
|
|
347
|
+
`formatLocalizedString` accepts `fallbackOrder`, and this [test exemplifies the use case and resolve](./src/localize.spec.ts#L151:L170).
|
|
348
|
+
|
|
349
|
+
#### When to use it
|
|
350
|
+
|
|
351
|
+
Given that we want to render `LocalizedString` of a given `Resource`, it is sensible to rely on `formatLocalizedString` in conjunction with the Application Context. A user usually has a defined preference of languages we can use.
|
|
352
|
+
|
|
353
|
+
```js
|
|
354
|
+
import Text from '@commercetools-uikit/text';
|
|
355
|
+
import { useApplicationContext } from '@commercetools-frontend/application-shell-connectors';
|
|
356
|
+
|
|
357
|
+
const { dataLocale, projectLanguages } = useApplicationContext(
|
|
358
|
+
(applicationContext) => ({
|
|
359
|
+
dataLocale: applicationContext.dataLocale,
|
|
360
|
+
// The Application Context also exposes the languages that are defined on the Project settings
|
|
361
|
+
// we can rely on this to determine the fallback order.
|
|
362
|
+
// This helps with consistency, although you can specify the fallback order however you want
|
|
363
|
+
projectLanguages: context.project.languages,
|
|
364
|
+
})
|
|
365
|
+
);
|
|
366
|
+
|
|
367
|
+
return (
|
|
368
|
+
<Text.Headline>
|
|
369
|
+
{formatLocalizedString(product, {
|
|
370
|
+
key: 'name',
|
|
371
|
+
locale: dataLocale,
|
|
372
|
+
fallback: '<MY_CUSTOM_FALLBACK>',
|
|
373
|
+
fallbackOrder: projectLanguages,
|
|
374
|
+
})}
|
|
375
|
+
</Text.Headline>
|
|
376
|
+
);
|
|
377
|
+
```
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
{
|
|
2
|
+
"ac": "Ascension",
|
|
3
|
+
"ad": "Andorra",
|
|
4
|
+
"ae": "Vereinigte Arabische Emirate",
|
|
5
|
+
"af": "Afghanistan",
|
|
6
|
+
"ag": "Antigua und Barbuda",
|
|
7
|
+
"ai": "Anguilla",
|
|
8
|
+
"al": "Albanien",
|
|
9
|
+
"am": "Armenien",
|
|
10
|
+
"ao": "Angola",
|
|
11
|
+
"aq": "Antarktis",
|
|
12
|
+
"ar": "Argentinien",
|
|
13
|
+
"as": "Amerikanisch-Samoa",
|
|
14
|
+
"at": "Österreich",
|
|
15
|
+
"au": "Australien",
|
|
16
|
+
"aw": "Aruba",
|
|
17
|
+
"ax": "Ålandinseln",
|
|
18
|
+
"az": "Aserbaidschan",
|
|
19
|
+
"ba": "Bosnien und Herzegowina",
|
|
20
|
+
"bb": "Barbados",
|
|
21
|
+
"bd": "Bangladesch",
|
|
22
|
+
"be": "Belgien",
|
|
23
|
+
"bf": "Burkina Faso",
|
|
24
|
+
"bg": "Bulgarien",
|
|
25
|
+
"bh": "Bahrain",
|
|
26
|
+
"bi": "Burundi",
|
|
27
|
+
"bj": "Benin",
|
|
28
|
+
"bl": "St. Barthélemy",
|
|
29
|
+
"bm": "Bermuda",
|
|
30
|
+
"bn": "Brunei Darussalam",
|
|
31
|
+
"bo": "Bolivien",
|
|
32
|
+
"bq": "Karibische Niederlande",
|
|
33
|
+
"br": "Brasilien",
|
|
34
|
+
"bs": "Bahamas",
|
|
35
|
+
"bt": "Bhutan",
|
|
36
|
+
"bv": "Bouvetinsel",
|
|
37
|
+
"bw": "Botsuana",
|
|
38
|
+
"by": "Belarus",
|
|
39
|
+
"bz": "Belize",
|
|
40
|
+
"ca": "Kanada",
|
|
41
|
+
"cc": "Kokosinseln",
|
|
42
|
+
"cd": "Kongo-Kinshasa",
|
|
43
|
+
"cf": "Zentralafrikanische Republik",
|
|
44
|
+
"cg": "Kongo-Brazzaville",
|
|
45
|
+
"ch": "Schweiz",
|
|
46
|
+
"ci": "Côte d’Ivoire",
|
|
47
|
+
"ck": "Cookinseln",
|
|
48
|
+
"cl": "Chile",
|
|
49
|
+
"cm": "Kamerun",
|
|
50
|
+
"cn": "China",
|
|
51
|
+
"co": "Kolumbien",
|
|
52
|
+
"cp": "Clipperton-Insel",
|
|
53
|
+
"cr": "Costa Rica",
|
|
54
|
+
"cu": "Kuba",
|
|
55
|
+
"cv": "Cabo Verde",
|
|
56
|
+
"cw": "Curaçao",
|
|
57
|
+
"cx": "Weihnachtsinsel",
|
|
58
|
+
"cy": "Zypern",
|
|
59
|
+
"cz": "Tschechien",
|
|
60
|
+
"de": "Deutschland",
|
|
61
|
+
"dg": "Diego Garcia",
|
|
62
|
+
"dj": "Dschibuti",
|
|
63
|
+
"dk": "Dänemark",
|
|
64
|
+
"dm": "Dominica",
|
|
65
|
+
"do": "Dominikanische Republik",
|
|
66
|
+
"dz": "Algerien",
|
|
67
|
+
"ea": "Ceuta und Melilla",
|
|
68
|
+
"ec": "Ecuador",
|
|
69
|
+
"ee": "Estland",
|
|
70
|
+
"eg": "Ägypten",
|
|
71
|
+
"eh": "Westsahara",
|
|
72
|
+
"er": "Eritrea",
|
|
73
|
+
"es": "Spanien",
|
|
74
|
+
"et": "Äthiopien",
|
|
75
|
+
"eu": "Europäische Union",
|
|
76
|
+
"ez": "Eurozone",
|
|
77
|
+
"fi": "Finnland",
|
|
78
|
+
"fj": "Fidschi",
|
|
79
|
+
"fk": "Falklandinseln",
|
|
80
|
+
"fm": "Mikronesien",
|
|
81
|
+
"fo": "Färöer",
|
|
82
|
+
"fr": "Frankreich",
|
|
83
|
+
"ga": "Gabun",
|
|
84
|
+
"gb": "Vereinigtes Königreich",
|
|
85
|
+
"gd": "Grenada",
|
|
86
|
+
"ge": "Georgien",
|
|
87
|
+
"gf": "Französisch-Guayana",
|
|
88
|
+
"gg": "Guernsey",
|
|
89
|
+
"gh": "Ghana",
|
|
90
|
+
"gi": "Gibraltar",
|
|
91
|
+
"gl": "Grönland",
|
|
92
|
+
"gm": "Gambia",
|
|
93
|
+
"gn": "Guinea",
|
|
94
|
+
"gp": "Guadeloupe",
|
|
95
|
+
"gq": "Äquatorialguinea",
|
|
96
|
+
"gr": "Griechenland",
|
|
97
|
+
"gs": "Südgeorgien und die Südlichen Sandwichinseln",
|
|
98
|
+
"gt": "Guatemala",
|
|
99
|
+
"gu": "Guam",
|
|
100
|
+
"gw": "Guinea-Bissau",
|
|
101
|
+
"gy": "Guyana",
|
|
102
|
+
"hk": "Sonderverwaltungsregion Hongkong",
|
|
103
|
+
"hm": "Heard und McDonaldinseln",
|
|
104
|
+
"hn": "Honduras",
|
|
105
|
+
"hr": "Kroatien",
|
|
106
|
+
"ht": "Haiti",
|
|
107
|
+
"hu": "Ungarn",
|
|
108
|
+
"ic": "Kanarische Inseln",
|
|
109
|
+
"id": "Indonesien",
|
|
110
|
+
"ie": "Irland",
|
|
111
|
+
"il": "Israel",
|
|
112
|
+
"im": "Isle of Man",
|
|
113
|
+
"in": "Indien",
|
|
114
|
+
"io": "Britisches Territorium im Indischen Ozean",
|
|
115
|
+
"iq": "Irak",
|
|
116
|
+
"ir": "Iran",
|
|
117
|
+
"is": "Island",
|
|
118
|
+
"it": "Italien",
|
|
119
|
+
"je": "Jersey",
|
|
120
|
+
"jm": "Jamaika",
|
|
121
|
+
"jo": "Jordanien",
|
|
122
|
+
"jp": "Japan",
|
|
123
|
+
"ke": "Kenia",
|
|
124
|
+
"kg": "Kirgisistan",
|
|
125
|
+
"kh": "Kambodscha",
|
|
126
|
+
"ki": "Kiribati",
|
|
127
|
+
"km": "Komoren",
|
|
128
|
+
"kn": "St. Kitts und Nevis",
|
|
129
|
+
"kp": "Nordkorea",
|
|
130
|
+
"kr": "Südkorea",
|
|
131
|
+
"kw": "Kuwait",
|
|
132
|
+
"ky": "Kaimaninseln",
|
|
133
|
+
"kz": "Kasachstan",
|
|
134
|
+
"la": "Laos",
|
|
135
|
+
"lb": "Libanon",
|
|
136
|
+
"lc": "St. Lucia",
|
|
137
|
+
"li": "Liechtenstein",
|
|
138
|
+
"lk": "Sri Lanka",
|
|
139
|
+
"lr": "Liberia",
|
|
140
|
+
"ls": "Lesotho",
|
|
141
|
+
"lt": "Litauen",
|
|
142
|
+
"lu": "Luxemburg",
|
|
143
|
+
"lv": "Lettland",
|
|
144
|
+
"ly": "Libyen",
|
|
145
|
+
"ma": "Marokko",
|
|
146
|
+
"mc": "Monaco",
|
|
147
|
+
"md": "Republik Moldau",
|
|
148
|
+
"me": "Montenegro",
|
|
149
|
+
"mf": "St. Martin",
|
|
150
|
+
"mg": "Madagaskar",
|
|
151
|
+
"mh": "Marshallinseln",
|
|
152
|
+
"mk": "Nordmazedonien",
|
|
153
|
+
"ml": "Mali",
|
|
154
|
+
"mm": "Myanmar",
|
|
155
|
+
"mn": "Mongolei",
|
|
156
|
+
"mo": "Sonderverwaltungsregion Macau",
|
|
157
|
+
"mp": "Nördliche Marianen",
|
|
158
|
+
"mq": "Martinique",
|
|
159
|
+
"mr": "Mauretanien",
|
|
160
|
+
"ms": "Montserrat",
|
|
161
|
+
"mt": "Malta",
|
|
162
|
+
"mu": "Mauritius",
|
|
163
|
+
"mv": "Malediven",
|
|
164
|
+
"mw": "Malawi",
|
|
165
|
+
"mx": "Mexiko",
|
|
166
|
+
"my": "Malaysia",
|
|
167
|
+
"mz": "Mosambik",
|
|
168
|
+
"na": "Namibia",
|
|
169
|
+
"nc": "Neukaledonien",
|
|
170
|
+
"ne": "Niger",
|
|
171
|
+
"nf": "Norfolkinsel",
|
|
172
|
+
"ng": "Nigeria",
|
|
173
|
+
"ni": "Nicaragua",
|
|
174
|
+
"nl": "Niederlande",
|
|
175
|
+
"no": "Norwegen",
|
|
176
|
+
"np": "Nepal",
|
|
177
|
+
"nr": "Nauru",
|
|
178
|
+
"nu": "Niue",
|
|
179
|
+
"nz": "Neuseeland",
|
|
180
|
+
"om": "Oman",
|
|
181
|
+
"pa": "Panama",
|
|
182
|
+
"pe": "Peru",
|
|
183
|
+
"pf": "Französisch-Polynesien",
|
|
184
|
+
"pg": "Papua-Neuguinea",
|
|
185
|
+
"ph": "Philippinen",
|
|
186
|
+
"pk": "Pakistan",
|
|
187
|
+
"pl": "Polen",
|
|
188
|
+
"pm": "St. Pierre und Miquelon",
|
|
189
|
+
"pn": "Pitcairninseln",
|
|
190
|
+
"pr": "Puerto Rico",
|
|
191
|
+
"ps": "Palästinensische Autonomiegebiete",
|
|
192
|
+
"pt": "Portugal",
|
|
193
|
+
"pw": "Palau",
|
|
194
|
+
"py": "Paraguay",
|
|
195
|
+
"qa": "Katar",
|
|
196
|
+
"re": "Réunion",
|
|
197
|
+
"ro": "Rumänien",
|
|
198
|
+
"rs": "Serbien",
|
|
199
|
+
"ru": "Russland",
|
|
200
|
+
"rw": "Ruanda",
|
|
201
|
+
"sa": "Saudi-Arabien",
|
|
202
|
+
"sb": "Salomonen",
|
|
203
|
+
"sc": "Seychellen",
|
|
204
|
+
"sd": "Sudan",
|
|
205
|
+
"se": "Schweden",
|
|
206
|
+
"sg": "Singapur",
|
|
207
|
+
"sh": "St. Helena",
|
|
208
|
+
"si": "Slowenien",
|
|
209
|
+
"sj": "Spitzbergen und Jan Mayen",
|
|
210
|
+
"sk": "Slowakei",
|
|
211
|
+
"sl": "Sierra Leone",
|
|
212
|
+
"sm": "San Marino",
|
|
213
|
+
"sn": "Senegal",
|
|
214
|
+
"so": "Somalia",
|
|
215
|
+
"sr": "Suriname",
|
|
216
|
+
"ss": "Südsudan",
|
|
217
|
+
"st": "São Tomé und Príncipe",
|
|
218
|
+
"sv": "El Salvador",
|
|
219
|
+
"sx": "Sint Maarten",
|
|
220
|
+
"sy": "Syrien",
|
|
221
|
+
"sz": "Eswatini",
|
|
222
|
+
"ta": "Tristan da Cunha",
|
|
223
|
+
"tc": "Turks- und Caicosinseln",
|
|
224
|
+
"td": "Tschad",
|
|
225
|
+
"tf": "Französische Süd- und Antarktisgebiete",
|
|
226
|
+
"tg": "Togo",
|
|
227
|
+
"th": "Thailand",
|
|
228
|
+
"tj": "Tadschikistan",
|
|
229
|
+
"tk": "Tokelau",
|
|
230
|
+
"tl": "Timor-Leste",
|
|
231
|
+
"tm": "Turkmenistan",
|
|
232
|
+
"tn": "Tunesien",
|
|
233
|
+
"to": "Tonga",
|
|
234
|
+
"tr": "Türkei",
|
|
235
|
+
"tt": "Trinidad und Tobago",
|
|
236
|
+
"tv": "Tuvalu",
|
|
237
|
+
"tw": "Taiwan",
|
|
238
|
+
"tz": "Tansania",
|
|
239
|
+
"ua": "Ukraine",
|
|
240
|
+
"ug": "Uganda",
|
|
241
|
+
"um": "Amerikanische Überseeinseln",
|
|
242
|
+
"us": "Vereinigte Staaten",
|
|
243
|
+
"uy": "Uruguay",
|
|
244
|
+
"uz": "Usbekistan",
|
|
245
|
+
"va": "Vatikanstadt",
|
|
246
|
+
"vc": "St. Vincent und die Grenadinen",
|
|
247
|
+
"ve": "Venezuela",
|
|
248
|
+
"vg": "Britische Jungferninseln",
|
|
249
|
+
"vi": "Amerikanische Jungferninseln",
|
|
250
|
+
"vn": "Vietnam",
|
|
251
|
+
"vu": "Vanuatu",
|
|
252
|
+
"wf": "Wallis und Futuna",
|
|
253
|
+
"ws": "Samoa",
|
|
254
|
+
"xa": "Pseudo-Akzente",
|
|
255
|
+
"xb": "Pseudo-Bidi",
|
|
256
|
+
"xi": "Nordirland",
|
|
257
|
+
"xk": "Kosovo",
|
|
258
|
+
"ye": "Jemen",
|
|
259
|
+
"yt": "Mayotte",
|
|
260
|
+
"za": "Südafrika",
|
|
261
|
+
"zm": "Sambia",
|
|
262
|
+
"zw": "Simbabwe"
|
|
263
|
+
}
|