### Install easygettext with npm
Source: https://github.com/polyconseil/easygettext/blob/master/README.md
Install easygettext as a development dependency using npm.
```bash
npm install --save-dev easygettext
```
--------------------------------
### Install easygettext with yarn
Source: https://github.com/polyconseil/easygettext/blob/master/README.md
Install easygettext as a development dependency using yarn.
```bash
yarn add --dev easygettext
```
--------------------------------
### HTML token extraction examples
Source: https://github.com/polyconseil/easygettext/blob/master/README.md
Demonstrates various ways to mark strings for translation in HTML using the 'translate' attribute and directives like 'i18n' and 'get-text'. Supports context, comments, and plurals.
```html
Hello World
```
```html
Hello World
```
```html
Hello World
```
```html
Hello World
```
```html
```
```html
```
```html
Hello World
```
```html
Hello World
```
```html
Hello World
```
```html
```
```html
```
```html
```
```html
{{:: 'Something …' |translate}}
```
```html
```
```html
```
--------------------------------
### Javascript gettext token extraction examples
Source: https://github.com/polyconseil/easygettext/blob/master/README.md
Demonstrates the usage of $gettext, $pgettext, and $ngettext for extracting translatable strings from Javascript code. Supports concatenated and template string literals.
```javascript
const myVar = $gettext("My fantastic msgid")
```
```javascript
const myConcatVar = $gettext(
"My"
+ "fantastic"
+ "msgid"
)
```
```javascript
const myTempVar = $gettext(
`My
fantastic
msgid`
)
```
```javascript
const myContextualizedVar = $pgettext("some context", "Some other string")
```
```javascript
const myPluralVar = $ngettext(...)
```
--------------------------------
### Supported HTML translation token formats
Source: https://context7.com/polyconseil/easygettext/llms.txt
Examples of various HTML attributes, tags, and interpolation patterns recognized by the extraction tool.
```html
Hello World
Hello World
Hello World
Hello World
Hello World
Hello WorldHello WorldHello World{{:: 'Bind once text' | translate }}
Content
```
--------------------------------
### AngularJS HTML Translation Examples
Source: https://github.com/polyconseil/easygettext/blob/master/README.md
These HTML snippets demonstrate different ways translations can be embedded in AngularJS templates, including interpolation and filter usage. The extraction tool needs to be run twice to cover all cases.
```html
```
```html
{{::'Link text' |translate}}{{'Link text' |translate}}
```
--------------------------------
### AngularJS Translation Extraction (Interpolation)
Source: https://github.com/polyconseil/easygettext/blob/master/README.md
For AngularJS, extract translations using specific start and end delimiters for interpolation, and optionally a filter prefix. This command targets interpolation within `{{ }}`.
```bash
--startDelimiter '{{' --endDelimiter '}}' --filterPrefix '::'
```
--------------------------------
### AngularJS Translation Extraction (Direct)
Source: https://github.com/polyconseil/easygettext/blob/master/README.md
This command extracts translations from AngularJS templates without specific delimiters, useful for cases where translations are directly bound or filtered. It uses empty start and end delimiters.
```bash
--output ${html_b} --startDelimiter '' --endDelimiter '' --filterPrefix '::'
```
--------------------------------
### Run Jest Tests
Source: https://github.com/polyconseil/easygettext/blob/master/README.md
Execute the project's tests using the Jest testing framework. This command is typically run from the project's root directory.
```bash
npm test
```
--------------------------------
### preprocessTemplate()
Source: https://context7.com/polyconseil/easygettext/llms.txt
Preprocesses template content from various formats like Pug, Jade, or Vue into standard HTML for extraction.
```APIDOC
## preprocessTemplate()
### Description
Preprocess template content from Pug/Jade or Vue files into HTML for extraction.
### Parameters
- **content** (string) - Required - The template source code.
- **type** (string) - Required - The format type (e.g., 'html', 'pug', 'vue').
### Response
- **string|array** - Returns the processed HTML string or an array of strings if multiple templates are found.
```
--------------------------------
### Parser options for gettext-extract
Source: https://github.com/polyconseil/easygettext/blob/master/README.md
Available options for the --parser flag: auto, acorn, or babel.
```bash
--parser auto|acorn|babel
```
--------------------------------
### Extract gettext tokens from HTML, Pug, Vue, and JS files
Source: https://github.com/polyconseil/easygettext/blob/master/README.md
Invoke the gettext-extract tool on various file types to generate a POT dictionary template. The --output argument specifies the output file.
```bash
gettext-extract --output dictionary.pot foo.html bar.pug component.vue sourcefile.js
```
--------------------------------
### Extract Gettext from Multiple Files via npm Script
Source: https://github.com/polyconseil/easygettext/blob/master/README.md
This npm script demonstrates extracting gettext strings from all `.vue` files within a specified directory using `gettext-extract` and the `find` command. Adapt the file pattern and directory for other file types.
```json
{
//...
"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')"
}
}
```
--------------------------------
### Programmatic Extraction with Extractor Class
Source: https://context7.com/polyconseil/easygettext/llms.txt
Instantiate the `Extractor` class to programmatically parse files and generate POT output. Configure options like line numbers, attributes, filters, and delimiters.
```javascript
const { Extractor } = require('easygettext/src/extract.js');
// Create extractor with options
const extractor = new Extractor({
lineNumbers: true,
attributes: ['translate', 'i18n', 'v-translate'],
filters: ['translate', 'i18n'],
startDelimiter: '{{',
endDelimiter: '}}',
filterPrefix: '::',
removeHTMLWhitespaces: false,
});
// Parse HTML content
extractor.parse('greeting.html', '
Hello world
');
// Parse HTML with context and comments
extractor.parse('greeting.html', "\
Hello world
");
// Parse HTML with plurals
extractor.parse('items.html', "\
There is one item
");
// Extract from JavaScript code
extractor.parseJavascript('app.js', "\
const greeting = this.$gettext(\"Hello there!\");
const contextual = this.$pgettext(\"menu\", \"Home\");
const plural = this.$ngettext(\"%{ n } item\", \"%{ n } items\", count);
const contextualPlural = this.$npgettext(\"cart\", \"%{ n } item\", \"%{ n } items\", count);
");
// Extract from Vue component (handles both template and script)
extractor.extract('Component.vue', 'vue', "\
Welcome
{{ $gettext('Click here') }}
");
// Generate POT output
const potContent = extractor.toString();
console.log(potContent);
// Output:
// msgid ""
// msgstr ""
// \"Content-Type: text/plain; charset=utf-8\\n\"
// \"Content-Transfer-Encoding: 8bit\\n\"
// \"Generated-By: easygettext\\n\"
// \"Project-Id-Version: \\n\"
//
// #: greeting.html:2
// msgid \"Hello world\"
// msgstr ""
```
--------------------------------
### Test CLI with Attributes
Source: https://github.com/polyconseil/easygettext/blob/master/README.md
This command tests the CLI tool by specifying attributes for extraction and an output file. It's useful for verifying the functionality of the extraction process with custom attributes.
```bash
./src/extract-cli.js --attribute v-translate --attribute v-i18n ~/output.html
```
--------------------------------
### preprocessScript()
Source: https://context7.com/polyconseil/easygettext/llms.txt
Extracts and preprocesses script content from Vue single-file components.
```APIDOC
## preprocessScript()
### Description
Extract and preprocess script content from Vue single-file components.
### Parameters
- **component** (string) - Required - The full Vue component source code.
- **type** (string) - Required - The file type (e.g., 'vue').
### Response
- **array** - An array of objects containing 'content' (the script code) and 'lang' (the language, e.g., 'js' or 'ts').
```
--------------------------------
### Compile PO Files to JSON
Source: https://github.com/polyconseil/easygettext/blob/master/README.md
Use `gettext-compile` to convert one or more PO files into a sanitized JSON output file. This is useful for integrating translations into applications.
```bash
gettext-compile --output translations.json fr.po en.po de.po
```
--------------------------------
### Extract Translation Tokens with gettext-extract
Source: https://context7.com/polyconseil/easygettext/llms.txt
Use `gettext-extract` to generate POT files from source code. Specify custom attributes, parsers, delimiters, or filters as needed.
```bash
gettext-extract --output dictionary.pot foo.html bar.pug component.vue sourcefile.js
```
```bash
gettext-extract --attribute v-translate --output dictionary.pot src/*.vue
```
```bash
gettext-extract --attribute v-translate --attribute v-i18n --output dictionary.pot src/*.vue
```
```bash
gettext-extract --parser babel --output dictionary.pot src/*.js
```
```bash
gettext-extract --startDelimiter '[#' --endDelimiter '#]' --output dictionary.pot templates/*.html
```
```bash
gettext-extract --removeHTMLWhitespaces --output dictionary.pot templates/*.html
```
```bash
gettext-extract --filterPrefix '::' --output dictionary.pot templates/*.html
```
```bash
gettext-extract --attribute v-translate --output dictionary.pot $(find src/components -type f -name '*.vue')
```
--------------------------------
### CLI usage for gettext-extract
Source: https://github.com/polyconseil/easygettext/blob/master/README.md
Command-line interface options for gettext-extract, including attribute, prefix, output file, and parser.
```bash
gettext-extract [--attribute EXTRA-ATTRIBUTE] [--filterPrefix FILTER-PREFIX] [--output OUTFILE] [--parser auto|acorn|babel]
```
--------------------------------
### Compile PO Files to JSON with gettext-compile
Source: https://context7.com/polyconseil/easygettext/llms.txt
Use `gettext-compile` to convert PO files into a JSON format for JavaScript applications. Multiple PO files can be combined into a single JSON output.
```bash
gettext-compile --output translations.json fr.po en.po de.po
```
```bash
gettext-compile fr.po en.po
```
--------------------------------
### Preprocess template content with preprocessTemplate()
Source: https://context7.com/polyconseil/easygettext/llms.txt
This function preprocesses template content from various formats (HTML, Pug, Vue) into HTML. It's used for extracting translatable strings from templates. Specify the source language using the second argument.
```javascript
const { preprocessTemplate } = require('easygettext/src/extract.js');
// HTML passthrough
const html = preprocessTemplate('
hello
', 'html');
console.log(html); // '
hello
'
// Pug/Jade to HTML
const pug = preprocessTemplate('h1 hello', 'pug');
console.log(pug); // '
', '']
```
--------------------------------
### Extract gettext tokens from Javascript files
Source: https://github.com/polyconseil/easygettext/blob/master/README.md
Invoke gettext-extract on Javascript files to extract translatable strings using predefined tokens.
```bash
gettext-extract somefile.js
```
--------------------------------
### Custom attribute and delimiters for gettext-extract
Source: https://github.com/polyconseil/easygettext/blob/master/README.md
Customize the attribute used for translation tokens and change the start/end delimiters for template strings.
```bash
gettext-extract --attribute v-translate --output dictionary.pot foo.html bar.jade
```
```bash
gettext-extract --attribute v-translate --attribute v-i18n --output dictionary.pot foo.html bar.jade
```
```bash
gettext-extract --startDelimiter '[#' --endDelimiter '#]' --output dictionary.pot foo.html bar.jade
```
--------------------------------
### Use Babel parser for advanced JS features
Source: https://github.com/polyconseil/easygettext/blob/master/README.md
Set the --parser babel option to enable parsing of advanced JavaScript features like optional-chaining and nullish coalescing.
```bash
gettext-extract --parser babel --output dictionary.pot foo.html
```
--------------------------------
### Convert PO content to JSON with compile.po2json()
Source: https://context7.com/polyconseil/easygettext/llms.txt
Use this function to transform PO file content into a JSON dictionary. This is useful for integrating translations into JavaScript applications. It handles basic messages, context-specific messages, and plural forms.
```javascript
const { po2json } = require('easygettext/src/compile.js');
const poContent = `
msgid ""
msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"
"Language: fr\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
msgid "Hello, you!"
msgstr "Salut, toi!"
msgctxt "In spanish"
msgid "Hello, you!"
msgstr "Hola ustedes!"
msgid "(1 attempt)"
msgid_plural "({{ remaining }} attempts)"
msgstr[0] "(1 tentative)"
msgstr[1] "({{ remaining }} tentatives)"
`;
const result = po2json(poContent);
console.log(JSON.stringify(result, null, 2));
```
--------------------------------
### Bash Script for AngularJS Extraction and Merging
Source: https://github.com/polyconseil/easygettext/blob/master/README.md
This Bash script orchestrates the extraction of gettext strings from HTML files using two different configurations and JavaScript files. It then merges these into a single POT file using `msgcat` and cleans up temporary files.
```bash
#!/usr/bin/env bash
input_files="$(find ./src/ -iname \*.html)"
workdir=$(mktemp -d "${TMPDIR:-/tmp/}$(basename $0).XXXXXXXXXXXX") || exit 1
html_a=${workdir}/messages-html-interpolate.pot
html_b=${workdir}/messages-html.pot
./dist/extract-cli.js --output ${html_a} --startDelimiter '{{' --endDelimiter '}}' --filterPrefix '::' ${input_files}
./dist/extract-cli.js --output ${html_b} --startDelimiter '' --endDelimiter '' --filterPrefix '::' ${input_files}
# Extract gettext “messages” from JavaScript files here, into ${es_a} …
es_a=${workdir}/ecmascript.pot
# [...] > ${es_a}
# Merge the different catalog templates with `msgcat`:
merged_pot=${workdir}/merged.pot
msgcat ${html_a} ${html_b} ${es_a} > ${merged_pot}
# Cleanup, in case `msgcat` gave merge-conflicts in catalog header.
header=${workdir}/header.pot
sed -e '/^$/q' < ${html_a} > ${header}
body=${workdir}/body.pot
sed '1,/^$/d' < ${merged_pot} > ${body}
cat ${header} ${body} > ${output_file}
# Remove temporary directory with working files.
rm -r ${workdir}
```
--------------------------------
### compile.po2json()
Source: https://context7.com/polyconseil/easygettext/llms.txt
Converts raw PO file content into a structured JSON dictionary suitable for JavaScript applications.
```APIDOC
## compile.po2json()
### Description
Converts PO file content to a JSON dictionary structure for use in JavaScript applications.
### Parameters
- **poContent** (string) - Required - The raw string content of a .po file.
### Response
- **headers** (object) - Contains PO file metadata like Language and Plural-Forms.
- **messages** (object) - A dictionary mapping msgid to translations, handling contexts and plurals.
```
--------------------------------
### Extract from Vue Component
Source: https://github.com/polyconseil/easygettext/blob/master/README.md
Use `gettext-extract` to extract translatable strings from Vue.js components. Ensure the component uses the specified template syntax for translatable content.
```bash
gettext-extract MyComponent.vue
```
```html
{{ greeting_message }}
{{ number_of_people_here }}
Some text to be translated
```
--------------------------------
### Extracting Translation Tokens from HTML Fragments
Source: https://github.com/polyconseil/easygettext/blob/master/README.md
Demonstrates how JavaScript expressions within HTML translate filters are parsed to extract translatable strings. This is useful for frameworks like AngularJS.
```html
```
--------------------------------
### Extract script content from Vue components with preprocessScript()
Source: https://context7.com/polyconseil/easygettext/llms.txt
Extracts and preprocesses script content from Vue single-file components. It supports both JavaScript and TypeScript, identifying the language automatically. This is crucial for extracting translatable strings from component logic.
```javascript
const { preprocessScript } = require('easygettext/src/extract.js');
// Extract JavaScript from Vue component
const vueComponent = `