Skip to content

Commit 4b7e276

Browse files
committed
Merge pull request #119 from austhomp/translation-markers
Adding translated markers and custom missing prefix.
2 parents 6bf6a66 + 6102471 commit 4b7e276

4 files changed

Lines changed: 61 additions & 7 deletions

File tree

dist/angular-gettext.js

Lines changed: 17 additions & 3 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',
@@ -62,13 +74,15 @@ angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http",
6274

6375
getString: function (string, context) {
6476
string = this.getStringForm(string, 0) || prefixDebug(string);
65-
return context ? $interpolate(string)(context) : string;
77+
string = context ? $interpolate(string)(context) : string;
78+
return addTranslatedMarkers(string);
6679
},
6780

6881
getPlural: function (n, string, stringPlural, context) {
6982
var form = gettextPlurals(this.currentLanguage, n);
7083
string = this.getStringForm(string, form) || prefixDebug(n === 1 ? string : stringPlural);
71-
return context ? $interpolate(string)(context) : string;
84+
string = context ? $interpolate(string)(context) : string;
85+
return addTranslatedMarkers(string);
7286
},
7387

7488
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.

src/catalog.js

Lines changed: 17 additions & 3 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',
@@ -50,13 +62,15 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $h
5062

5163
getString: function (string, context) {
5264
string = this.getStringForm(string, 0) || prefixDebug(string);
53-
return context ? $interpolate(string)(context) : string;
65+
string = context ? $interpolate(string)(context) : string;
66+
return addTranslatedMarkers(string);
5467
},
5568

5669
getPlural: function (n, string, stringPlural, context) {
5770
var form = gettextPlurals(this.currentLanguage, n);
5871
string = this.getStringForm(string, form) || prefixDebug(n === 1 ? string : stringPlural);
59-
return context ? $interpolate(string)(context) : string;
72+
string = context ? $interpolate(string)(context) : string;
73+
return addTranslatedMarkers(string);
6074
},
6175

6276
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";
@@ -104,4 +111,23 @@ describe("Catalog", function () {
104111
assert.equal(catalog.getPlural(2, "There is {{count}} bird", "There are {{count}} birds", { count: 2 }), "There are 2 birds");
105112
assert.equal(catalog.getPlural(1, "There is {{count}} bird", "There are {{count}} birds", { count: 1 }), "There is 1 bird");
106113
});
114+
115+
it("Should add translation markers when enabled", function () {
116+
catalog.showTranslatedMarkers = true;
117+
assert.equal(catalog.getString("Bye"), "[Bye]");
118+
});
119+
120+
it("Should add custom translation markers when enabled", function () {
121+
catalog.showTranslatedMarkers = true;
122+
catalog.translatedMarkerPrefix = "(TRANS: ";
123+
catalog.translatedMarkerSuffix = ")";
124+
assert.equal(catalog.getString("Bye"), "(TRANS: Bye)");
125+
});
126+
127+
it("Should add prefix for untranslated strings and add translation markers when enabled", function () {
128+
catalog.debug = true;
129+
catalog.showTranslatedMarkers = true;
130+
catalog.currentLanguage = "fr";
131+
assert.equal(catalog.getString("Bye"), "[[MISSING]: Bye]");
132+
});
107133
});

0 commit comments

Comments
 (0)