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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support config for don't use cache #78

Closed
NamPNQ opened this issue Oct 17, 2019 · 20 comments · Fixed by #174
Closed

Support config for don't use cache #78

NamPNQ opened this issue Oct 17, 2019 · 20 comments · Fixed by #174
Labels
bug Something isn't working

Comments

@NamPNQ
Copy link

NamPNQ commented Oct 17, 2019

Here is my config

{
        keyAsDefaultValue: true,
        keyAsDefaultValueForDerivedKeys: false,
}

The problem is when I edit the translation file, after that, I add a new key in component, It will write to the translation file the old content with new key, the content I have edited is lost in the wild. So, I need a config for disable cache in this line

cache.originalTranslationFiles[filePath] = loadTranslationFile(

Thank you 🙏

@gilbsgilbs
Copy link
Owner

gilbsgilbs commented Oct 17, 2019

Doesn't discardOldKeys option solve your use-case? Can you provide a complete repoduction scenario? I'm no sure to understand completely what you expect and what is the output.

@NamPNQ
Copy link
Author

NamPNQ commented Oct 17, 2019

@gilbsgilbs the discardOldKeys is not really what I mean.
Here is the scenario:

component file has key k1

t('k1')

the plugin will put k1 into en.json

{
"k1":"k1"
}

i translate k1 to hello

{
"k1":"hello"
}

change component with new content

t('k1')
t('k2')

the plugin will put k2 into en.json and the old value of k1 still in the cache

{
"k1":"k1",
"k2":"k2",
}

@gilbsgilbs gilbsgilbs added the bug Something isn't working label Oct 17, 2019
@gilbsgilbs
Copy link
Owner

Definitely unexpected. It should never overwrite values you have modified.

@NamPNQ
Copy link
Author

NamPNQ commented Oct 17, 2019

The problem is we will never reload file content before write

@gilbsgilbs
Copy link
Owner

I tested this out and I'm still unable to reproduce. As far as I know, the cache you're referring to is non-persistent. Do you run babel in some esoteric environment (like within a watcher or something) that could explain why the cache would be kept across runs? I'm not sure reloading the original file over and over again is safe.

@kmelancholiy
Copy link

I have the same trouble. I'm using webpack and babel-loader, and this happens while watching with webpack -w.
I hope that this problem is resolved.

@cs-NET
Copy link

cs-NET commented Nov 25, 2019

A few observations when using webpack-dev-server in 'watch' mode, the old keys won't get discarded, and the plural contexts will be overwritten with the original value if you perform any edits directly within the json file.

My current workaround is to stop webpack-dev-server before making any manual changes to the json file, then restart it. And to discard old keys simply restart webpack-dev-server.

@gilbsgilbs
Copy link
Owner

Sorry for the delay. That's very interesting, thank you. It makes much more sense to me now 😄 .

Does anybody have a clear idea on what would be the proper fix? The first idea that comes to my mind is to disable extraction when watch mode is detected, but is it even possible to detect it? Or maybe it would be preferable to find a way to invalidate the cache at the right moment, but I'm not sure it's possible 🤔 .

@ianmartorell
Copy link

I'm running into the same issue developing an Expo app, and it's really cumbersome to have to restart the packager every time I need to edit the translations files. 😕

@PhilippSh
Copy link

Yea, having the same issue with react + HMR. Seems like we can catch module.hot in this case, but how do we initialize it again purging the cache?

@urrri
Copy link

urrri commented May 19, 2020

I just started using this plugin and also got this problem. I run webpack dev server, which incrementally builds project on changes. I expect that manual changes, applied to the file will not be overwritten back by cached ones. I expect behavior like with the regular code: new items should be added, manually changed items should remain changed. Without this behavior plugin is practically useless, because rebuild whole project takes too long time to perform it after each manual change of translations.

@ianmartorell
Copy link

I'm using i18next-parser instead now, and I just run it via an npm script whenever I need to, beats having to restart the dev server every time I change a string... 😅 This is my i18next-parser.config.js in case anyone is interested.

@urrri
Copy link

urrri commented May 20, 2020

Should i18next-parser work with Trans component? It doesn't work for me

@urrri
Copy link

urrri commented May 20, 2020

@gilbsgilbs
If you don't have any way to detect start or end of the build (I mean incremental build, caused by watch), you can do artificial, but working fix: debounce cleanup of cached data for e.g. 1 second. next time your code will be requested to parse something reload file(s). That will allow remove cache between builds. Minor problem remains if user tries to change data during build. But in this case most editors allow user to choose which changes he want to keep, from disk or from memory.

@ianmartorell
Copy link

@urrri Actually I don't know, maybe it doesn't. I always use the useTranslate hook.

@unCleanCode
Copy link

Having the same issues as described above, but on top of that as I have 'discardOldKeys': true, it also removes most of the strings from the translation file each time I change some string during the dev server is running

I assume it has something to do with cache, as if I simply run babel it can give the same result, unless I manually clear the cache first

it really annoys and breaks dev process now. Anyone has any idea how to fix it?

@urrri
Copy link

urrri commented Jul 29, 2020

I just defined separate npm task and babel config just to run that plugin. When I need update translations I run it manually without watcher.

@xDisfigure
Copy link
Contributor

xDisfigure commented Oct 6, 2020

Im facing the same issue. I am also using babel-loader and webpack (with watch in dev mode).
I will take a look a bit later and try to find a proper way to make it work as expected.

@xDisfigure
Copy link
Contributor

Hello!

Here is a feature request to support that behavior. I was facing same issues with my webpack setup

#174

@ianmartorell
Copy link

Should i18next-parser work with Trans component? It doesn't work for me

I'm now using the Trans component here and there, and i18next-parser detects it just fine. I use it like this:

<Trans i18nKey="login:form.labels.acceptance">
  <Link external to="/tos">
    Terms of Service
  </Link>
  <Link external to="/privacy">
    Privacy Policy
  </Link>
</Trans>
# login.json

{
  "form": {
    "labels": {
      "acceptance": "I have read and agree to the <0>Terms of Service</0> and the <1>Privacy Policy</1>."
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
9 participants