babel-plugin-react-intl:将字符串提取到单个文件

iam*_*ham 5 react-intl

当前,在使用时babel-plugin-react-intl,将使用'id','description'和'defaultMessage'为每个组件创建单独的json。我需要的是仅创建一个json,其中包含一个对象,其中所有“ id”作为“键”,所有“ defaultMessage”作为“值”

现状:

ComponentA.json

[
  {
    "id": "addEmoticonA",
    "description": "Add emoticon",
    "defaultMessage": "Insert Emoticon"
  },
  {
    "id": "addPhotoA",
    "description": "Add photo",
    "defaultMessage": "Insert photo"
  }
]
Run Code Online (Sandbox Code Playgroud)

ComponentB.json

[
  {
    "id": "addEmoticonB",
    "description": "Add emoji",
    "defaultMessage": "Insert Emoji"
  },
  {
    "id": "addPhotoB",
    "description": "Add picture",
    "defaultMessage": "Insert picture"
  }
]
Run Code Online (Sandbox Code Playgroud)

我需要翻译什么。

final.json

{
  "addEmoticonA": "Insert Emoticon",
  "addPhotoA": "Insert photo",
  "addEmoticonB": "Insert Emoji",
  "addPhotoB": "Insert picture"
}
Run Code Online (Sandbox Code Playgroud)

有什么办法可以完成这项任务?可能是通过使用python脚本或其他任何方式。即从不同的json文件制作一个json文件。或者使用babel-plugin-react-intl直接制作一个json文件

slc*_*ap5 8

There is a translations manager that will do this.

Or for a custom option see below


The script below which is based on this script goes through the translation messages created by babel-plugin-react-intl and creates js files that contain all messages from all components in the json format.

import fs from 'fs'
import {
  sync as globSync
}
from 'glob'
import {
  sync as mkdirpSync
}
from 'mkdirp'
import * as i18n from '../lib/i18n'

const MESSAGES_PATTERN = './_translations/**/*.json'
const LANG_DIR         = './_translations/lang/'
  // Ensure output folder exists
mkdirpSync(LANG_DIR)

// Aggregates the default messages that were extracted from the example app's
// React components via the React Intl Babel plugin. An error will be thrown if
// there are messages in different components that use the same `id`. The result
// is a flat collection of `id: message` pairs for the app's default locale.
let defaultMessages = globSync(MESSAGES_PATTERN)
  .map(filename => fs.readFileSync(filename, 'utf8'))
  .map(file => JSON.parse(file))
  .reduce((collection, descriptors) => {
    descriptors.forEach(({
      id, defaultMessage, description
    }) => {
      if (collection.hasOwnProperty(id))
        throw new Error(`Duplicate message id: ${id}`)

      collection[id] = {
        defaultMessage, description
      }
    })
    return collection
  }, {})

// Sort keys by name
const messageKeys = Object.keys(defaultMessages)
messageKeys.sort()
defaultMessages = messageKeys.reduce((acc, key) => {
  acc[key] = defaultMessages[key]
  return acc
}, {})

// Build the JSON document for the available languages
i18n.en = messageKeys.reduce((acc, key) => {
  acc[key] = defaultMessages[key].defaultMessage
  return acc
}, {})
Object.keys(i18n).forEach(lang => {
  const langDoc = i18n[lang]
  const units = Object.keys(defaultMessages).map((id) => [id, defaultMessages[id]]).reduce((collection, [id]) => {
    collection[id] = langDoc[id] || '';
    return collection;
  }, {});
  fs.writeFileSync(`${LANG_DIR}${lang}.json`, JSON.stringify(units, null, 2))
})
Run Code Online (Sandbox Code Playgroud)