# i18n

Box has a specific i18n workflow for React applications using `react-intl`.

### Old Workflow

It had these steps:

- Engineers would write code with strings and then extract those strings into message.js files.
- Before the code is committed and pushed to their feature branch, the strings would need to be extracted. This is accomplished by running the formatjs babel plugin within babel within webpack during a build target like "yarn start".
- The formatjs plugin would extract strings into a number of files in i18n/json/\*\*.json
- After the json files are extracted, webpack would run the TranslationsPlugin, also from this repo. This would aggregate all of the json files and output i18n/en-US.properties.
- The user would add en-US.properties to their PR as well.
- The PR would get pushed to GHE, and in some projects, a CI check would make sure that the en-US.properties file is in sync with the strings in the source files
- Once the PR is approved, it would be merged to the default branch where the moji automation would find it
- The results of the moji automation was to produce a PR with the translated i18n/<locale>.properties files
- For a build, the TranslationsPlugin would use those translated files and generate i18n/<locale>.js files instead.
- For React applications, the TranslationsPlugin would also generate i18n/bundles/<locale>.js which imports the i18n/<locale>.js file, plus all of the <locale>.properties files from all of the packages it depends upon that are listed in the i18n.config.js file.
- For React libraries, it would not generate bundles. Instead, it just publishes the i18n/\*.properties files for the apps to bundle as needed.

Sometimes, however if the engineer made a change to a string (like a typo fix for example), they might feel like they would not need to run "yarn start" to cause the update to the en-US.properties. If the project did not contain a CI check to guarantee that the en-US.properties was up-to-date, then the string change would not be seen by the moji automation and would not be translated. Additionally, the next engineer would make some unrelated change and then be surprised that the en-US.properties file was updated.

### New Workflow

- Engineers would write code with strings and then [optionally] extract those strings into message.js files.
- Engineers would get their code reviewed and merged to the default branch. That's all they need to do!
- Sometime later, the moji automation runs and generates the translated "i18n/_.ts" or "i18n/_.js" files and sends a PR with those translations
- A package.json script can generate the bundles files locally (i18n/bundles/\*.[jt]s) if needed. (Apps do this, libraries do not.)
- Webpack can then include those files directly without running the TranslationsPlugin

## Contents

This repository contains tools to help with the i18n workflow:

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

