gilbsgilbs / babel-plugin-i18next-extract

Babel plugin that statically extracts i18next and react-i18next translation keys.
https://i18next-extract.netlify.com
MIT License
161 stars 37 forks source link

Support config for don't use cache #78

Closed NamPNQ closed 4 years ago

NamPNQ commented 5 years ago

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 https://github.com/gilbsgilbs/babel-plugin-i18next-extract/blob/55894cab44bbb3d199167b70c1525bc28621093c/src/exporters/index.ts#L122

Thank you 🙏

gilbsgilbs commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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

NamPNQ commented 5 years ago

The problem is we will never reload file content before write

gilbsgilbs commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 4 years ago

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

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 :thinking: .

ianmartorell commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

urrri commented 4 years ago

@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 commented 4 years ago

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

unCleanCode commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

Hello!

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

https://github.com/gilbsgilbs/babel-plugin-i18next-extract/pull/174

ianmartorell commented 4 years ago

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>."
  }
}