Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions dist/angular-gettext.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http",
}
};

// Trim polyfill for old browsers (instead of jQuery)
// Based on AngularJS-v1.2.2 (angular.js#620)
var trim = (function () {
if (!String.prototype.trim) {
return function (value) {
return (typeof value === 'string') ? value.replace(/^\s*/, '').replace(/\s*$/, '') : value;
};
}
return function (value) {
return (typeof value === 'string') ? value.trim() : value;
};
})();

function broadcastUpdated() {
$rootScope.$broadcast('gettextLanguageChanged');
}
Expand All @@ -47,11 +60,17 @@ angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http",
showTranslatedMarkers: false,
translatedMarkerPrefix: '[',
translatedMarkerSuffix: ']',
idTransform: function (s) { return trim(s); },
strings: {},
baseLanguage: 'en',
currentLanguage: 'en',
cache: $cacheFactory('strings'),

setIdTransform: function (idTransform) {
this.idTransform = idTransform;
broadcastUpdated();
},

setCurrentLanguage: function (lang) {
this.currentLanguage = lang;
broadcastUpdated();
Expand Down Expand Up @@ -93,8 +112,9 @@ angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http",
},

getStringForm: function (string, n, context) {
var msgId = (typeof string === 'string') ? this.idTransform(string) : string;
var stringTable = this.strings[this.currentLanguage] || {};
var contexts = stringTable[string] || {};
var contexts = stringTable[msgId] || {};
var plurals = contexts[context || noContext] || [];
return plurals[n];
},
Expand Down Expand Up @@ -132,18 +152,6 @@ angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http",
}]);

angular.module('gettext').directive('translate', ["gettextCatalog", "$parse", "$animate", "$compile", "$window", function (gettextCatalog, $parse, $animate, $compile, $window) {
// Trim polyfill for old browsers (instead of jQuery)
// Based on AngularJS-v1.2.2 (angular.js#620)
var trim = (function () {
if (!String.prototype.trim) {
return function (value) {
return (typeof value === 'string') ? value.replace(/^\s*/, '').replace(/\s*$/, '') : value;
};
}
return function (value) {
return (typeof value === 'string') ? value.trim() : value;
};
})();

function assert(condition, missing, found) {
if (!condition) {
Expand All @@ -161,7 +169,7 @@ angular.module('gettext').directive('translate', ["gettextCatalog", "$parse", "$
assert(!attrs.translatePlural || attrs.translateN, 'translate-n', 'translate-plural');
assert(!attrs.translateN || attrs.translatePlural, 'translate-plural', 'translate-n');

var msgid = trim(element.html());
var msgid = element.html();
var translatePlural = attrs.translatePlural;
var translateContext = attrs.translateContext;

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.

22 changes: 21 additions & 1 deletion src/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $h
}
};

// Trim polyfill for old browsers (instead of jQuery)
// Based on AngularJS-v1.2.2 (angular.js#620)
var trim = (function () {
if (!String.prototype.trim) {
return function (value) {
return (typeof value === 'string') ? value.replace(/^\s*/, '').replace(/\s*$/, '') : value;
};
}
return function (value) {
return (typeof value === 'string') ? value.trim() : value;
};
})();

function broadcastUpdated() {
$rootScope.$broadcast('gettextLanguageChanged');
}
Expand All @@ -35,11 +48,17 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $h
showTranslatedMarkers: false,
translatedMarkerPrefix: '[',
translatedMarkerSuffix: ']',
idTransform: function (s) { return trim(s); },
strings: {},
baseLanguage: 'en',
currentLanguage: 'en',
cache: $cacheFactory('strings'),

setIdTransform: function (idTransform) {
this.idTransform = idTransform;
broadcastUpdated();
},

setCurrentLanguage: function (lang) {
this.currentLanguage = lang;
broadcastUpdated();
Expand Down Expand Up @@ -81,8 +100,9 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $h
},

getStringForm: function (string, n, context) {
var msgId = (typeof string === 'string') ? this.idTransform(string) : string;
var stringTable = this.strings[this.currentLanguage] || {};
var contexts = stringTable[string] || {};
var contexts = stringTable[msgId] || {};
var plurals = contexts[context || noContext] || [];
return plurals[n];
},
Expand Down
14 changes: 1 addition & 13 deletions src/directive.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
angular.module('gettext').directive('translate', function (gettextCatalog, $parse, $animate, $compile, $window) {
// Trim polyfill for old browsers (instead of jQuery)
// Based on AngularJS-v1.2.2 (angular.js#620)
var trim = (function () {
if (!String.prototype.trim) {
return function (value) {
return (typeof value === 'string') ? value.replace(/^\s*/, '').replace(/\s*$/, '') : value;
};
}
return function (value) {
return (typeof value === 'string') ? value.trim() : value;
};
})();

function assert(condition, missing, found) {
if (!condition) {
Expand All @@ -28,7 +16,7 @@ angular.module('gettext').directive('translate', function (gettextCatalog, $pars
assert(!attrs.translatePlural || attrs.translateN, 'translate-n', 'translate-plural');
assert(!attrs.translateN || attrs.translatePlural, 'translate-plural', 'translate-n');

var msgid = trim(element.html());
var msgid = element.html();
var translatePlural = attrs.translatePlural;
var translateContext = attrs.translateContext;

Expand Down
16 changes: 16 additions & 0 deletions test/unit/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe("Directive", function () {
catalog = gettextCatalog;
catalog.setStrings("nl", {
Hello: "Hallo",
" Hello friend ": " Hallo amigo ",
"Hello {{name}}!": "Hallo {{name}}!",
"One boat": ["Een boot", "{{count}} boten"],
Archive: { verb: "Archiveren", noun: "Archief" }
Expand All @@ -26,6 +27,21 @@ describe("Directive", function () {
assert.equal(el.text(), "Hello!");
});

it("Should trim whitespace around the message id by default", function () {
catalog.setCurrentLanguage("nl");
var el = $compile("<div><h1 translate> Hello </h1></div>")($rootScope);
$rootScope.$digest();
assert.equal(el.text(), "Hallo");
});

it("Should let the message id transform be customized", function () {
catalog.setCurrentLanguage("nl");
catalog.setIdTransform(function (s) { return s.replace(/\s+/g, " "); });
var el = $compile("<div><h1 translate> Hello friend </h1></div>")($rootScope);
$rootScope.$digest();
assert.equal(el.text(), " Hallo amigo ");
});

it("Should translate known strings", function () {
catalog.setCurrentLanguage("nl");
var el = $compile("<div><h1 translate>Hello</h1></div>")($rootScope);
Expand Down