This tool is to support Google Sheets upload and download i18n json file.
As a dependency in a package
npm install i18n-gs --save-dev
# or
yarn add i18n-gs --dev
Globally with npm
npm install i18n-gs -g
On-demand:
You can use npx
to run it without installation
npx i18n-gs
Installed as a dependency:
npx i18n-gs
# or
yarn i18n-gs
Installed globally:
i18n-gs
Initialize the project with config file
i18n-gs init
Upload the files to google sheet (only support flat key style)
i18n-gs upload [namespaces...]
Options:
-l, --locales <locales...> locales to be included
- Example:
i18n-gs upload
i18n-gs upload common
i18n-gs upload common --locales en
Download the files from google sheet
i18n-gs download [namespaces...]
Options:
-l, --locales <locales...> locales to be included
- Example:
i18n-gs download
i18n-gs download common
i18n-gs download common --locales en
Setup a service account and share sheet's editor permission to the service account
- Go to Google Cloud Console
- Select or create a project
- Search for "Google Sheets API" and enable it
- Search for "Service Accounts"
- Click "+ Create Service Account" and follow the steps to create service account
- Save the generated JSON credential file to local (or your project root folder)
- Update the configuration file
- Go to the Google Spreadsheet you plan to use
- Share Editor permission to the service account in your Google Spreadsheet
The first row must contain the following header keys
- Column A must be named 'key'
- The following columns must be named by language code
You can check the template here
Create a file i18n-gs.config.js
in the project root
module.exports = {
spreadsheet: {
sheetId: "<your sheet id>",
credential: {
type: "serviceAccount",
path: "<your credential file path>",
},
},
i18n: {
path: "<your locale directory path>",
keyStyle: "nested",
locales: {
includes: ["en"],
excludes: [],
},
namespaces: {
includes: ["common"],
excludes: [],
},
},
logging: {
level: "info",
},
};
Specifies the id of your google sheet
- Type:
string
- Example:
module.exports = {
spreadsheet: {
sheetId: "<your sheet id>",
},
};
Specifies the method to connect google sheet (Only support service account for now)
- Type:
'serviceAccount'
- Example:
module.exports = {
spreadsheet: {
credential: {
type: "serviceAccount",
},
},
};
Specifies the path to your credential file (Applicable to service account)
- Type:
string
- Example:
module.exports = {
spreadsheet: {
credential: {
path: "<your credential file path>",
},
},
};
Specifies the path to store your locales files
- Type:
string
- Example:
module.exports = {
i18n: {
path: "<your locale directory path>",
},
};
Style of the i18n key on local file
- Type:
'nested' | 'flat'
- Example:
module.exports = {
i18n: {
keyStyle: "nested",
},
};
The key style is as follows:
{
// nested:
blog: {
section: {
title: "My first blog";
}
},
// flat:
blog.section.title: "My first blog"
}
Specifies the locales to include when upload / download
Note: If the provided array is empty, nothing will be included when performing action
- Type:
string[]
- Example:
module.exports = {
i18n: {
locales: {
includes: ["en", "ja"],
},
},
};
Specifies the locales to exclude when upload / download
- Type:
string[]
- Example:
module.exports = {
i18n: {
locales: {
excludes: ["de", "fr"],
},
},
};
Specifies the namespaces to include when upload / download
Note: If the provided array is empty, nothing will be included when performing action
- Type:
string[]
- Example:
module.exports = {
i18n: {
namespaces: {
includes: ["common", "glossary"],
},
},
};
Specifies the namespaces to exclude when upload / download
- Type:
string[]
- Example:
module.exports = {
i18n: {
namespaces: {
excludes: ["local", "debug"],
},
},
};
Specifies the log level
- Type:
'silent' | 'error' | 'warn' | 'info' | 'debug'
- Example:
module.exports = {
logging: {
level: "info",
},
};