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

feat(config): outputPath function support #168

Closed xDisfigure closed 4 years ago

xDisfigure commented 4 years ago

Feature

Request

Config: outputPath function support

Expected

I would like to have the control over the outputPath value so it can directly writes changes to a specific locale file.

type outputPath = string | (locale: string, namespace: string) => string

ℹ️ Important to consider

That option will only be available for javascript babel config file

Context & Usage

In our application locales cannot be extracted into the same folder because they are shared across the app. Its only after our build we copy every locales to the same folder (public/locales/)

Example: Folder structure

.
└── src/
    ├── locales/
    │   ├── en/
    │   └── fr/
    │       └── common.json
    ├── pages/
    │   └── Dashboard/
    │       └── locales/
    │           ├── en/
    │           └── fr/
    │               └── dashboard.json
    └── anotherFolder/
        └── locales/
            ├── en/
            └── fr/
                └── anotherFolder.json

Example: babel.config.js

module.exports = {
  plugins: [
    [
      'i18next-extract',
      {
        locales: ['en', 'fr'],
        outputPath: (locale, namespace) => {
          const relPath = `src/**/*/locales/${locale}/${namespace}.json`
          const absPath = `${__dirname}/${relPath}`
          const results = glob.sync(absPath)

          if (!results.length) {
            throw new Error(`I18next: missing file in ${relPath}`)
          }

          return results[0]
        }
      }
    ]
  ],
}
codeclimate[bot] commented 4 years ago

Code Climate has analyzed commit 8a501c8e and detected 0 issues on this pull request.

View more on Code Climate.

damln commented 4 years ago

Thank you 🙏 Useful feature

gilbsgilbs commented 4 years ago

Thanks for the PR :tada: . I'll try to make a release today.

xDisfigure commented 4 years ago

Thanks for the PR 🎉 . I'll try to make a release today.

with pleasure, thanks for your fast reply !

gilbsgilbs commented 4 years ago

Seems like updating dependencies isn't as straightforward as I originally intended. No runtime issues, but looks like babel's type guards don't work as expected anymore. I need to figure out what's happening (at least to pin the relevant libraries) before shipping a new release. Can't do it just yet sorry, we'll have to wait a bit (unless somebody takes a look at it).

I've opened #169 for tracking purposes.

gilbsgilbs commented 4 years ago

Should be released on npm under 0.8.0 tag. Thanks again for the contribution.