Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Partially convert to plain JS.

commit 33557ea742ecf271e10797fbebe8c2a88be5a8eb 1 parent ab44169
Ruben Vermeersch authored
23 .jshintrc
View
@@ -0,0 +1,23 @@
+{
+ "curly": true,
+ "eqeqeq": true,
+ "immed": true,
+ "latedef": true,
+ "newcap": true,
+ "noarg": true,
+ "sub": true,
+ "undef": true,
+ "boss": true,
+ "eqnull": true,
+ "node": true,
+ "white": true,
+ "predef": [
+ "angular",
+ "describe",
+ "it",
+ "before",
+ "beforeEach",
+ "after",
+ "afterEach"
+ ]
+}
13 Gruntfile.coffee
View
@@ -3,6 +3,7 @@ module.exports = (grunt) ->
@loadNpmTasks('grunt-contrib-coffee')
@loadNpmTasks('grunt-contrib-concat')
@loadNpmTasks('grunt-contrib-connect')
+ @loadNpmTasks('grunt-contrib-jshint')
@loadNpmTasks('grunt-contrib-uglify')
@loadNpmTasks('grunt-contrib-watch')
@loadNpmTasks('grunt-karma')
@@ -15,13 +16,17 @@ module.exports = (grunt) ->
options:
bare: true
files:
- 'dist/angular-gettext.js': ['src/index.coffee', 'src/*.coffee']
+ 'dist/angular-gettext.js': ['src/*.coffee']
+ jshint:
+ all: [ 'src/*.js', '!src/plural.js' ]
+ options:
+ jshintrc: '.jshintrc'
concat:
dist:
files:
- 'dist/angular-gettext.js': ['dist/angular-gettext.js', 'src/plural.js']
+ 'dist/angular-gettext.js': ['src/index.js', 'dist/angular-gettext.js', 'src/*.js']
uglify:
dist:
@@ -35,7 +40,7 @@ module.exports = (grunt) ->
options:
livereload: true
all:
- files: ['src/**.coffee', 'test/*/*']
+ files: ['src/**.coffee', 'src/**.js', 'test/*/*']
tasks: ['build', 'karma:unit:run', 'karma:e2e:run']
unit:
files: ['src/**.coffee', 'test/unit/*']
@@ -84,7 +89,7 @@ module.exports = (grunt) ->
outputFile: 'e2e-results.xml'
@registerTask 'default', ['test']
- @registerTask 'build', ['clean', 'coffee', 'concat', 'ngmin', 'uglify']
+ @registerTask 'build', ['clean', 'coffee', 'jshint', 'concat', 'ngmin', 'uglify']
@registerTask 'package', ['build', 'release']
@registerTask 'test', ['build', 'connect:e2e', 'karma:unit', 'karma:e2e', 'watch:all']
@registerTask 'test_unit', ['build', 'karma:unit', 'watch:unit']
21 dist/angular-gettext.js
View
@@ -63,8 +63,7 @@ angular.module('gettext').directive('translate', [
priority: 900,
compile: function (element, attrs, transclude) {
return function ($scope, $element) {
- var countFn, err;
- err = function (missing, found) {
+ var err = function (missing, found) {
throw new Error('You should add a ' + missing + ' attribute whenever you add a ' + found + ' attribute.');
};
if (attrs.translatePlural && !attrs.translateN) {
@@ -73,27 +72,25 @@ angular.module('gettext').directive('translate', [
if (attrs.translateN && !attrs.translatePlural) {
err('translate-plural', 'translate-n');
}
- countFn = $parse(attrs.translateN);
- return transclude($scope, function (clone) {
- var input, process;
- input = clone.html();
+ var countFn = $parse(attrs.translateN);
+ transclude($scope, function (clone) {
+ var input = clone.html();
clone.removeAttr('translate');
$element.replaceWith(clone);
- process = function () {
- var interpolated, prev, translated;
- prev = clone.html();
+ return $scope.$watch(function () {
+ var prev = clone.html();
+ var translated;
if (attrs.translatePlural) {
translated = gettextCatalog.getPlural(countFn($scope), input, attrs.translatePlural);
} else {
translated = gettextCatalog.getString(input);
}
- interpolated = $interpolate(translated)($scope);
+ var interpolated = $interpolate(translated)($scope);
if (prev === interpolated) {
return;
}
return clone.html(interpolated);
- };
- return $scope.$watch(process);
+ });
});
};
}
2  dist/angular-gettext.min.js
View
@@ -1 +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;return b=function(){function b(){this.debug=!1,this.strings={},this.currentLanguage="en"}var c;return c=function(a,b){return a?"[MISSING]: "+b:b},b.prototype.setStrings=function(a,b){var c,d,e;this.strings[a]||(this.strings[a]={}),e=[];for(c in b)d=b[c],"string"==typeof d?e.push(this.strings[a][c]=[d]):e.push(this.strings[a][c]=d);return e},b.prototype.getString=function(a){var b,d;return(null!=(b=this.strings[this.currentLanguage])?null!=(d=b[a])?d[0]:void 0:void 0)||c(this.debug,a)},b.prototype.getPlural=function(b,d,e){var f,g,h;return f=a(this.currentLanguage,b),g=(null!=(h=this.strings[this.currentLanguage])?h[d]:void 0)||[],g[f]||c(this.debug,1===b?d:e)},b}(),new b}]),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,i;return i=function(a,b){throw new Error("You should add a "+a+" attribute whenever you add a "+b+" attribute.")},e.translatePlural&&!e.translateN&&i("translate-n","translate-plural"),e.translateN&&!e.translatePlural&&i("translate-plural","translate-n"),h=c(e.translateN),f(d,function(c){var f,i;return f=c.html(),c.removeAttr("translate"),g.replaceWith(c),i=function(){var g,i,j;return i=c.html(),j=e.translatePlural?a.getPlural(h(d),f,e.translatePlural):a.getString(f),g=b(j)(d),i!==g?c.html(g):void 0},d.$watch(i)})}}}}]),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"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}}});
+angular.module("gettext",[]),angular.module("gettext").factory("gettext",function(){return function(a){return a}}),angular.module("gettext").factory("gettextCatalog",["gettextPlurals",function(a){var b;return b=function(){function b(){this.debug=!1,this.strings={},this.currentLanguage="en"}var c;return c=function(a,b){return a?"[MISSING]: "+b:b},b.prototype.setStrings=function(a,b){var c,d,e;this.strings[a]||(this.strings[a]={}),e=[];for(c in b)d=b[c],"string"==typeof d?e.push(this.strings[a][c]=[d]):e.push(this.strings[a][c]=d);return e},b.prototype.getString=function(a){var b,d;return(null!=(b=this.strings[this.currentLanguage])?null!=(d=b[a])?d[0]:void 0:void 0)||c(this.debug,a)},b.prototype.getPlural=function(b,d,e){var f,g,h;return f=a(this.currentLanguage,b),g=(null!=(h=this.strings[this.currentLanguage])?h[d]:void 0)||[],g[f]||c(this.debug,1===b?d:e)},b}(),new b}]),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){throw new Error("You should add a "+a+" attribute whenever you add a "+b+" attribute.")};e.translatePlural&&!e.translateN&&h("translate-n","translate-plural"),e.translateN&&!e.translatePlural&&h("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"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}}});
3  package.json
View
@@ -34,7 +34,8 @@
"karma-firefox-launcher": "~0.1.0",
"karma-junit-reporter": "~0.1.0",
"karma-mocha": "~0.1.0",
- "karma-ng-scenario": "~0.1.0"
+ "karma-ng-scenario": "~0.1.0",
+ "grunt-contrib-jshint": "~0.6.4"
},
"repository": {
"type": "git",
30 src/directive.coffee
View
@@ -1,30 +0,0 @@
-angular.module('gettext').directive 'translate', (gettextCatalog, $interpolate, $parse) ->
- return {
- transclude: 'element'
- priority: 900
- compile: (element, attrs, transclude) ->
- return ($scope, $element) ->
- err = (missing, found) ->
- throw new Error("You should add a #{missing} attribute whenever you add a #{found} attribute.")
- err('translate-n', 'translate-plural') if attrs.translatePlural && !attrs.translateN
- err('translate-plural', 'translate-n') if attrs.translateN && !attrs.translatePlural
-
- countFn = $parse(attrs.translateN)
-
- transclude $scope, (clone) ->
- input = clone.html()
- clone.removeAttr('translate')
- $element.replaceWith(clone)
-
- process = () ->
- prev = clone.html()
- if attrs.translatePlural
- translated = gettextCatalog.getPlural(countFn($scope), input, attrs.translatePlural)
- else
- translated = gettextCatalog.getString(input)
- interpolated = $interpolate(translated)($scope)
- return if prev == interpolated # Skip DOM change
- clone.html(interpolated)
-
- $scope.$watch(process)
- }
44 src/directive.js
View
@@ -0,0 +1,44 @@
+angular.module('gettext').directive('translate', function (gettextCatalog, $interpolate, $parse) {
+ return {
+ transclude: 'element',
+ priority: 900,
+ compile: function (element, attrs, transclude) {
+ return function ($scope, $element) {
+ // Validate attributes
+ var err = function (missing, found) {
+ throw new Error("You should add a " + missing + " attribute whenever you add a " + found + " attribute.");
+ };
+ if (attrs.translatePlural && !attrs.translateN) { err('translate-n', 'translate-plural'); }
+ if (attrs.translateN && !attrs.translatePlural) { err('translate-plural', 'translate-n'); }
+
+ var countFn = $parse(attrs.translateN);
+
+ transclude($scope, function (clone) {
+ var input = clone.html();
+ clone.removeAttr('translate');
+ $element.replaceWith(clone);
+
+ return $scope.$watch(function () {
+ var prev = clone.html();
+
+ // Fetch correct translated string.
+ var translated;
+ if (attrs.translatePlural) {
+ translated = gettextCatalog.getPlural(countFn($scope), input, attrs.translatePlural);
+ } else {
+ translated = gettextCatalog.getString(input);
+ }
+
+ // Interpolate with scope.
+ var interpolated = $interpolate(translated)($scope);
+ if (prev === interpolated) {
+ return; // Skip DOM change.
+ }
+
+ return clone.html(interpolated);
+ });
+ });
+ };
+ }
+ };
+});
4 src/filter.coffee
View
@@ -1,4 +0,0 @@
-angular.module('gettext').filter 'translate', (gettextCatalog, $interpolate, $parse) ->
- return (input) ->
- return gettextCatalog.getString(input)
-
5 src/filter.js
View
@@ -0,0 +1,5 @@
+angular.module('gettext').filter('translate', function (gettextCatalog, $interpolate, $parse) {
+ return function (input) {
+ return gettextCatalog.getString(input);
+ };
+});
8 src/index.coffee
View
@@ -1,8 +0,0 @@
-angular.module('gettext', [])
-
-angular.module('gettext').factory 'gettext', () ->
- # Does nothing, simply returns the input string.
- #
- # This function serves as a marker for `grunt-angular-gettext` to know that
- # this string should be extracted for translations.
- return (str) -> str
13 src/index.js
View
@@ -0,0 +1,13 @@
+angular.module('gettext', []);
+
+angular.module('gettext').factory('gettext', function () {
+ /*
+ * Does nothing, simply returns the input string.
+ *
+ * This function serves as a marker for `grunt-angular-gettext` to know that
+ * this string should be extracted for translations.
+ */
+ return function (str) {
+ return str;
+ };
+});

2 comments on commit 33557ea

dawk

This function returns an input string rather then translated version of it. According to http://angular-gettext.rocketeer.be/dev-guide/annotate-js/ it marks text for translation. It would make sense though if it returned translated version.

For my own needs, I made the following fix to that function:

angular.module("gettext").factory("gettext",[
'gettextCatalog',
'$interpolate',
'$parse',
function(gettextCatalog, $interpolate, $parse){
return function(str) {
return gettextCatalog.getString(a);
}
}]);

Ruben Vermeersch
Owner

This is intentional. You don't want your strings translated at that point, otherwise they won't get retranslated whenever the language changes.

Please sign in to comment.
Something went wrong with that request. Please try again.