Permalink
Browse files

Partially convert to plain JS.

  • Loading branch information...
1 parent ab44169 commit 33557ea742ecf271e10797fbebe8c2a88be5a8eb @rubenv committed Sep 18, 2013
Showing with 106 additions and 60 deletions.
  1. +23 −0 .jshintrc
  2. +9 −4 Gruntfile.coffee
  3. +9 −12 dist/angular-gettext.js
  4. +1 −1 dist/angular-gettext.min.js
  5. +2 −1 package.json
  6. +0 −30 src/directive.coffee
  7. +44 −0 src/directive.js
  8. +0 −4 src/filter.coffee
  9. +5 −0 src/filter.js
  10. +0 −8 src/index.coffee
  11. +13 −0 src/index.js
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"
+ ]
+}
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']
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);
+ });
});
};
}
Oops, something went wrong.
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",
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)
- }
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);
+ });
+ });
+ };
+ }
+ };
+});
View
@@ -1,4 +0,0 @@
-angular.module('gettext').filter 'translate', (gettextCatalog, $interpolate, $parse) ->
- return (input) ->
- return gettextCatalog.getString(input)
-
View
@@ -0,0 +1,5 @@
+angular.module('gettext').filter('translate', function (gettextCatalog, $interpolate, $parse) {
+ return function (input) {
+ return gettextCatalog.getString(input);
+ };
+});
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
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 commented on 33557ea Nov 15, 2013

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);
}
}]);

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.