angular-gettext
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -0,1 +1,4 @@ | ||
if (typeof jQuery === 'undefined') { | ||
throw new Error('Angular-gettext depends on jQuery, be sure to include it!'); | ||
} | ||
angular.module('gettext', []); | ||
@@ -71,3 +74,3 @@ angular.module('gettext').factory('gettext', function () { | ||
transclude($scope, function (clone) { | ||
var input = clone.html(); | ||
var input = $.trim(clone.html()); | ||
clone.removeAttr('translate'); | ||
@@ -74,0 +77,0 @@ $element.replaceWith(clone); |
@@ -1,1 +0,1 @@ | ||
angular.module("gettext",[]),angular.module("gettext").factory("gettext",function(){return function(a){return a}}),angular.module("gettext").factory("gettextCatalog",["gettextPlurals",function(a){var b,c=function(a){return b.debug?"[MISSING]: "+a:a};return b={debug:!1,strings:{},currentLanguage:"en",setStrings:function(a,b){var c,d;this.strings[a]||(this.strings[a]={});for(c in b)d=b[c],this.strings[a][c]="string"==typeof d?[d]:d},getStringForm:function(a,b){var c=this.strings[this.currentLanguage]||{},d=c[a]||[];return d[b]},getString:function(a){return this.getStringForm(a,0)||c(a)},getPlural:function(b,d,e){var f=a(this.currentLanguage,b);return this.getStringForm(d,f)||c(1===b?d:e)}}}]),angular.module("gettext").directive("translate",["gettextCatalog","$interpolate","$parse",function(a,b,c){return{transclude:"element",priority:900,compile:function(d,e,f){return function(d,g){var h=function(a,b,c){if(!a)throw new Error("You should add a "+b+" attribute whenever you add a "+c+" attribute.")};h(!e.translatePlural||e.translateN,"translate-n","translate-plural"),h(!e.translateN||e.translatePlural,"translate-plural","translate-n");var i=c(e.translateN);f(d,function(c){var f=c.html();return c.removeAttr("translate"),g.replaceWith(c),d.$watch(function(){var g,h=c.html();g=e.translatePlural?a.getPlural(i(d),f,e.translatePlural):a.getString(f);var j=b(g)(d);return h!==j?c.html(j):void 0})})}}}}]),angular.module("gettext").filter("translate",["gettextCatalog","$interpolate","$parse",function(a){return function(b){return a.getString(b)}}]),angular.module("gettext").factory("gettextPlurals",function(){return function(a,b){switch(a){case"ay":case"bo":case"cgg":case"dz":case"fa":case"id":case"ja":case"jbo":case"ka":case"kk":case"km":case"ko":case"ky":case"lo":case"ms":case"my":case"sah":case"su":case"th":case"tt":case"ug":case"vi":case"wo":case"zh":return 0;case"is":return 1!=b%10||11==b%100?1:0;case"jv":return 0!=b?1:0;case"mk":return 1==b||1==b%10?0:1;case"ach":case"ak":case"am":case"arn":case"br":case"fil":case"fr":case"gun":case"ln":case"mfe":case"mg":case"mi":case"oc":case"pt_BR":case"tg":case"ti":case"tr":case"uz":case"wa":case"zh":return b>1?1:0;case"lv":return 1==b%10&&11!=b%100?0:0!=b?1:2;case"lt":return 1==b%10&&11!=b%100?0:b%10>=2&&(10>b%100||b%100>=20)?1:2;case"be":case"bs":case"hr":case"ru":case"sr":case"uk":return 1==b%10&&11!=b%100?0:b%10>=2&&4>=b%10&&(10>b%100||b%100>=20)?1:2;case"mnk":return 0==b?0:1==b?1:2;case"ro":return 1==b?0:0==b||b%100>0&&20>b%100?1:2;case"pl":return 1==b?0:b%10>=2&&4>=b%10&&(10>b%100||b%100>=20)?1:2;case"cs":case"sk":return 1==b?0:b>=2&&4>=b?1:2;case"sl":return 1==b%100?1:2==b%100?2:3==b%100||4==b%100?3:0;case"mt":return 1==b?0:0==b||b%100>1&&11>b%100?1:b%100>10&&20>b%100?2:3;case"gd":return 1==b||11==b?0:2==b||12==b?1:b>2&&20>b?2:3;case"cy":return 1==b?0:2==b?1:8!=b&&11!=b?2:3;case"kw":return 1==b?0:2==b?1:3==b?2:3;case"ga":return 1==b?0:2==b?1:7>b?2:11>b?3:4;case"ar":return 0==b?0:1==b?1:2==b?2:b%100>=3&&10>=b%100?3:b%100>=11?4:5;default:return 1!=b?1:0}}}); | ||
if("undefined"==typeof jQuery)throw new Error("Angular-gettext depends on jQuery, be sure to include it!");angular.module("gettext",[]),angular.module("gettext").factory("gettext",function(){return function(a){return a}}),angular.module("gettext").factory("gettextCatalog",["gettextPlurals",function(a){var b,c=function(a){return b.debug?"[MISSING]: "+a:a};return b={debug:!1,strings:{},currentLanguage:"en",setStrings:function(a,b){var c,d;this.strings[a]||(this.strings[a]={});for(c in b)d=b[c],this.strings[a][c]="string"==typeof d?[d]:d},getStringForm:function(a,b){var c=this.strings[this.currentLanguage]||{},d=c[a]||[];return d[b]},getString:function(a){return this.getStringForm(a,0)||c(a)},getPlural:function(b,d,e){var f=a(this.currentLanguage,b);return this.getStringForm(d,f)||c(1===b?d:e)}}}]),angular.module("gettext").directive("translate",["gettextCatalog","$interpolate","$parse",function(a,b,c){return{transclude:"element",priority:900,compile:function(d,e,f){return function(d,g){var h=function(a,b,c){if(!a)throw new Error("You should add a "+b+" attribute whenever you add a "+c+" attribute.")};h(!e.translatePlural||e.translateN,"translate-n","translate-plural"),h(!e.translateN||e.translatePlural,"translate-plural","translate-n");var i=c(e.translateN);f(d,function(c){var f=$.trim(c.html());return c.removeAttr("translate"),g.replaceWith(c),d.$watch(function(){var g,h=c.html();g=e.translatePlural?a.getPlural(i(d),f,e.translatePlural):a.getString(f);var j=b(g)(d);return h!==j?c.html(j):void 0})})}}}}]),angular.module("gettext").filter("translate",["gettextCatalog","$interpolate","$parse",function(a){return function(b){return a.getString(b)}}]),angular.module("gettext").factory("gettextPlurals",function(){return function(a,b){switch(a){case"ay":case"bo":case"cgg":case"dz":case"fa":case"id":case"ja":case"jbo":case"ka":case"kk":case"km":case"ko":case"ky":case"lo":case"ms":case"my":case"sah":case"su":case"th":case"tt":case"ug":case"vi":case"wo":case"zh":return 0;case"is":return 1!=b%10||11==b%100?1:0;case"jv":return 0!=b?1:0;case"mk":return 1==b||1==b%10?0:1;case"ach":case"ak":case"am":case"arn":case"br":case"fil":case"fr":case"gun":case"ln":case"mfe":case"mg":case"mi":case"oc":case"pt_BR":case"tg":case"ti":case"tr":case"uz":case"wa":case"zh":return b>1?1:0;case"lv":return 1==b%10&&11!=b%100?0:0!=b?1:2;case"lt":return 1==b%10&&11!=b%100?0:b%10>=2&&(10>b%100||b%100>=20)?1:2;case"be":case"bs":case"hr":case"ru":case"sr":case"uk":return 1==b%10&&11!=b%100?0:b%10>=2&&4>=b%10&&(10>b%100||b%100>=20)?1:2;case"mnk":return 0==b?0:1==b?1:2;case"ro":return 1==b?0:0==b||b%100>0&&20>b%100?1:2;case"pl":return 1==b?0:b%10>=2&&4>=b%10&&(10>b%100||b%100>=20)?1:2;case"cs":case"sk":return 1==b?0:b>=2&&4>=b?1:2;case"sl":return 1==b%100?1:2==b%100?2:3==b%100||4==b%100?3:0;case"mt":return 1==b?0:0==b||b%100>1&&11>b%100?1:b%100>10&&20>b%100?2:3;case"gd":return 1==b||11==b?0:2==b||12==b?1:b>2&&20>b?2:3;case"cy":return 1==b?0:2==b?1:8!=b&&11!=b?2:3;case"kw":return 1==b?0:2==b?1:3==b?2:3;case"ga":return 1==b?0:2==b?1:7>b?2:11>b?3:4;case"ar":return 0==b?0:1==b?1:2==b?2:b%100>=3&&10>=b%100?3:b%100>=11?4:5;default:return 1!=b?1:0}}}); |
{ | ||
"name": "angular-gettext", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Gettext support for Angular.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
136
README.md
@@ -7,137 +7,3 @@ # angular-gettext - gettext utilities for angular.js | ||
## Getting started | ||
Add angular-gettext to your project: | ||
1. Grab the files, either by copying the files from the `dist` folder or (preferably) through bower: `bower install --save angular-gettext`. | ||
2. Include the angular-gettext source files in your app: | ||
```html | ||
<script src="bower_components/angular-gettext/dist/angular-gettext.min.js"></script> | ||
``` | ||
3. Add a dependency to angular-gettext in your Angular app: | ||
```js | ||
angular.module('myApp', ['gettext']); | ||
``` | ||
You can now start using the `translate` directive to mark strings as translatable. | ||
Next steps: | ||
* Annotate strings with the `translate` directive. | ||
* Extract strings from the source code. | ||
* Translate strings | ||
* Integrate translated strings back into the application. | ||
* Set the correct language for translations. | ||
## Using the `translate` directive | ||
Strings are marked as translatable using the `translate` directive. Here's a simple example: | ||
```html | ||
<h1 translate>Hello!</h1> | ||
``` | ||
This div will automatically be translated using the translated strings (which we'll define later on). For instance, in Dutch, it might read `Hallo!`. | ||
### Plurals | ||
Plural strings can be annotated using two extra attributes: `translate-n` and `translate-plural`: | ||
```html | ||
<div translate translate-n="count" translate-plural="{{count}} boats">One boat</div> | ||
``` | ||
The general format is: | ||
```html | ||
<div translate translate-n="COUNTEXPR" translate-plural="PLURALSTR">SINGULARSTR</div> | ||
``` | ||
Depending on the value of `COUNTEXPR`, either the singular string or the plural string will be selected. | ||
### Interpolation | ||
Full interpolation support is available in translated strings, so the following will work as expected: | ||
```html | ||
<div translate>Hello {{name}}!</div> | ||
``` | ||
## Using the `translate` filter | ||
There's also a filter available. This filter does not support plural strings: | ||
```html | ||
<input type="text" placeholder="{{'Username'|translate}}" /> | ||
``` | ||
## Extracting strings | ||
Use [`grunt-angular-gettext`](https://github.com/rubenv/grunt-angular-gettext) to extract strings from your HTML templates into a `.pot` catalog. | ||
### Marking strings in JavaScript code as translatable. | ||
If you have text that should be translated in your JavaScript code, wrap it with a call to a function named `gettext`. This module provides an injectable function to do so: | ||
```js | ||
angular.module("myApp").controller("helloController", function (gettext) { | ||
var myString = gettext("Hello"); | ||
}); | ||
``` | ||
The `Hello` string will be added to your `.pot` file using the code above. | ||
## Translate strings | ||
Use a tool like [poedit](http://www.poedit.net/), [Pootle](http://pootle.translatehouse.org/) or [Transifex](https://www.transifex.com/) to translate the strings. | ||
## Setting translated strings | ||
You can set translated strings by injecting the `gettextCatalog` and using the `setStrings` method. | ||
As an example, you may have the following code in your application: | ||
```js | ||
angular.module('myApp').run(function (gettextCatalog) { | ||
// Load the strings automatically during initialization. | ||
gettextCatalog.setStrings('nl', { | ||
'Hello': 'Hallo', | ||
'One boat': ['Een boot', '{{count}} boats'] | ||
}); | ||
}); | ||
``` | ||
Converting translated `.po` files into angular-compatible JavaScript files can be done automatically using the [`grunt-angular-gettext`](https://github.com/rubenv/grunt-angular-gettext) module. | ||
## Setting the language | ||
Set the language by setting a language code on the catalog: | ||
```js | ||
angular.module('myApp').run(function (gettextCatalog) { | ||
gettextCatalog.currentLanguage = 'nl'; | ||
}); | ||
``` | ||
You can do this anywhere in your application, the use of a `run` block above is just an example. | ||
## Highlighting untranslated strings | ||
You can enable a debugging mode to clearly indicate untranslated strings: | ||
```js | ||
angular.module('myApp').run(function (gettextCatalog) { | ||
gettextCatalog.debug = true; | ||
}); | ||
``` | ||
This will prepend `[MISSING]:` to each untranslated string. | ||
## Contributing | ||
All code lives in the `src` folder and is written in CoffeeScript. Try to stick to the style conventions used in existing code. | ||
Tests can be run using `grunt test`. A convenience command to automatically run the tests is also available: `grunt watch`. Please add test cases when adding new functionality: this will prove that it works and ensure that it will keep working in the future. | ||
Check the website for usage instructions: [http://angular-gettext.rocketeer.be/](http://angular-gettext.rocketeer.be/). | ||
@@ -144,0 +10,0 @@ ## License |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
216
13546
32