- [i18n Folder Structure](#i18n-folder-structure)
  - [What to commit vs. gitignore](#what-to-commit-vs-gitignore)
- [Language](#language)
  - [Example Usage:](#example-usage)
- [buildTranslations.js](#buildtranslationsjs)
- [props2es.js](#props2esjs)
  - [Usage](#usage)
  - [Example Usage (package.json)](#example-usage-packagejson)
- [buildLanguageBundles.js](#buildlanguagebundlesjs)
- [buildBundles.js](#buildbundlesjs)
  - [Usage](#usage-1)
  - [Configuration (i18n.config.js)](#configuration-i18nconfigjs)
  - [Example Usage (package.json)](#example-usage-packagejson-1)
  - [Example Output Bundle file:](#example-output-bundle-file)
- [TranslationsPlugin (Deprecated)](#translationsplugin-deprecated)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

## i18n Folder Structure

This is the standard Box `i18n` structure that your application or library should have under the new localization process:

```
projectRoot
  |-- src/
    |-- components/
      |-- MyComponent/
        |-- messages.ts                // Your source strings (commit this!)
  |-- i18n/
    |-- json/
      |-- en-US.json                   // generated by Moji Jenkins (intermediate file, not committed)
    |-- bundles/                       // applications only, generated by buildBundles from package.json
      |-- en-US.ts                     // generated - combines app + library translations
      |-- ...other language bundles
    |-- en-US.ts                       // generated by Moji Jenkins job
    |-- fr-FR.ts                       // generated by Moji Jenkins job
    |-- ...other language files
  |-- i18n.config.js                   // applications only - lists translation dependencies
...
```

### What to commit vs. gitignore

| File                       | Commit? | Notes                                        |
| -------------------------- | ------- | -------------------------------------------- |
| `src/**/messages.ts`       | ✅ Yes  | Your source strings                          |
| `i18n.config.js`           | ✅ Yes  | Configuration for bundle generation          |
| `i18n/json/*`              | ❌ No\* | Intermediate files generated by Moji Jenkins |
| `i18n/*.ts` or `i18n/*.js` | ❌ No\* | Generated by Moji Jenkins job                |
| `i18n/bundles/*`           | ❌ No\* | Generated by `buildBundles`                  |

\* These files are committed by the Moji Jenkins job only. Engineers should never modify these files by hand. If you would like to see changes, contact the moji team in the #moji channel so that all interested parties are notified.

## Language

The list can be found at https://github.com/box/box-languages.
It is the full list of BCP-47 language tags that are officially supported by Box.

### Example Usage:

```js
const languages = require('@box/languages');
languages.forEach(...do something...);
```

## buildTranslations.js

**_You should not be using this script directly. It should be used via the `props2es.js` script file as mentioned below._**

## props2es.js

This is a stand alone node script for generating language specific ES and properties files from the output of
react-intl's babel plugin or from the output of formatjs-cli. This script will compile all `i18n/*.properties` files into `i18n/*.js` (or `i18n/*.ts`) files. You can use this scrip to do the same thing that the TranslationsPlugin used to do.

However, when you migrate to the new localization process, you will not need this tool any more. The moji jenkins
job will be generating the js or ts files as needed.

### Usage

```
props2es [i18nDir] [--typescript]
```

- `i18nDir` (optional): Path to the i18n directory. If not provided, defaults to `i18n` folder in the project root.
- `--typescript` (optional): Generate `.ts` files instead of `.js` files.

### Example Usage (package.json)

```js
...
  scripts: {
    ...,
    "build:i18n": "props2es",
    "build:i18n:custom": "props2es ./src/custom-i18n",
    "build:i18n:ts": "props2es --typescript",
    "build:i18n:custom:ts": "props2es ./src/custom-i18n --typescript"
    ...
  }
...
```

## buildLanguageBundles.js

**_You should not be using this script directly. It should be used via the `buildBundles.js` CLI script mentioned below._**

## buildBundles.js

This is a stand alone node script for generating language bundle files. This script wraps `buildLanguageBundles.js` and reads `translationDependencies` from your project's `i18n.config.js` file.

### Usage

```
buildBundles [i18nDir] [--typescript]
```

- `i18nDir` (optional): Path to the i18n directory. If not provided, defaults to `i18n` folder in the project root.
- `--typescript` (optional): Generate `.ts` files instead of `.js` files.

### Configuration (i18n.config.js)

Create an `i18n.config.js` file in your project root:

```js
module.exports = {
  translationDependencies: ['box-react-ui', 'box-ui-elements'],
};
```

### Example Usage (package.json)

```js
...
  scripts: {
    ...,
    "build:bundles": "buildBundles",
    "build:bundles:custom": "buildBundles ./src/locales",
    "build:bundles:ts": "buildBundles --typescript"
    ...
  }
...
```

### Example Output Bundle file:

```js
/**
 * Auto-generated Language Bundle File for fi-FI
 * @IMPORTANT: DO NOT MODIFY THIS FILE DIRECTLY
 */

import boxCldrData from '@box/cldr-data/locale-data/fi-FI';
import messageData0 from 'box-react-ui/i18n/fi-FI';
import messageData1 from 'box-ui-elements/i18n/fi-FI';
import appMessageData from '../fi-FI';

const language = 'fi-FI';
const locale = 'fi';

const messages = {
  ...messageData0,
  ...messageData1,
  ...appMessageData,
};

export { language, locale, messages, boxCldrData };
```

## TranslationsPlugin (Deprecated)

> **⚠️ DEPRECATED:** This webpack plugin is deprecated. Use the standalone scripts instead:
>
> - `props2es` for building translation files until the project has migrated to the new localization process
> - `buildBundles` for generating language bundles

See [TranslationsPlugin Documentation](../webpack/README.md#translationsplugin-deprecated) for legacy usage.
