Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

ResourceStore options doesn't update on instance clone #1974

Closed
GheorgheP opened this issue Jun 16, 2023 · 5 comments
Closed

ResourceStore options doesn't update on instance clone #1974

GheorgheP opened this issue Jun 16, 2023 · 5 comments

Comments

@GheorgheP
Copy link

GheorgheP commented Jun 16, 2023

馃悰 Bug Report

When clone the instance using i18n.cloneInstance, the ResourceStore is not cloned with new options.
Specifically in my case the keySeparator is used from original instance.

To Reproduce

const instance = i18n.cloneInstance({
  keySeparator: '[[my-new-separator]]'
})

Then try to translate a text with the new instance:

instance.t('my-key[[my-new-separator]]value');

When trying to translate with original instance key separator, it does translate.

Cause

I investigated the problem and it is cause by the fact that we create the ResourceStore at the init, but only when new instance is created, not cloned. So it is initialised only with initial options:

i18next/src/i18next.js

Lines 83 to 102 in 18ce83f

if (!this.options.isClone) {
if (this.modules.logger) {
baseLogger.init(createClassOnDemand(this.modules.logger), this.options);
} else {
baseLogger.init(null, this.options);
}
let formatter;
if (this.modules.formatter) {
formatter = this.modules.formatter;
} else if (typeof Intl !== 'undefined') {
formatter = Formatter;
}
const lu = new LanguageUtils(this.options);
this.store = new ResourceStore(this.options.resources, this.options);
const s = this.services;
s.logger = baseLogger;
s.resourceStore = this.store;

@jamuhl
Copy link
Member

jamuhl commented Jun 16, 2023

i18n.cloneInstance reuses same resources -> so why should the separators be different using same translations?!?

@adrai
Copy link
Member

adrai commented Jun 16, 2023

Better use different instances => https://www.i18next.com/overview/api#createinstance

@GheorgheP
Copy link
Author

i18n.cloneInstance reuses same resources -> so why should the separators be different using same translations?!?

The idea is simple. I have a react app, that uses a UI library (also made by me). So I pass to that ui library the i18n instance for it's internal translations. But that UI library queries that strings different from my main app (because it was made independent from my main app).
In the end I have the necessary translations with the proper structure, but the separator strings or context/plural separators are different.

Maybe we can create something like ResourceStore.cloneInstance similar to i18n.cloneInstance, so you can change the options that doesn't affects the resources. I do understand that creating a new ResourceStore may no be a good idea, or a bad idea at all, but we can use the same clone strategy.

@GheorgheP
Copy link
Author

Better use different instances => https://www.i18next.com/overview/api#createinstance

In my case this is not a good idea as I use remote translations and creating a new instance would mean to lod twice the resources.

@adrai adrai closed this as completed in c3df7b6 Jun 16, 2023
@adrai
Copy link
Member

adrai commented Jun 16, 2023

v23.1.0 introduces a new cloneInstance option:

const instance = i18n.cloneInstance({
  forkResourceStore: true,
  keySeparator: '[[my-new-separator]]'
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants