npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

grunt-i18n-templates

v0.1.4

Published

i18n for front-end templates.

Downloads

11

Readme

grunt-i18nTemplates

TASK STEP 1

Join all your templates files in a single json file so it is easy to access all the templates from javascript.

 // index.html
 "<html><body><h1>[[title: Hello World]]</h1> .....</body></html>"
 
 // form.html
 "<for><inpup> ...... </form>"

Result templates.json

 // i.e. : joined  index.html and form.html
 // result file: 
 {
   "index":"<html><body><h1>[[title: Hello World]]</h1> .....</body></html>",
   "form":"<for><inpup> ...... </form>"
 }

TASK STEP 2

Parses template files looking for locale-Definitions [[key: text]], and Generates localization files that contais the string to translate.

 index.html locale definition => [[title: Hello World]]
 
 form.html locale definition => none

Parsing the two example files generate the next localization files when options.locales is set to ["EN","ES"]

 // EN_locales.json  
 {
   "index:title":"Hello World"
 }
 
 // ES_locales.json
 {
   "index:title":"Hola Mundo" //this is the translation
 }

TASK STEP 3

Reads the values in the localization files and use them to generate the translated template files. i.e EN_templates.json and ES_templates.json.

 // EN_templates.json
 {
   "index":"<html><body><h1>Hello World</h1> .....</body></html>",
   "form":"<for><inpup> ...... </form>"
 }
 // ES_templates.json
 {
   "index":"<html><body><h1>Hola Mundo</h1> .....</body></html>",
   "form":"<for><inpup> ...... </form>"
 }

features

  • Join all templates in a single templates.json file, so it is more easy to access from javascript.
  • Automatically generate localization files.
  • Compatible with multiple template engines. Translation is done statically so the result can be used by any template engine.
  • Never override the sentences edited in the locale files. it is posiible to run this task at any time, the values in the localization files will never be overwritten, but new templates or new locale-Definitions will be added.

Generated templates

All templates are joined in the file templates.json. This file is never translated and always reflect the last estate of the templates, templates.json can be used in system where no translation is equired and you only want the join templates functionality. All the others Xlang_templates.json uses the values stored in the localization files.

If the value of one locale-Definition is changed in one file, i.e from [[title: Hello]] to [[title: Bye]] this change is not reflected in the translated templates Xlang_templates.json becouse this are using the values previously stored in the localization files. this change will be reflected only in templates.json.

Using the generated code.

i.e: using Jquery and mustache

//this is the english template file generated by 
//this task and contains all you templates.
var EN_templates = "./templates/EN_templates.json";
jQuery.getJSON(templateFile, function(data){
      /* data contains all the templates */
      var templates = data;
      Mustache.render(EN_templates.index,{});
});  

GRUNT TASK CONFIGURATION

This plugin requires Grunt ~0.4.5

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-i18nTemplates --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-i18n-templates');

The "i18nTemplates" task

Overview

In your project's Gruntfile, add a section named i18nTemplates to the data object passed into grunt.initConfig().

grunt.initConfig({
  i18nTemplates: {
  	options: {
    	locales: ["en","es","de"],
        templatesDest: "./public/templates", //this is where the translated templates will be generated
        localesDest: "./src/locales"  //this is where translation files will be stored	
   	},
  	your_target: {
       src: ['./src/templates/**/*.html']
    },
  },
});

Options

options.templatesDest

Type: String Default value: none, this value is required and not set by default.

A path to the templates folder.

options.localesDest

Type: String Default value: none, this value is required and not set by default.

A path to the locales folder.

options.locales

Type: Array Default value: none

An Array defining all the languages. i.e: ["en","es","de"]

Usage Examples

This example will generate 4 locale files in the ./src/locales folder (one for each language), and will generate 5 templates files in the ./public/templates, (one for each language + the one without translate).

grunt.initConfig({
  i18nTemplates: {
  	options: {
    	locales: ["en","es","de"],
        templatesDest: "./public/templates", 
        localesDest: "./src/locales" 	
   	},
  	your_target: {
       src: ['./src/templates/**/*.html']
    },
  },
});

Release History

0.1.4 fix bug that overrides the values stored in the locales files.

0.1.3 Improve Log Info and update Readme.

0.1.2 fix bugs.

0.1.0 first working version.