Skip to content

Instantly share code, notes, and snippets.

@fraserxu
Created September 18, 2015 03:42
Show Gist options
  • Save fraserxu/6ed0d41f0e3da0475a33 to your computer and use it in GitHub Desktop.
Save fraserxu/6ed0d41f0e3da0475a33 to your computer and use it in GitHub Desktop.
Steps for the translation workflow in a Electron application:

Steps for the translation workflow in a Electron application:

  1. Requirements
  • jsxgettext - this is a node module to generate po file from JavaScript file npm install jsxgettext --save-dev to install
  • gettext - native gettext tool to merge po files brew install gettext and run brew link gettext --force if needed.
  • node-gettext - this is a node module to parse and read PO file npm install node-gettext --save to install
  1. Prepare the folder structure

create a locale directory to put the origianl po files

.
├── en.new.po
├── en.po
├── my.new.po
└── my.po
  1. Prepare a Makefile and add the genPo task

bundle.js is the JavaScript file which will use to generate the new PO file

NODE_BIN = node_modules/.bin

genPo:
  $(NODE_BIN)/jsxgettext -j dist/bundle.js > locale/my.new.po
  $(NODE_BIN)/jsxgettext -j dist/bundle.js > locale/en.new.po
  msgmerge locale/my.po locale/my.new.po -o locale/my.po
  msgmerge locale/en.po locale/en.new.po -o locale/en.po

.PHONY: genPo

  1. Run make genPo to generate and merge the po files
  2. Use it in the client

First you need to set up gettext

/**
 * Getttext
 */
var gettext = require('node-gettext')
var fs = require('fs')

gettext = new gettext()

// add the language you need, here `my` means Myanmar
gettext.addTextdomain('en', fs.readFileSync(path.join(__dirname, './locale/en.po')))
gettext.addTextdomain('my', fs.readFileSync(path.join(__dirname, 'locale/my.po')))

// set the default domain
gettext.textdomain('my')

And finally you can use it in you HTML or whatever template language you have, here I use JSX with React. In the render function of a component

render () {
  <span>{GT.gettext('Some really weird language here...')}</span>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment