cozy-ui 77.7.0 → 77.7.1

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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [77.7.1](https://github.com/cozy/cozy-ui/compare/v77.7.0...v77.7.1) (2022-11-22)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * Load default langage when using createUseI18n ([5ce65fe](https://github.com/cozy/cozy-ui/commit/5ce65fe))
7
+
1
8
  # [77.7.0](https://github.com/cozy/cozy-ui/compare/v77.6.0...v77.7.0) (2022-11-21)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cozy-ui",
3
- "version": "77.7.0",
3
+ "version": "77.7.1",
4
4
  "description": "Cozy apps UI SDK",
5
5
  "main": "./index.js",
6
6
  "bin": {
@@ -102,9 +102,17 @@ export const createUseI18n = locales => () => {
102
102
  const { lang } = useI18n() || { lang: DEFAULT_LANG }
103
103
  return useMemo(() => {
104
104
  const polyglot = new Polyglot({
105
- lang: lang,
106
- phrases: locales[lang]
105
+ locale: DEFAULT_LANG,
106
+ phrases: locales[DEFAULT_LANG]
107
107
  })
108
+ if (lang && lang !== DEFAULT_LANG) {
109
+ try {
110
+ polyglot.locale(lang)
111
+ polyglot.extend(locales[lang])
112
+ } catch (e) {
113
+ console.warn(`The dict phrases for "${lang}" can't be loaded`)
114
+ }
115
+ }
108
116
  const f = initFormat(lang)
109
117
  const t = polyglot.t.bind(polyglot)
110
118
  return { t, f, lang }
@@ -87,3 +87,40 @@ describe('use i18n with custom locales', () => {
87
87
  expect(root.getByText('Bonjour le monde')).toBeTruthy()
88
88
  })
89
89
  })
90
+
91
+ describe('use i18n with custom locales and fallback to default', () => {
92
+ const locales = {
93
+ en: {
94
+ 'hello-world': 'Hello world',
95
+ 'how-are-you': 'How are you ?'
96
+ },
97
+ fr: {
98
+ 'hello-world': 'Bonjour le monde'
99
+ }
100
+ }
101
+ const useI18n = createUseI18n(locales)
102
+ const Child = () => {
103
+ const { t } = useI18n()
104
+ return (
105
+ <div>
106
+ <div>{t('hello-world')}</div>
107
+ <div>{t('how-are-you')}</div>
108
+ </div>
109
+ )
110
+ }
111
+ const Parent = () => {
112
+ return (
113
+ <>
114
+ <I18n lang="fr" dictRequire={() => ({})}>
115
+ <Child />
116
+ </I18n>
117
+ </>
118
+ )
119
+ }
120
+
121
+ it('should display missing key in default langage', () => {
122
+ const root = render(<Parent />)
123
+ expect(root.getByText('Bonjour le monde')).toBeInTheDocument()
124
+ expect(root.getByText('How are you ?')).toBeInTheDocument()
125
+ })
126
+ })
@@ -133,9 +133,19 @@ export var createUseI18n = function createUseI18n(locales) {
133
133
 
134
134
  return useMemo(function () {
135
135
  var polyglot = new Polyglot({
136
- lang: lang,
137
- phrases: locales[lang]
136
+ locale: DEFAULT_LANG,
137
+ phrases: locales[DEFAULT_LANG]
138
138
  });
139
+
140
+ if (lang && lang !== DEFAULT_LANG) {
141
+ try {
142
+ polyglot.locale(lang);
143
+ polyglot.extend(locales[lang]);
144
+ } catch (e) {
145
+ console.warn("The dict phrases for \"".concat(lang, "\" can't be loaded"));
146
+ }
147
+ }
148
+
139
149
  var f = initFormat(lang);
140
150
  var t = polyglot.t.bind(polyglot);
141
151
  return {