@inglorious/i18n 1.2.2 → 1.2.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/i18n",
3
- "version": "1.2.2",
3
+ "version": "1.2.3",
4
4
  "description": "Internationalization for Inglorious Web: an i18next instance driven by a store entity.",
5
5
  "author": "Matteo Antony Mistretta <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
6
6
  "license": "MIT",
@@ -37,8 +37,8 @@
37
37
  "i18next": "^26.5.2"
38
38
  },
39
39
  "devDependencies": {
40
- "@inglorious/eslint-config": "1.1.4",
41
- "@inglorious/store": "11.1.0",
40
+ "@inglorious/eslint-config": "1.2.0",
41
+ "@inglorious/store": "12.1.0",
42
42
  "prettier": "^3.9.6",
43
43
  "vitest": "^5.0.0"
44
44
  },
package/src/index.js CHANGED
@@ -148,19 +148,23 @@ export function createI18n({
148
148
  },
149
149
  }
150
150
 
151
- // Switches to `locale`, loading it first if needed. Whether it is a change.
151
+ // Switches to `locale`, loading it first if needed, so `loadingLocale` names
152
+ // it until its catalogue is in: what a server render or a hydration waits for.
153
+ // Whether it is a change.
152
154
  function requestLocale(entity, locale, api) {
153
155
  if (!locales.includes(locale)) return false
154
156
  const isChange = locale !== entity.locale
155
157
  // Choosing the current locale again also cancels a pending change.
156
158
  entity.loadingLocale = null
157
- if (!isChange) return false
158
159
 
159
160
  if (loader.isLoaded(locale)) {
160
- switchLocale(entity, locale)
161
- return true
161
+ // The i18next instance is shared by every store, as on a server rendering
162
+ // pages in different languages, so it can be elsewhere when the entity is not.
163
+ if (isChange || locale !== instance.language) switchLocale(entity, locale)
164
+ return isChange
162
165
  }
163
166
 
167
+ // Also the current locale, while the catalogue it started with is loading.
164
168
  // The id is taken now: the promise settles after this batch, when the
165
169
  // entity here is no longer the live one.
166
170
  const id = entity.id
@@ -169,7 +173,7 @@ export function createI18n({
169
173
  () => api.notify(`#${id}:localeLoadSuccess`, locale),
170
174
  (error) => api.notify(`#${id}:localeLoadError`, { locale, error }),
171
175
  )
172
- return true
176
+ return isChange
173
177
  }
174
178
 
175
179
  function switchLocale(entity, locale) {
package/src/index.test.js CHANGED
@@ -226,6 +226,40 @@ describe("loadLocale", () => {
226
226
  expect(loadLocale).toHaveBeenCalledTimes(1)
227
227
  })
228
228
 
229
+ it("waits for the starting locale when asked for it while it loads", async () => {
230
+ stubBrowser({ languages: ["it-IT"] })
231
+ const { loads, loadLocale } = controlledLoader()
232
+ const { store, t } = createI18nStore({ loadLocale })
233
+
234
+ store.notify("#i18n:localeChange", "it")
235
+
236
+ expect(store.getState().i18n.loadingLocale).toBe("it")
237
+
238
+ loads.it.resolve(italian)
239
+ await settle()
240
+
241
+ expect(store.getState().i18n.loadingLocale).toBe(null)
242
+ expect(t("hello")).toBe("Ciao")
243
+ })
244
+
245
+ it("switches the shared instance back for a store already in the locale", () => {
246
+ stubBrowser({ languages: ["en"] })
247
+ const { I18n, t } = createI18n({ resources })
248
+ const createI18nStoreOf = () =>
249
+ createStore({
250
+ types: { I18n },
251
+ entities: { i18n: { type: "I18n" } },
252
+ })
253
+ const english = createI18nStoreOf()
254
+ const italianStore = createI18nStoreOf()
255
+
256
+ italianStore.notify("#i18n:localeChange", "it")
257
+ expect(t("hello")).toBe("Ciao")
258
+
259
+ english.notify("#i18n:localeChange", "en")
260
+ expect(t("hello")).toBe("Hello")
261
+ })
262
+
229
263
  it("keeps the current locale when a load fails, and tries again later", async () => {
230
264
  stubBrowser({ languages: ["en"] })
231
265
  vi.spyOn(console, "warn").mockImplementation(() => {})