Skip to content

Google Sheets upload and download i18n json file.

License

Notifications You must be signed in to change notification settings

soapwong703/i18n-gs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

59 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

i18n-gs

This tool is to support Google Sheets upload and download i18n json file.

Installation

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

Usage

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

Commands

Init

Initialize the project with config file

i18n-gs init

Upload

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

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

Authentication

Service Account:

Setup a service account and share sheet's editor permission to the service account

  1. Go to Google Cloud Console
  2. Select or create a project
  3. Search for "Google Sheets API" and enable it
  4. Search for "Service Accounts"
  5. Click "+ Create Service Account" and follow the steps to create service account
  6. Save the generated JSON credential file to local (or your project root folder)
  7. Update the configuration file
  8. Go to the Google Spreadsheet you plan to use
  9. Share Editor permission to the service account in your Google Spreadsheet

Spreadsheet Format

The first row must contain the following header keys

  1. Column A must be named 'key'
  2. The following columns must be named by language code

You can check the template here

Configuration

Create a file i18n-gs.config.js in the project root

Example

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",
  },
};

i18n-gs.config.js fields

spreadsheet.sheetId

Specifies the id of your google sheet

  • Type: string
  • Example:
module.exports = {
  spreadsheet: {
    sheetId: "<your sheet id>",
  },
};

spreadsheet.credential.type

Specifies the method to connect google sheet (Only support service account for now)

  • Type: 'serviceAccount'
  • Example:
module.exports = {
  spreadsheet: {
    credential: {
      type: "serviceAccount",
    },
  },
};

spreadsheet.credential.path

Specifies the path to your credential file (Applicable to service account)

  • Type: string
  • Example:
module.exports = {
  spreadsheet: {
    credential: {
      path: "<your credential file path>",
    },
  },
};

i18n.path

Specifies the path to store your locales files

  • Type: string
  • Example:
module.exports = {
  i18n: {
    path: "<your locale directory path>",
  },
};

i18n.keyStyle

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"
}

i18n.locales.includes

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"],
    },
  },
};

i18n.locales.excludes

Specifies the locales to exclude when upload / download

  • Type: string[]
  • Example:
module.exports = {
  i18n: {
    locales: {
      excludes: ["de", "fr"],
    },
  },
};

i18n.namespaces.includes

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"],
    },
  },
};

i18n.namespaces.excludes

Specifies the namespaces to exclude when upload / download

  • Type: string[]
  • Example:
module.exports = {
  i18n: {
    namespaces: {
      excludes: ["local", "debug"],
    },
  },
};

logging.level

Specifies the log level

  • Type: 'silent' | 'error' | 'warn' | 'info' | 'debug'
  • Example:
module.exports = {
  logging: {
    level: "info",
  },
};