Giter Club home page Giter Club logo

ast-i18n's Introduction

AST i18n Build Status

The objective of this tool is to make easy to migrate an existing codebase to use i18n

How it works

  • it gets a list of files from the command line
  • it runs a babel plugin transform to find all string inside JSXText
  • it generates a stable key for the extracted strings
  • it generates i18n files format based on this map
  • it modify your existing code to use i18n library of your preference

Example

Before this transform

import React from 'react';

const Simple = () => (
  <span>My simple text</span>
);

After this transform

import React from 'react';
import { withTranslation } from 'react-i18next'

const Simple = ({ t }) => (
  <span>{t('my_simple_text')}</span>
);

Usage of string extractor

yarn start --src=myapp/src
  • It will generate a resource.jsx file, like the below:
export default {
  translation: {
   'ok': `ok`,
   'cancelar': `cancelar`,
   'continuar': `continuar`,
   'salvar': `salvar`,
   'endereco': `endereço:`,
   'troca_de_senha': `troca de senha`,
   'dados_pessoais': `dados pessoais`,
   [key]: 'value',
  }
}

How to use resource with react-i18next?

  • rename resource.tsx to your main language, like en.ts
  • create other resource languages based on the generated one
import en from './en';

i18n.use(LanguageDetector).init({
  resources: {
    en,
  },
  fallbackLng: 'ptBR',
  debug: false,

  interpolation: {
    escapeValue: false, // not needed for react!!
    formatSeparator: ',',
  },

  react: {
    wait: true,
  },
});

Usage of i18n codemod

npm i -g jscodeshift

jscodeshift -t src/i18nTransfomerCodemod.ts PATH_TO_FILES

How to customize blacklist

Use ast.config.js to customize blacklist for jsx attribute name and call expression calle

module.exports = {
  blackListJsxAttributeName: [
    'type',
    'id',
    'name',
    'children',
    'labelKey',
    'valueKey',
    'labelValue',
    'className',
    'color',
    'key',
    'size',
    'charSet',
    'content',
  ],
  blackListCallExpressionCalle: [
    't',
    '_interopRequireDefault',
    'require',
    'routeTo',
    'format',
    'importScripts',
    'buildPath',
    'createLoadable',
    'import',
    'setFieldValue',
  ],
};

ast-i18n's People

Contributors

sibelius avatar katilius avatar renanmav avatar dependabot[bot] avatar eveningkid avatar guilhermedecampo avatar jgcmarins avatar

Stargazers

Roman avatar

Watchers

James Cloos avatar

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.