Code Monkey home page Code Monkey logo

Comments (3)

gilbsgilbs avatar gilbsgilbs commented on June 7, 2024

Hi,
Thanks for raising an issue.

I don't know anything about ICU and I'll have to read more about it and how it integrates with i18next before I can give you a relevant answer.

About the detection of Trans components that are imported from the macro, maybe #33 could help, but if we don't want to wait for it we could also add macro imports in the existing whitelist. I'm not sure it would really do what you expect in any case: it would consider the Trans component from the macro to be a normal Trans component. I reckon we're looking for something slightly more subtle than that, isn't it?

Once I found this project, I realized how much cleaner it would be if the macro could run first, transform the source to handle the funky ICU format curly braces, then the extractor wouldn't need to know any special format, and would just have a "normal" Trans element to work with.

This is in theory. In practice, I don't think Babel gives you much control over when transformations happen. And you might get bitten by some other transformation if it happens too early (e.g. JSX). That's why the plugin currently runs on the Program node (the root node) and traverses the untransformed AST from there (to ensure no transformation has taken place). I'm definitely not against changing this (since it is terrible performance-wise), yet we have to be very careful about it.

I'm also considering allowing third-party plugins (extractors and exporters) at some point (probably not really short term though). Maybe ICU could be a good use-case for a custom exporter.

Sorry if I missed the point. I'm not against some more examples so that I can have the bigger picture.

from babel-plugin-i18next-extract.

gilbsgilbs avatar gilbsgilbs commented on June 7, 2024

I have a workaround in referencesImport that adds an additional fallback if the bindings don't exist properly, to just look up the import in the ImportDeclarations. Would that be a welcome PR?

Do you mean adding || referencesImport(openingElement.get('name'), 'react-i18next/icu.macro', 'Trans') to isTransComponent function? Do you have a fork where I could see what you've done? I don't think I have anything against adding basic support for components from the macro, as long as it doesn't require very deep complications to the code.

from babel-plugin-i18next-extract.

redbugz avatar redbugz commented on June 7, 2024

I was just adding this in referencesImport before the last return false:

  const existingImport = nodePath.hub.file.path.node.body.find(
      importNode => BabelTypes.isImportDeclaration(importNode) && importNode.source.value === 'react-i18next',
  );
  if (existingImport) return true;

  return false;

It's failing some tests in the repo so I have to figure that out, but it's working in my app. And I can probably tighten up the checks to be a bit more targeted.

I'm running the plugin like this:
package.json:

    "locales:extract": "NODE_ENV=development babel --config-file ./i18next-extract-babel.js 'src/**/*.js'",

i18next-extract-babel.js:

module.exports = {
  presets: ['react-app'],
  plugins: [
    'macros',
    [
      'i18next-extract',
      {
        keySeparator: null,
      },
    ],
  ],
}

from babel-plugin-i18next-extract.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.