Skip to content

Commit

Permalink
Merge pull request #28 from guillaumepotier/msgctxt
Browse files Browse the repository at this point in the history
Context | Remove it from untranslated singular or plural sentences
  • Loading branch information
guillaumepotier authored Oct 16, 2019
2 parents 28b1706 + 0f7f315 commit a266457
Show file tree
Hide file tree
Showing 13 changed files with 424 additions and 938 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
language: node_js

node_js:
- "node"

- 9
script:
- npm run-script test
52 changes: 32 additions & 20 deletions dist/gettext.amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ define(function () { 'use strict';
// sprintf equivalent, takes a string and some arguments to make a computed string
// eg: strfmt("%1 dogs are in %2", 7, "the kitchen"); => "7 dogs are in the kitchen"
// eg: strfmt("I like %1, bananas and %1", "apples"); => "I like apples, bananas and apples"
// NB: removes msg context if there is one present
var strfmt = function (fmt) {
var args = arguments;
var args = arguments;

return fmt
return fmt
// put space after double % to prevent placeholder replacement of such matches
.replace(/%%/g, '%% ')
// replace placeholders
Expand All @@ -58,6 +59,16 @@ define(function () { 'use strict';
.replace(/%% /g, '%')
};

var removeContext = function(str) {
// if there is context, remove it
if (str.indexOf(_ctxt_delimiter) !== -1) {
var parts = str.split(_ctxt_delimiter);
return parts[1];
}

return str;
};

var expand_locale = function(locale) {
var locales = [locale],
i = locale.lastIndexOf('-');
Expand Down Expand Up @@ -90,7 +101,7 @@ define(function () { 'use strict';
var t = function (messages, n, options /* ,extra */) {
// Singular is very easy, just pass dictionnary message through strfmt
if (1 === messages.length)
return strfmt.apply(this, [messages[0]].concat(Array.prototype.slice.call(arguments, 3)));
return strfmt.apply(this, [removeContext(messages[0])].concat(Array.prototype.slice.call(arguments, 3)));

var plural;

Expand All @@ -112,7 +123,7 @@ define(function () { 'use strict';
if ('undefined' === typeof plural.plural || plural.plural > plural.nplurals || messages.length <= plural.plural)
plural.plural = 0;

return strfmt.apply(this, [messages[plural.plural], n].concat(Array.prototype.slice.call(arguments, 3)));
return strfmt.apply(this, [removeContext(messages[plural.plural]), n].concat(Array.prototype.slice.call(arguments, 3)));
};

return {
Expand Down Expand Up @@ -187,23 +198,24 @@ define(function () { 'use strict';
options = {},
key = msgctxt ? msgctxt + _ctxt_delimiter + msgid : msgid,
exist,
locale;
var locales = expand_locale(_locale);
locale,
locales = expand_locale(_locale);

for (var i in locales) {
locale = locales[i];
exist = _dictionary[domain] && _dictionary[domain][locale] && _dictionary[domain][locale][key];

// because it's not possible to define both a singular and a plural form of the same msgid,
// we need to check that the stored form is the same as the expected one.
// if not, we'll just ignore the translation and consider it as not translated.
if (msgid_plural) {
exist = exist && "string" !== typeof _dictionary[domain][locale][key];
} else {
exist = exist && "string" === typeof _dictionary[domain][locale][key];
}
if (exist) {
break;
}
locale = locales[i];
exist = _dictionary[domain] && _dictionary[domain][locale] && _dictionary[domain][locale][key];

// because it's not possible to define both a singular and a plural form of the same msgid,
// we need to check that the stored form is the same as the expected one.
// if not, we'll just ignore the translation and consider it as not translated.
if (msgid_plural) {
exist = exist && "string" !== typeof _dictionary[domain][locale][key];
} else {
exist = exist && "string" === typeof _dictionary[domain][locale][key];
}
if (exist) {
break;
}
}

if (!exist) {
Expand Down
2 changes: 1 addition & 1 deletion dist/gettext.amd.min.js

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

52 changes: 32 additions & 20 deletions dist/gettext.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ var i18n = function (options) {
// sprintf equivalent, takes a string and some arguments to make a computed string
// eg: strfmt("%1 dogs are in %2", 7, "the kitchen"); => "7 dogs are in the kitchen"
// eg: strfmt("I like %1, bananas and %1", "apples"); => "I like apples, bananas and apples"
// NB: removes msg context if there is one present
var strfmt = function (fmt) {
var args = arguments;
var args = arguments;

return fmt
return fmt
// put space after double % to prevent placeholder replacement of such matches
.replace(/%%/g, '%% ')
// replace placeholders
Expand All @@ -58,6 +59,16 @@ var i18n = function (options) {
.replace(/%% /g, '%')
};

var removeContext = function(str) {
// if there is context, remove it
if (str.indexOf(_ctxt_delimiter) !== -1) {
var parts = str.split(_ctxt_delimiter);
return parts[1];
}

return str;
};

var expand_locale = function(locale) {
var locales = [locale],
i = locale.lastIndexOf('-');
Expand Down Expand Up @@ -90,7 +101,7 @@ var i18n = function (options) {
var t = function (messages, n, options /* ,extra */) {
// Singular is very easy, just pass dictionnary message through strfmt
if (1 === messages.length)
return strfmt.apply(this, [messages[0]].concat(Array.prototype.slice.call(arguments, 3)));
return strfmt.apply(this, [removeContext(messages[0])].concat(Array.prototype.slice.call(arguments, 3)));

var plural;

Expand All @@ -112,7 +123,7 @@ var i18n = function (options) {
if ('undefined' === typeof plural.plural || plural.plural > plural.nplurals || messages.length <= plural.plural)
plural.plural = 0;

return strfmt.apply(this, [messages[plural.plural], n].concat(Array.prototype.slice.call(arguments, 3)));
return strfmt.apply(this, [removeContext(messages[plural.plural]), n].concat(Array.prototype.slice.call(arguments, 3)));
};

return {
Expand Down Expand Up @@ -187,23 +198,24 @@ var i18n = function (options) {
options = {},
key = msgctxt ? msgctxt + _ctxt_delimiter + msgid : msgid,
exist,
locale;
var locales = expand_locale(_locale);
locale,
locales = expand_locale(_locale);

for (var i in locales) {
locale = locales[i];
exist = _dictionary[domain] && _dictionary[domain][locale] && _dictionary[domain][locale][key];

// because it's not possible to define both a singular and a plural form of the same msgid,
// we need to check that the stored form is the same as the expected one.
// if not, we'll just ignore the translation and consider it as not translated.
if (msgid_plural) {
exist = exist && "string" !== typeof _dictionary[domain][locale][key];
} else {
exist = exist && "string" === typeof _dictionary[domain][locale][key];
}
if (exist) {
break;
}
locale = locales[i];
exist = _dictionary[domain] && _dictionary[domain][locale] && _dictionary[domain][locale][key];

// because it's not possible to define both a singular and a plural form of the same msgid,
// we need to check that the stored form is the same as the expected one.
// if not, we'll just ignore the translation and consider it as not translated.
if (msgid_plural) {
exist = exist && "string" !== typeof _dictionary[domain][locale][key];
} else {
exist = exist && "string" === typeof _dictionary[domain][locale][key];
}
if (exist) {
break;
}
}

if (!exist) {
Expand Down
Loading

0 comments on commit a266457

Please sign in to comment.