### 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 World Hello World Hello 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', "\ "); // 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); // '

hello

' // Vue template extraction const vue = preprocessTemplate('', 'vue'); console.log(vue); // '

hello

' // Vue with Pug template const vuePug = preprocessTemplate("", 'vue'); console.log(vuePug); // '

hello

' // Vue with multiple templates (NativeScript) const multiVue = preprocessTemplate(` `, 'vue'); console.log(multiVue); // ['

Web

', '