GitXplorerGitXplorer
s

ast-i18n

public
216 stars
18 forks
7 issues

Commits

List of commits on branch main.
Verified
f5ac4c365dd5279d77685a7a966b574c95d7ac80

Merge pull request #88 from xerosanyam/patch-1

ssibelius committed 2 years ago
Verified
926c4a9c99d9a4d09c0ef4d7352423aa49c62814

fix typo

xxerosanyam committed 2 years ago
Verified
5f710effc3bc80ae820bec383891ba9632b6385f

Merge pull request #80 from antoni/master

ssibelius committed 3 years ago
Verified
b7f0cbb7b73bceb0bc22fccc4f661726b37b372f

fix: Fix typo in README

aantoni committed 3 years ago
Verified
d98541d6f4f779dc84ef34fee6c08df5af718d70

Merge pull request #73 from sibelius/dependabot/npm_and_yarn/y18n-3.2.2

ssibelius committed 3 years ago
Verified
fdc9e4849e2d592985798928d083fadfa3b41673

Merge pull request #74 from sibelius/dependabot/npm_and_yarn/handlebars-4.7.7

ssibelius committed 3 years ago

README

The README file for this repository.

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/i18nTransformerCodemod.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',
  ],
};