0.16.0 • Published 4 years ago

gulp-html-i18n v0.16.0

Weekly downloads
502
License
-
Repository
github
Last release
4 years ago

gulp-html-i18n

Internationalize your HTML files with gulp!

Build Status codecov

Language Definition Files

gulp-html-i18n supports three formats for definition files: JavaScript, JSON, and YAML

JS

Given the following in a file named: lang/en-US/index.js

AMD

define({
  heading: "Welcome!",
  footer:  "Copyright 2015"
});

CommonJS

module.exports = {
  heading: "Welcome!",
  footer:  "Copyright 2015"
};

gulp-html-i18n will produce an object called index. You can then use ${{ index.heading }}$ to get a result of "Welcome!".

JSON

Given the following in a file named: lang/en-US/index.json

{
  "heading": "Welcome!",
  "footer":  "Copyright 2015"
}

gulp-html-i18n will produce an object called index. You can then use ${{ index.heading }}$ to get a result of "Welcome!".

YAML

Given the following in a file named: lang/en-US/index.yaml

heading: Welcome!
footer:  Copyright 2015

gulp-html-i18n will produce an object called index. You can then use ${{ index.heading }}$ to get a result of "Welcome!".

HTML Markup

To use either of the examples from above, replace the text in your HTML files with a formatted tag: ${{ library.tag.name }}$

${{ _lang_ }}$, ${{ _langs_ }}$, ${{ _default_lang_ }}$, ${{ _filename_ }}$, and ${{ _filepath_ }}$ are special markups, stand for current file language, all file languages, the defaultLang option, the output file name and the output file path relative to file.base.

Example: index.html

Initial:

<html>
  <body>
    <h1>${{ index.heading }}$</h1>
    <div>
      <!-- Website content -->
    </div>
    <div>${{ index.footer }}$</div>
  <body>
</html>

Output:

<html>
  <body>
    <h1>Welcome!</h1>
    <div>
      <!-- Website content -->
    </div>
    <div>Copyright 2015</div>
  <body>
</html>

Render Engine

gulp-html-i18n supports two renderEngines: regex, mustache

Regex

This is the default and is used either with the langRegExp (the most flexible) or delimiters (easier)

Mustache

Provides additional support for things like loops and conditionals. (for full mustache documentation) You must used delimiters for mustache, you cannot use langRegExp option

en/index.yaml Yaml is useful for multiline strings

home:
    paragraphs:
        - >
            First paragraph contents 
            put together in multiple lines
        - >
            Second paragraph
            also in multiple lines
        - Third Paragraph

gulpfile.js

i18n({
  langDir: './lang',
  renderEngine: 'mustache'
})

index.html

<h1>Welcome</h1>
${{# home.paragraphs }}$
    <p>${{ . }}$</p>
${{/ home.paragraphs }}$

Will produce : index-en.html

<h1>Welcome</h1>
<p>First paragraph contents put together in multiple lines</p>
<p>Second paragraph also in multiple lines</p>
<p>Third Paragraph</p>

Gulp Usage

The following task:

gulp.task('build:localize', function() {
  var dest  = './public';
  var index = './index.html';

  return gulp.src(index)
    .pipe(i18n({
      langDir: './lang',
      trace: true
    }))
    .pipe(gulp.dest(dest));
});

will compile index.html to public/index-{lang}.html for each language your define in ./lang.

Options

OptionDefaultTypeDescription
langDir (required)undefinedStringSpecifies the path to find definitions
filenameI18nfalseBooleanIf true, you can use ${{ xxx }}$ tag in your filename as in the file content, then the translated filename will contain the translated content instead of the language code.
createLangDirsfalseBooleanIf true, instead of translating index.html into index-en.html, etc, will translate to en/index.html, etc.
defaultLangundefinedStringIf defined and createLangDirs is true, translate index.html into index.html with the default language, etc.
renderEngineregexStringIf given sets rendering to be done by regex or Mustache (for more functionality)
delimiters'${{','}}$'String[]Can be used instead of langRegExp. Required to update mustache engine, langRegExp will not work with mustache
failOnMissingfalseBooleanIf true, any undefined tag found in an HTML file will throw an error. When false, missing tags are logged, but the process finishes.
fallbackundefinedStringIf given, will use the provided language as a fallback: For any other language, if a given tag's value is not provided, it will use the fallback language value.
inlineundefinedStringIf given, will use the provided language to create an output file of the same name as input. For example, passing inline: 'en-US' for index.html will result in index.html with English replacements.
langRegExp/\${{ ?(\w-.+) ?}}\$/gRegExpthe regular expression used for matching the language tags.
escapeQuotesfalseBooleanIf true, will replace " and ' with \\" and \\'.
tracefalseBooleanIf true, will place comments in output HTML to show where the translated strings came from
extendDefinationundefinedFunctionreturn an object to extend the language defination
0.16.0

4 years ago

0.15.0

5 years ago

0.14.0

6 years ago

0.13.0

6 years ago

0.12.0

6 years ago

0.11.0

6 years ago

0.10.3

6 years ago

0.10.2

6 years ago

0.10.1

6 years ago

0.10.0

6 years ago

0.9.0

7 years ago

0.8.2

7 years ago

0.8.1

7 years ago

0.8.0

7 years ago

0.7.1

7 years ago

0.7.0

7 years ago

0.6.5

7 years ago

0.6.4

8 years ago

0.6.3

8 years ago

0.6.2

8 years ago

0.6.1

8 years ago

0.6.0

8 years ago

0.5.0

8 years ago

0.4.4

8 years ago

0.4.3

8 years ago

0.4.2

8 years ago

0.4.1

8 years ago

0.4.0

8 years ago

0.3.4

8 years ago

0.3.3

8 years ago

0.3.2

8 years ago

0.3.1

8 years ago

0.3.0

8 years ago

0.2.3

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago