Skip to content

Commit

Permalink
Revision to 2.1.1
Browse files Browse the repository at this point in the history
* Added option to define the variable that holds the locale to use. The default is still `window.locale` but you can now define it by setting the option `localeVar`.
* Added tests to validate the locale code is working correctly
* Spelling corrections and clean-up of documentation.
  • Loading branch information
intervalia committed Jan 5, 2016
1 parent 62da2e5 commit c2c6cf8
Show file tree
Hide file tree
Showing 24 changed files with 303 additions and 31 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,16 @@ Here is the list of options and their description and usage:
| **defaultLocale** | `defaultLocale:"en"` | Set the locale that your project will use as the default locale. If you do not provide the `defaultLocale` option then the default locale is set to `"en"`. `defaultLocale` is also the locale that is used if the user attempts to request a non-supported locale. |
| **exposeLang** | `exposeLang:true/false` | If set to `true` then the language strings are also placed into a global object for access outside of the IIFE. The language strings will be added to `[globalObj].[assemblyName].lang` where `assemblyName` is the name of the assembly that is being created.<br/><br/>**See `globalObj`.** |
| **externalLibName** | `externalLibName:"filename"` | Name for the external lib file. The default is `assembly-lib.js` and `assembly-lib-min.js`.<br/><br/>**See `useExternalLib`.** |
| **iifeParams** | `iifeParams:paramsObject` | This is an optional object that contains the list of parameters used by the IIFE and the list of parameters passed into the IIFE. The default values are "window, document".<br/><br/>**See *Option: iifeParams* below.** |
| **globalObj** | `globalObj:"objectToUse"` | This is an optional string that defines the global object that is used to expose the language string into the global scope. The default value is `"window.components"`.<br/><br/>If you are building servers-side components to run in node.js, then you would set this to `"global.components"` or something similar. _But be aware that this could become a problem if you are working with a server cluster._<br/><br/>Currently this is only used if you set the option `exposeLang` to `true`. |
| **iifeParams** | `iifeParams:paramsObject` | This is an optional object that contains the list of parameters used by the IIFE and the list of parameters passed into the IIFE. The default values are "window, document".<br/><br/> **See *Option: iifeParams* below.** |
| **localeVar** | `localeVar:"window.locale"` | The default value for this options is `window.locale`. If your application uses some other variable to set the locale then you can supply it here like `window.myObj.locale`. If the defined variable name is undefined or does not exist then the locale is set to the value of the option `defaultLocale`.<br><br> **See `defaultLocale` above.** |
| **minTemplateWS** | `minTemplateWS:true/false` | This controls how white space is processed in the templates. If set to `true` then each set of white space is reduced to a single space to reduce the overall size of the templates while maintaining separation of tags. If set to `false` then all white space is preserved with the exception the white space at the beginning and end of the template which is trimmed and removed. |
| **supportTransKeys** | `supportTransKeys:true/false` | If set to `true` this creates a set to translation test values.<br/><br/>**See *Option: supportTransKeys* below.** |
| **tagMissingStrings** | `tagMissingStrings:true/false` | If set to `true` then any string that was in the locale file for the default locale that is not found in one of the other locale files is marked so the user can see the lack of translation easily. If set to `false` then the missing translations are set to the key for that string. |
| **useExternalLib** | `useExternalLib:true/false` | If set to `true` then a single file `assambly-lib.js` is created with the common code used for each assembly. If it is set to `false` then each assembly contains copies of the common code needed for the assembly to work. If you choose to use the external libraries then you must include that file before including your own. |
| **useOldDest** | `useOldDest:true/false` | *New in 2.0.0* - If set to `true` then the output directory structure is used (Before ver. 2.0.0) If set to `false` then the output files are stored one level higher that the pre 2.0.0 locations. __This is deprecated and provided for backward compatibility only. `useOldDest` will be removed in version 3.x__ |
| **useOldDest** | `useOldDest:true/false` | *New in 2.0.0* - If set to `true` then the output directory structure is used. The output files are placed in the same folder as the `assembly.json` file. (Same as before ver. 2.0.0) If set to `false` then the output files are stored one level higher than the pre 2.0.0 locations, the parent folder of where the `assembly.json` file. |
| **useStrict** | `useStrict:true/false` | If set to `true` then `"use strict";` is added just inside the IIFE.<br/><br/>**See *Option: useStrict* below.** |
| **watch** | `watch:true/false` | If set to `true` then the dependancies of the assembly file are watched. If any of them change, are added or removed then the `assembly.json` file is `touched` with the current date/time. This allows gulp.watch to monitor only the `assembly.json` files and yet recompile when anything related to the assembly has changed. **It is the developers responsability to use gulp.watch for this to work.** |
| **watch** | `watch:true/false` | If set to `true` then the dependancies of the assembly file are watched. If any of them change, are added or removed then the `assembly.json` file is `touched` with the current date/time. This allows gulp.watch to monitor only the `assembly.json` files and yet recompile when anything related to the assembly has changed. **It is the developers responsibility to use gulp.watch for this to work.** |


_**Option names are case sensitive. `defaultLocale` is correct but `DefaultLocale` is not.**_
Expand Down Expand Up @@ -240,12 +241,14 @@ Note: _Additional properties can be placed in the `assembly.json` file to be use
>All of the code from the files listed in the `files` array is wrapped inside an IIFE. This IIFE is to prevent name collisions between this component and all other JavaScript that you will load. So if you want anything accessible outside of the IIFE then you must provide the code to make it accessible. The simplest, but not best solution, is to create global variable. In the browser this is done by attaching parameters to the `window` object. For example:
```js
var localVar = "This will be a provate varaible, protected inside an IIFE";
var localVar = "This will be a private variable, protected inside an IIFE";

window.globalVar = localVar; // This is now accessible throughout the app/web page
```
>_Depending on your environment you may expose properties, classes and functions through things like `module.exports`, `define` or an existing global object or function._
> _**I should warn about making everything public and a member of the `window` object. This can lead to complicated code and hinder reusability. It is better to use some form of a module loading system.**_
>_Depending on your environment you may expose properties, classes and functions through things like `module.exports`, `define` or an existing global object or function. But I leave that to you to research and find the best mechanism for you and your team._
---
Expand Down Expand Up @@ -397,7 +400,7 @@ _The value for `window.locale` must be set before loading any component output f
#### Accessing locale strings in your JavaScript
Within a component output file, each assembly and sub-assembly would contain it's own locale strings. These are accesed through the property `lang`. In the examples JSON files above you would acces the strings as `lang.BUTTON_OK`, `lang.BUTTON_CANCEL`, `lang.BUTTON_CLOSE` and `lang.NO_CHANGES`.
Within a component output file, each assembly and sub-assembly would contain it's own locale strings. These are accessed through the property `lang`. In the examples JSON files above you would access the strings as `lang.BUTTON_OK`, `lang.BUTTON_CANCEL`, `lang.BUTTON_CLOSE` and `lang.NO_CHANGES`.
>___TODO: Provide more information here___
Expand Down
5 changes: 5 additions & 0 deletions UPDATE_HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
Update History
==============
### 2.1.1 - Jan 4, 2016
* Added option to define the variable that holds the locale to use. The default is still `window.locale` but you can now define it by setting the option `localeVar`.
* Added tests to validate the locale code is working correctly
* Spelling corrections and clean-up of documentation.

### 2.1.0 - Oct 10, 2015
* Added watch ability that will `touch` an assembly if any of its dependancies are changed, added or removed.
* Updated documentation
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-component-assembler",
"version": "2.1.0",
"version": "2.1.1",
"author": "Michael G Collins <[email protected]>",
"license": "MIT",
"description": "gulp-component-assembler is a gulp plug-in that assembles JavaScript components. The components are a combination of JavaScript files, HTML Templates and Localization strings.",
Expand Down
22 changes: 12 additions & 10 deletions src/locales.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ function processLocales(baseLocalePath, localeFileName, assemblyName, options) {
var translations, strings, localeList = [];
var supportTransKeys = !!options.supportTransKeys;

options.localeVar = options.localeVar || "window.locale";

if (supportTransKeys) {
localeList =["zz","ke"];
}
Expand All @@ -26,7 +28,7 @@ function processLocales(baseLocalePath, localeFileName, assemblyName, options) {

translations.key.forEach(function(key, keyIndex) {
if (options.tagMissingStrings) {
strings.push(translations[locale][key] || "-*"+(translations[options.locale][key] || "Not Found")+" *-");
strings.push(translations[locale][key] || "-*"+(translations[options.locale][key] || "Not Found")+"*-");
}
else {
strings.push(translations[locale][key] || translations[options.locale][key] || "");
Expand Down Expand Up @@ -75,22 +77,22 @@ function processLocales(baseLocalePath, localeFileName, assemblyName, options) {

// Return the correct lang object
contents += " return lang;\n"+
"}\n\n"+
// set the lang variable
"var lang = getLang(window.locale || '"+options.locale+"');\n";
"}";
}
else {
contents += "function getLang(locale) {\n"+
" return __getLangObj(locale, langKeys, validLocales, langs);\n"+
"}\n\n"+
"var lang = getLang(window.locale || '"+options.locale+"');\n";
"}";
}
// set the lang variable
contents += "\n\nvar lang = getLang("+options.localeVar+" || '"+options.locale+"');\n";

if (options.exposeLang) {
var globalObj = options.globalObj || "components";
contents += "window."+globalObj+" = window."+globalObj+" || {};\n";
contents += "window."+globalObj+"."+assemblyName+" = window."+globalObj+"."+assemblyName+" || {};\n";
contents += "window."+globalObj+"."+assemblyName+".lang = lang;\n";
var globalObj = "window."+(options.globalObj || "components");
var globalAssembly = globalObj+"."+assemblyName;
contents += globalObj + " = " + globalObj + " || {};\n";
contents += globalAssembly + " = " + globalAssembly + " || {};\n";
contents += globalAssembly + ".lang = lang;\n";
}
}

Expand Down
26 changes: 13 additions & 13 deletions test/data/sub1.tagMissingStrings.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@
var langKeys = ["TEST","YES","NO","WHY"];
var langs = {
// Included locale file: strings_de.json
"de": ["Dies ist ein Test.","Ja","Nicht","-*Why? *-"],
"de": ["Dies ist ein Test.","Ja","Nicht","-*Why?*-"],
// Included locale file: strings_en.json
"en": ["This is a test.","Yes","No","Why?"],
// Included locale file: strings_es.json
"es": ["Esta es una prueba.","Sí","No","-*Why? *-"],
"es": ["Esta es una prueba.","Sí","No","-*Why?*-"],
// Included locale file: strings_fr.json
"fr": ["C'est un test.","Oui","Non","-*Why? *-"],
"fr": ["C'est un test.","Oui","Non","-*Why?*-"],
// Included locale file: strings_it.json
"it": ["Questo è un test.","Sì","No","-*Why? *-"],
"it": ["Questo è un test.","Sì","No","-*Why?*-"],
// Included locale file: strings_ja.json
"ja": ["これはテストです。","はい","いいえ","-*Why? *-"],
"ja": ["これはテストです。","はい","いいえ","-*Why?*-"],
// Included locale file: strings_ko.json
"ko": ["테스트입니다.","예","없음","-*Why? *-"],
"ko": ["테스트입니다.","예","없음","-*Why?*-"],
// Included locale file: strings_mn.json
"mn": ["Энэ нь тест юм.","Тийм ээ","ямар ч","-*Why? *-"],
"mn": ["Энэ нь тест юм.","Тийм ээ","ямар ч","-*Why?*-"],
// Included locale file: strings_nl.json
"nl": ["Dit is een test.","Ja","Geen","-*Why? *-"],
"nl": ["Dit is een test.","Ja","Geen","-*Why?*-"],
// Included locale file: strings_no.json
"no": ["Dette er en test.","Ja","Nei","-*Why? *-"],
"no": ["Dette er en test.","Ja","Nei","-*Why?*-"],
// Included locale file: strings_pt.json
"pt": ["Este é um teste.","Sim","Não","-*Why? *-"],
"pt": ["Este é um teste.","Sim","Não","-*Why?*-"],
// Included locale file: strings_ro.json
"ro": ["Acesta este un test.","Da","Nu","-*Why? *-"],
"ro": ["Acesta este un test.","Da","Nu","-*Why?*-"],
// Included locale file: strings_ru.json
"ru": ["Это тест.","Да","Нет","-*Why? *-"],
"ru": ["Это тест.","Да","Нет","-*Why?*-"],
// Included locale file: strings_zh.json
"zh": ["这是一个考验。","是的","无","-*Why? *-"],
"zh": ["这是一个考验。","是的","无","-*Why?*-"],
};
var validLocales = ["de","en","es","fr","it","ja","ko","mn","nl","no","pt","ro","ru","zh"];

Expand Down
2 changes: 1 addition & 1 deletion test/specs/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
describe('Test gulp-component-assembler', function () {
require('./assemblies.test');
require('./globArray.test');
//require('./locales.test');
require('./locales.test');
require('./plugin.test');
//require('./scripts.test');
//require('./templates.test');
Expand Down
Loading

0 comments on commit c2c6cf8

Please sign in to comment.