Интернационализация реагирует на применение. Как извлечь сообщения? - PullRequest
0 голосов
/ 07 марта 2020

У меня есть приложение с использованием машинописи, babel, реагировать, и я хочу добавить интернационализацию в свое приложение. Как извлечь мой en. json в другие локали, например.

У меня есть переводы в моем ./src/translations

en. json

{
  "page.login.title: "Login",
  "page.login.link.forgot.passowrd: "Forgot password"
}

ru. json

{
  "page.login.title: "Страница входа",
}

ua. json

{
 "page.login.link.forgot.passowrd: "Забув пароль"
}

Как получить файлы в своей папке ./src/translations, как показано ниже после извлечения

ru. json

{
  "page.login.title: "Login",
  "page.login.link.forgot.passowrd: "Forgot password"
}

ru. json

{
  "page.login.title: "Страница входа",
  "page.login.link.forgot.passowrd: "Forgot password"
}

ua. json

{
 "page.login.title: "Login",
 "page.login.link.forgot.passowrd: "Забув пароль"
}

1 Ответ

0 голосов
/ 07 марта 2020

Я написал сценарий на nodejs

const fs = require('fs');

const MAIN_LANGUAGE = './src/translations/en.json';
const LANG_DIR = './src/translations/';

const mainLocale = JSON.parse(
  fs.readFileSync(MAIN_LANGUAGE, 'utf8', error => {
    if (error) throw error;
  })
);

fs.readdirSync(LANG_DIR).forEach(file => {
  const currentLocale = JSON.parse(
    fs.readFileSync(`${LANG_DIR}/${file}`, 'utf8', error => {
      if (error) throw error;
    })
  );

  const updatedCurrentLocale = JSON.stringify(
    { ...mainLocale, ...currentLocale },
    null,
    2
  );

  fs.writeFileSync(
    `${LANG_DIR}/${file}`,
    updatedCurrentLocale,
    'utf8',
    error => {
      if (error) throw error;
    }
  );

  console.log(`READY LOCALE: ${file}`);
});
...