Skip to content

Commit 846116a

Browse files
committed
Merge branch 'master' into contexts
Conflicts: dist/angular-gettext.js dist/angular-gettext.min.js src/catalog.js
2 parents 76348d4 + fcf3483 commit 846116a

6 files changed

Lines changed: 77 additions & 23 deletions

File tree

bower.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-gettext",
3-
"version": "1.1.2",
3+
"version": "1.1.4",
44
"main": "dist/angular-gettext.js",
55
"ignore": [
66
"**/.*",
@@ -12,10 +12,10 @@
1212
"Gruntfile.js"
1313
],
1414
"dependencies": {
15-
"angular": "~1.2.0"
15+
"angular": ">=1.2.0"
1616
},
1717
"devDependencies": {
1818
"jquery": ">=1.8.0",
19-
"angular-mocks": "~1.2.0"
19+
"angular-mocks": ">=1.2.0"
2020
}
2121
}

dist/angular-gettext.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@ angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http",
1515

1616
var prefixDebug = function (string) {
1717
if (catalog.debug && catalog.currentLanguage !== catalog.baseLanguage) {
18-
return '[MISSING]: ' + string;
18+
return catalog.debugPrefix + string;
19+
} else {
20+
return string;
21+
}
22+
};
23+
24+
var addTranslatedMarkers = function (string) {
25+
if (catalog.showTranslatedMarkers) {
26+
return catalog.translatedMarkerPrefix + string + catalog.translatedMarkerSuffix;
1927
} else {
2028
return string;
2129
}
@@ -27,6 +35,10 @@ angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http",
2735

2836
catalog = {
2937
debug: false,
38+
debugPrefix: '[MISSING]: ',
39+
showTranslatedMarkers: false,
40+
translatedMarkerPrefix: '[',
41+
translatedMarkerSuffix: ']',
3042
strings: {},
3143
baseLanguage: 'en',
3244
currentLanguage: 'en',
@@ -54,14 +66,14 @@ angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http",
5466
broadcastUpdated();
5567
},
5668

57-
getStringForm: function (string, n, gettextContext) {
69+
getStringForm: function (string, n, context) {
5870
var stringTable = this.strings[this.currentLanguage] || {};
5971
var plurals = stringTable[string] || [];
6072
var translation;
6173

6274
// Translation is an object with context bound translations for the string
6375
if (angular.isObject(plurals[0])){
64-
plurals = (plurals[0][gettextContext] || []);
76+
plurals = (plurals[0][context] || []);
6577
if (!angular.isArray(plurals)){
6678
throw new Error('Context bound translations must be wrapped in a array');
6779
}
@@ -70,15 +82,17 @@ angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http",
7082
return translation;
7183
},
7284

73-
getString: function (string, context, gettextContext) {
74-
string = this.getStringForm(string, 0, gettextContext) || prefixDebug(string);
75-
return context ? $interpolate(string)(context) : string;
85+
getString: function (string, scope, context) {
86+
string = this.getStringForm(string, 0, context) || prefixDebug(string);
87+
string = scope ? $interpolate(string)(scope) : string;
88+
return addTranslatedMarkers(string);
7689
},
7790

78-
getPlural: function (n, string, stringPlural, context, gettextContext) {
91+
getPlural: function (n, string, stringPlural, scope, context) {
7992
var form = gettextPlurals(this.currentLanguage, n);
80-
string = this.getStringForm(string, form, gettextContext) || prefixDebug(n === 1 ? string : stringPlural);
81-
return context ? $interpolate(string)(context) : string;
93+
string = this.getStringForm(string, form, context) || prefixDebug(n === 1 ? string : stringPlural);
94+
string = scope ? $interpolate(string)(scope) : string;
95+
return addTranslatedMarkers(string);
8296
},
8397

8498
loadRemote: function (url) {

dist/angular-gettext.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-gettext",
3-
"version": "1.1.2",
3+
"version": "1.1.4",
44
"description": "Gettext support for Angular.js",
55
"main": "dist/angular-gettext.js",
66
"directories": {

src/catalog.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $h
33

44
var prefixDebug = function (string) {
55
if (catalog.debug && catalog.currentLanguage !== catalog.baseLanguage) {
6-
return '[MISSING]: ' + string;
6+
return catalog.debugPrefix + string;
7+
} else {
8+
return string;
9+
}
10+
};
11+
12+
var addTranslatedMarkers = function (string) {
13+
if (catalog.showTranslatedMarkers) {
14+
return catalog.translatedMarkerPrefix + string + catalog.translatedMarkerSuffix;
715
} else {
816
return string;
917
}
@@ -15,6 +23,10 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $h
1523

1624
catalog = {
1725
debug: false,
26+
debugPrefix: '[MISSING]: ',
27+
showTranslatedMarkers: false,
28+
translatedMarkerPrefix: '[',
29+
translatedMarkerSuffix: ']',
1830
strings: {},
1931
baseLanguage: 'en',
2032
currentLanguage: 'en',
@@ -42,14 +54,14 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $h
4254
broadcastUpdated();
4355
},
4456

45-
getStringForm: function (string, n, gettextContext) {
57+
getStringForm: function (string, n, context) {
4658
var stringTable = this.strings[this.currentLanguage] || {};
4759
var plurals = stringTable[string] || [];
4860
var translation;
4961

5062
// Translation is an object with context bound translations for the string
5163
if (angular.isObject(plurals[0])){
52-
plurals = (plurals[0][gettextContext] || []);
64+
plurals = (plurals[0][context] || []);
5365
if (!angular.isArray(plurals)){
5466
throw new Error('Context bound translations must be wrapped in a array');
5567
}
@@ -58,15 +70,17 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $h
5870
return translation;
5971
},
6072

61-
getString: function (string, context, gettextContext) {
62-
string = this.getStringForm(string, 0, gettextContext) || prefixDebug(string);
63-
return context ? $interpolate(string)(context) : string;
73+
getString: function (string, scope, context) {
74+
string = this.getStringForm(string, 0, context) || prefixDebug(string);
75+
string = scope ? $interpolate(string)(scope) : string;
76+
return addTranslatedMarkers(string);
6477
},
6578

66-
getPlural: function (n, string, stringPlural, context, gettextContext) {
79+
getPlural: function (n, string, stringPlural, scope, context) {
6780
var form = gettextPlurals(this.currentLanguage, n);
68-
string = this.getStringForm(string, form, gettextContext) || prefixDebug(n === 1 ? string : stringPlural);
69-
return context ? $interpolate(string)(context) : string;
81+
string = this.getStringForm(string, form, context) || prefixDebug(n === 1 ? string : stringPlural);
82+
string = scope ? $interpolate(string)(scope) : string;
83+
return addTranslatedMarkers(string);
7084
},
7185

7286
loadRemote: function (url) {

test/unit/catalog.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ describe("Catalog", function () {
3939
assert.equal(catalog.getString("Hello"), "[MISSING]: Hello");
4040
});
4141

42+
it("Should add custom prefix for untranslated strings when in debug", function () {
43+
catalog.debug = true;
44+
catalog.debugPrefix = "#X# ";
45+
catalog.currentLanguage = "fr";
46+
assert.equal(catalog.getString("Hello"), "#X# Hello");
47+
});
48+
4249
it("Should not add prefix for untranslated strings in English", function () {
4350
catalog.debug = true;
4451
catalog.currentLanguage = "en";
@@ -136,4 +143,23 @@ describe("Catalog", function () {
136143
assert.equal(catalog.getPlural(2, "There is {{count}} bird", "There are {{count}} birds", { count: 2 }), "There are 2 birds");
137144
assert.equal(catalog.getPlural(1, "There is {{count}} bird", "There are {{count}} birds", { count: 1 }), "There is 1 bird");
138145
});
146+
147+
it("Should add translation markers when enabled", function () {
148+
catalog.showTranslatedMarkers = true;
149+
assert.equal(catalog.getString("Bye"), "[Bye]");
150+
});
151+
152+
it("Should add custom translation markers when enabled", function () {
153+
catalog.showTranslatedMarkers = true;
154+
catalog.translatedMarkerPrefix = "(TRANS: ";
155+
catalog.translatedMarkerSuffix = ")";
156+
assert.equal(catalog.getString("Bye"), "(TRANS: Bye)");
157+
});
158+
159+
it("Should add prefix for untranslated strings and add translation markers when enabled", function () {
160+
catalog.debug = true;
161+
catalog.showTranslatedMarkers = true;
162+
catalog.currentLanguage = "fr";
163+
assert.equal(catalog.getString("Bye"), "[[MISSING]: Bye]");
164+
});
139165
});

0 commit comments

Comments
 (0)