Gathering detailed insights and metrics for bc-gettext-utils
Gathering detailed insights and metrics for bc-gettext-utils
Gathering detailed insights and metrics for bc-gettext-utils
Gathering detailed insights and metrics for bc-gettext-utils
npm install bc-gettext-utils
Typescript
Module System
Node Version
NPM Version
JavaScript (98.99%)
C# (0.39%)
Vue (0.31%)
HTML (0.16%)
PHP (0.15%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
25 Commits
2 Watchers
1 Branches
1 Contributors
Updated on Jul 28, 2022
Latest Version
1.1.1
Package Id
bc-gettext-utils@1.1.1
Unpacked Size
58.59 kB
Size
11.29 kB
File Count
18
NPM Version
8.19.2
Node Version
16.18.1
Published on
Jul 18, 2023
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
3
bc-gettext-utils
is a toolset for Node.js which helps creating and updating translations in a format compatible with gettext .po
and .mo
files. It extracts translatable strings from JavaScript, .NET and PHP source files and merges extracted translations with existing ones.
In order to load and save translations in .po
and .mo
formats, use the gettext-parser library.
Install using npm:
1npm install bc-gettext-utils
Use the extractors
collection of functions to extract translatable strings from source files, e.g.:
1const { translationBuilder, extractors } = require( 'bc-gettext-utils' ); 2 3const builder = translationBuilder(); 4 5builder.add( file, extractors.js( text, [options] ) ); 6builder.add( file, extractors.vue( text, [options] ) ); 7builder.add( file, extractors.cs( text, [options] ) ); 8builder.add( file, extractors.cshtml( text, [options] ) ); 9builder.add( file, extractors.xaml( text, [options] ) ); 10builder.add( file, extractors.php( text, [options] ) ); 11 12const translations = builder.translations; 13const count = builder.count;
Where:
file
is the path of the source file to be included in the commentstext
is the source file contents as a UTF-8 stringoptions
is the optional configuration object (see below)Available extractors:
extractors.js
- JavaScript filesextractors.vue
- Vue single-file componentsextractors.cs
- C# filesextractors.cshtml
- Razor pages and MVC viewsextractors.xaml
- XAML filesextractors.php
- PHP filesThe object returned by builder.translations
follows the format used by gettext-parser, e.g.:
1{ 2 "": { 3 "example": { 4 "msgid": "example", 5 "msgstr": [ "" ], 6 "comments": { 7 "reference": "/path/to/file:123" 8 } 9 } 10 }, 11 "context": { 12 "another example": { 13 "msgctxt": "context", 14 "msgid": "another example", 15 "msgstr": [ "" ], 16 "comments": { 17 "reference": "/path/to/file:256" 18 } 19 } 20 } 21}
The number of extracted unique messages is available as builder.count
.
The following functions or methods are recognized in JavaScript and C# code:
1_( "text" ); 2_p( "context", "text" ); 3_n( "text", "plural text" ); 4_pn( "context", "text", "plural text" );
Additional arguments are ignored. The names of these functions can be customized by passing additional options to the extractor, for example:
1builder.add( file, extractors.js( text, file, { 2 string: '_', 3 particularString: '_p', 4 pluralString: '_n', 5 particularPluralString: '_pn', 6} ) );
Multiple names can be specified by passing an array.
Note that string literals must be used for the extraction to work. In JavaScript, 'single quoted'
and "double quoted"
strings are supported. In C#, "regular"
and @"verbatim"
string literals can be used.
Concatenation of multiple string literals using the +
operator is also supported:
1_( "this is a long text\n" 2 + "and this is another line" );
In C#, translatable strings are also extracted from the Display
attribute and the ErrorMessage
property of validation attributes, for example:
1[Required( ErrorMessage = "This field is required." )] 2[Display( Name = "First Name" )] 3public string FirstName { get; set; }
The names of the attribute and property can be customized by passing additional options to the extractor, for example:
1builder.add( file, extractors.cs( text, file, { 2 displayAttribute: 'Display', 3 errorMessageProperty: 'ErrorMessage', 4} ) );
In Vue single-file components, translatable strings can be placed in the following locations:
<p>{{ _( 'text' ) }}</p>
<a v-bind:title="_( 'text' )">
<a :title="_( 'text' )">
<script>
blockIn Razor .cshtml
files, translatable strings are extracted from:
@_( "text" )
@{ string title = _( "text" ); }
@if ( a > 0 ) { title = _( "text" ); }
@functions
directiveIn XAML files, by default, translatable strings are extracted from i18n:Translate
, i18n:Format
and i18n:MultiFormat
markup extensions, for example:
<Label Content="{i18n:Translate text}"/>
<Label Content="{i18n:Translate "hello, world", Context=context}"/>
<Label Content="{Binding Count, Converter={i18n:Format 'a dog', PluralText='{0} dogs'}}"/>
The element syntax is also supported:
1<Label> 2 <Label.Content> 3 <i18n:Translate Context="context">another example</i18n:Translate> 4 </Label.Content> 5</Label>
The names of extensions and their attributes can be customized by passing additional options to the extractor, for example:
1builder.add( file, extractors.xaml( text, file, { 2 extensions: [ 'i18n:Translate', 'i8n:Format', 'i8n:MultiFormat' ], 3 textAttribute: 'Text', 4 pluralTextAttribute: 'PluralText', 5 contextAttribute: 'Context', 6} ) );
In PHP files, translatable strings are extracted from code blocks starting with <?php
or <?=
.
The order of arguments can be changed to support WordPress internationalization functions, by setting the reverseContext
option to true
:
1builder.add( file, extractors.php( text, file, { 2 string: [ '__', '_e', 'esc_html__', 'esc_html_e', 'esc_attr__', 'esc_attr_e' ], 3 particularString: [ '_x', '_ex', 'esc_html_x', 'esc_attr_x' ], 4 pluralString: '_n', 5 particularPluralString: '_nx', 6 reverseContext: true, 7} ) );
The following function can be used to merge existing, already translated messages, with newly extracted translations:
1const { mergeTranslations } = require( 'bc-gettext-utils' ); 2 3const { translations, added, updated, deleted } = mergeTranslations( existingTranslations, newTranslations );
The existingTranslations
, newTranslations
and the returned translations
all follow the format used by gettext-parser.
In addition to the merged translations, this function returns the following information:
added
the number of new translations which were not included in existing translations and have been addedupdate
the number of translations which existed before, but the reference or plural string has been updateddeleted
the number of existing translations which were not included in new translations and have been removedThe existing translations, translator comments and flags are preserved.
The following function can be used to normalize plurals in merged translations:
1const { normalizePlurals } = require( 'bc-gettext-utils' ); 2 3const normalizedTranslations = normalizePlurals( translations, 2 );
It ensures that the plural messages contain the specified number of translated strings, and singular messages contain exactly one translated string.
Use the compareReference
function to sort translations by file path and line number. This can be used when creating a .po
file using gettext-parser:
1const { compareReference } = require( 'bc-gettext-utils' ); 2const gettextParser = require( 'gettext-parser' ); 3 4const data = { headers, translations }; 5const output = gettextParser.po.compile( data, { sort: compareReference } );
No vulnerabilities found.
No security vulnerabilities found.