Skip to content

Commit

Permalink
Changed the code that retrieves a translation given a context.
Browse files Browse the repository at this point in the history
Previously the format of the catalog for translations with context was
[{ctxA: t1, ctxB: t2}, {ctxA: t1_plural, ctxB: t2_plural}] which was not
very clever (duplicate info, bad structure, sorry!).

Now the format is the more concise [{ctxA: [t1, t1_plural], ctxB: [t2, t2_plural]}].

The wrapping arrays in the context object values are mandatory even if
only one element is present (no plurals).

The external array wrapping is optional but allowed to achieve
consistency with how things were already done before context
translations were allowed.
  • Loading branch information
cristiano2lopes committed Sep 6, 2014
1 parent 26d7d3f commit e649412
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 19 deletions.
12 changes: 8 additions & 4 deletions dist/angular-gettext.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,15 @@ angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http",
getStringForm: function (string, n, gettextContext) {
var stringTable = this.strings[this.currentLanguage] || {};
var plurals = stringTable[string] || [];
var translation = plurals[n];
if (angular.isObject(translation)){
//Translation is an object with context bound translations for the string
translation = translation[gettextContext];
var translation;
//Translation is an object with context bound translations for the string
if (angular.isObject(plurals[0])){
plurals = (plurals[0][gettextContext] || []);
if (!angular.isArray(plurals)){
throw new Error('Context bound translations must be wrapped in a array');
}
}
translation = plurals[n];
return translation;
},

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.

12 changes: 8 additions & 4 deletions src/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $h
getStringForm: function (string, n, gettextContext) {
var stringTable = this.strings[this.currentLanguage] || {};
var plurals = stringTable[string] || [];
var translation = plurals[n];
if (angular.isObject(translation)){
//Translation is an object with context bound translations for the string
translation = translation[gettextContext];
var translation;
//Translation is an object with context bound translations for the string
if (angular.isObject(plurals[0])){
plurals = (plurals[0][gettextContext] || []);
if (!angular.isArray(plurals)){
throw new Error('Context bound translations must be wrapped in a array');
}
}
translation = plurals[n];
return translation;
},

Expand Down
20 changes: 19 additions & 1 deletion test/unit/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ describe("Catalog", function () {
it("Should get the translation according to the context in which the string is used", function () {
catalog.currentLanguage = "pt-BR";
catalog.setStrings("pt-BR", {
beautiful: [{ male: "bonito", female: "bonita" }, { male: "bonitos", female: "bonitas" }]
beautiful: [{
male: ["bonito", "bonitos"],
female: ["bonita", "bonitas"]
}]
});
assert.equal(catalog.getString("beautiful", undefined, "male"), "bonito");
assert.equal(catalog.getString("beautiful", undefined, "female"), "bonita");
Expand All @@ -89,6 +92,21 @@ describe("Catalog", function () {
assert.equal(catalog.getPlural(2, "beautiful", "beautiful", undefined), "beautiful");
});

it("Only allows context bound translations wrapped in an array", function () {
catalog.currentLanguage = "pt-BR";
catalog.setStrings("pt-BR", {
beautiful: [{
male: "bonito",
female: ["bonita"]
}]
});
var errorCall = function () {
catalog.getString("beautiful", undefined, "male");
};
assert.throws(errorCall, Error, "Context bound translations must be wrapped in a array");
assert.equal(catalog.getString("beautiful", undefined, "female"), "bonita");
});

it("Should add prefix for untranslated plural strings when in debug (single)", function () {
catalog.debug = true;
catalog.currentLanguage = "nl";
Expand Down
12 changes: 4 additions & 8 deletions test/unit/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,14 @@ describe("Directive", function () {
catalog.setStrings("pt-BR", {
Developer: [
{
male: "Programador",
female: "Programadora"
},
{
male: "{{count}} Programadores",
female: "{{count}} Programadoras"
male: ["Programador", "{{count}} Programadores"],
female: ["Programadora", "{{count}} Programadoras"]
}
],
"{{gratitude}} developer!": [
{
male: "{{gratitude}} programador!",
female: "{{gratitude}} programadora!"
male: ["{{gratitude}} programador!"],
female: ["{{gratitude}} programadora!"]
}
]
});
Expand Down
2 changes: 1 addition & 1 deletion test/unit/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe("Filter", function () {
"One boat": ["Een boot", "{{count}} boten"]
});
catalog.setStrings("pt-BR", {
Hello: [{ Bull: "Olé", Person: "Olá" }]
Hello: [{ Bull: ["Olé"], Person: ["Olá"] }]
});
}));

Expand Down

0 comments on commit e649412

Please sign in to comment.