rolandpd / resx-to-json-loader

Webpack loader to convert .resx-files to simple json (to use in i18n frameworks).
0 stars 0 forks source link

Usage, Examples? #1

Closed wouter140 closed 4 years ago

wouter140 commented 4 years ago

Hey there,

I am using i18next for my translations. We already have resx files with the translations used throughout our applications, so we would like to use resx in i18next. On the i18next page, they show this plugin to use for resx conversion, but it does not show how to use it.

I tried adding it to the i18next instance with the use function:

i18n
    .use(ResXConverter)
    .use(LanguageDetector)
    .init({

This, however, does not work. I also tried to give the file to the ResXConverter as a function and use the result as the translation data, but instead, I get a completely different object.

resources: {
    en: ResXConverter(en_translations_path)
}

How is this library intended to be used? Could you possibly give examples?

rolandpd commented 4 years ago

Hello and thank you for your interest in this loader. I didn't know that this loader is mentioned on the i18next page.

I use it the follwoing way:

In my app I have an import of a file called 'locales.i18n' like:

import translations from './locales.i18n';
...
// options of i18next
var i18nOpts = {
  lng: 'en-US',
  defaultNS: 'app',
  resources: translations
};

The contents of locales.i18n is just a path to the base folder of all resx-files. The path can be any glob. The root of the given path is the package root (not relative to the locales.i18n file itself)

{
  "path": "./i18n/**/*.resx"
}

The last thing is in my webpack config to read the file. Json-Loader is also required.

      {
        test: /\.i18n$/,
        use: [
          'json-loader',
          'resx-to-json-loader'
        ]
      },

I hope these steps help you to use this loader.


Due to the odd "loacales.i18n" file and the inflexible usage I now use another tool of me that just converts the resx files to json files as a prebuild step. This gives me better possibilities to use it both with a loader (json-loader) but also as dynamic imports: https://github.com/rolandpd/resx-to-json-cli

Sample usage in package.json

...
"prebuild": "resx -u -s ./i18n/**/*.resx -t ./src/assets/i18n",
...
wouter140 commented 4 years ago

Ah, makes sense to put it as a prebuild step. Unfortunately, I have to get the translation files from a server and convert them to be used in i18next. Using the conversion logic of your library I was able to implement this functionality using the XHR Backend extension and implement the transform function to transform the result to json.

Thanks for your response :)