Skip to content

Commit

Permalink
Partially convert to plain JS.
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenv committed Sep 18, 2013
1 parent ab44169 commit 33557ea
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 60 deletions.
23 changes: 23 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -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 changes: 9 additions & 4 deletions Gruntfile.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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:
Expand All @@ -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/*']
Expand Down Expand Up @@ -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']
Expand Down
21 changes: 9 additions & 12 deletions dist/angular-gettext.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
});
});
};
}
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-gettext.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
30 changes: 0 additions & 30 deletions src/directive.coffee

This file was deleted.

44 changes: 44 additions & 0 deletions src/directive.js
Original file line number Diff line number Diff line change
@@ -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 changes: 0 additions & 4 deletions src/filter.coffee

This file was deleted.

5 changes: 5 additions & 0 deletions src/filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
angular.module('gettext').filter('translate', function (gettextCatalog, $interpolate, $parse) {
return function (input) {
return gettextCatalog.getString(input);
};
});
8 changes: 0 additions & 8 deletions src/index.coffee

This file was deleted.

13 changes: 13 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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
Copy link

@dawk dawk commented on 33557ea Nov 15, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@rubenv
Copy link
Owner Author

@rubenv rubenv commented on 33557ea Nov 20, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.