You can install the easygettext package by running
npm install --save-dev easygettext
or
yarn add --dev easygettext
Usage & Examples
HTML token extraction
Simply invoke the tool on the templates you want to extract a POT dictionary template from.
The optional '--output' argument enables you to directly output to a file.
We recognize the $gettext, $pgettext and $ngettext tokens as the ones from which we can extract text from.
Those tokens are frozen for now, but feel free to make a pull request and add support for variable ones :)
We currently can't extract template strings with variables though.
Extract from Vue components
You can also extract the strings marked as translatable inside the <script> and <template> sections of Vue.js components:
gettext-extract MyComponent.vue
With a component that looks like this:
<template> <h1>{{ greeting_message }}</h1> <p>{{ number_of_people_here }}</p> <h2 v-translate> Some text to be translated </template> <script> export default { name: "greetings", computed: { greeting_message() { return this.$gettext("Hello there!") }, number_of_people_here(nb_folks) { return this.$ngettext
The Javascript & HTML (or Pug) extraction within a Vue component works with the same rules as stated upper in this document.
Extracting from multiple files
gettext-extract needs the exact file paths to work. If you want to extract gettext from all files in a folder, you can use the UNIX find command. Here is an example as a npm script:
{ //... "scripts": { // This is for VueJS files, please adapt for HTML or Jade/Pug templates "extract-gettext-cli": "gettext-extract --attribute v-translate --output dictionary.pot $(find scripts/src/components -type f -name '*.vue')" }}
gettext-compile
Outputs or writes to an output file, the sanitized JSON version of a PO file.
angular-gettext is a very neat tool, that comes with Grunt tooling
to extract translation tokens from your Pug/Jade/HTML templates and JavaScript code.
Unfortunately, this has two drawbacks:
It isn't a simple command-line interface, and forces the usage of Grunt;
It is angular-specific.
This library comes up with two simple CLI tools to extract and compile your HTML tokens.
Why This Library?
Our frontend toolchain, systematic doesn't rely on Grunt/Gulp/Broccoli/...
and uses a combination of simple Makefiles and CLI tools to do the job.
The toolchain being framework-agnostic, we don't want to depend on Angular to extract our HTML translation tokens.
On another note, we use the standard xgettext
tool to extract our JavaScript translation tokens.
Nevertheless, the way angular-gettext does it (with tokens, directly in HTML) is elegant, is used by many other
libraries and will also be the way to do it in Angular2.
Also, by utilizing either acorn or babel, this tool will parse and compile typical JavaScript expressions used in translate-filter expressions. For example, exposed to a (AngularJS-based) fragment like
Which will be correctly looked up and translated during runtime, at least by angular-gettext.
Known Issues
TypeScript support is currently limited in that line numbers are not tracked and won't show in generated .po files. This can lead to issues with more complex translations and should be kept in mind.
Credits
Thanks a million to @rubenv for the initial ideas and
implementations in angular-gettext-tools, which inspired me a lot.