Skip to content

Commit

Permalink
Merge pull request #67 from guillaumepotier/bc-plurals
Browse files Browse the repository at this point in the history
[BC Break] Change the way we choose the plural form + string replacement
  • Loading branch information
guillaumepotier authored Jun 19, 2023
2 parents 70d9f04 + eeaea43 commit ca305c0
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 46 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ language: node_js
node_js:
- 9
script:
- npm build
- npm run-script test
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# gettext.js changelog

**[2.0.0]** [BC Break]

- ngettext, dcnpgettext bc break change to match better the GNU gettext
plural form specification.

**[1.0.0]**

- add support for single plural languages
Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,29 @@ i18n.setLocale("fr");
| `pgettext(msgctxt, msgid)` | Translate a string specified by context. | `_p()` |
| `dcnpgettext(domain, msgctxt, msgid, msgid_plural, n)` | Translate a potentially pluralizable string, potentially specified by context, and potentially of a different domain (as specified in `setMessages` or `loadJSON`). | None. |


### Plural forms

`ngettext` and `dcnpgettext` accept a `n` parameter to specify the plural form.

```javascript
i18n.ngettext('There is an apple', 'There are many apples', 1); // There is an apple
i18n.ngettext('There is an apple', 'There are many apples', 10); // There are many apples
i18n.ngettext('There is %1 apple', 'There are %1 apples', 10); // There are %1 apples
i18n.ngettext('There is %1 apple', 'There are %1 apples', 10, 10); // There are 10 apples
```

### Variabilized strings

All four functions above can take extra arguments for variablization.

`gettext('There are %1 in the %2', 'apples', 'bowl');` -> "There are apples in the bowl

`ngettext('One %2', '%1 %2', 10, 'bananas');` -> "10 bananas"
`ngettext('One %2', '%1 %2', 10, 10, 'bananas');` -> "10 bananas"

It uses the public method `i18n.strfmt("string", var1, var2, ...)` which you may re-use elsewhere in your project.


#### Literal percent sign (%)

When you need to have literal percent sign followed by a number (common in Hebrew or Turkish) you can escape it using another percent sign, for example:
Expand Down
4 changes: 2 additions & 2 deletions dist/gettext.amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ define(function () { 'use strict';
/*! gettext.js - Guillaume Potier - MIT Licensed */
var i18n = function (options) {
options = options || {};
this && (this.__version = '1.1.1');
this && (this.__version = '2.0.0');

// default values that could be overriden in i18n() construct
var defaults = {
Expand Down Expand Up @@ -131,7 +131,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, [removeContext(messages[plural.plural]), n].concat(Array.prototype.slice.call(arguments, 3)));
return strfmt.apply(this, [removeContext(messages[plural.plural])].concat(Array.prototype.slice.call(arguments, 3)));
};

return {
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.

4 changes: 2 additions & 2 deletions dist/gettext.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*! gettext.js - Guillaume Potier - MIT Licensed */
var i18n = function (options) {
options = options || {};
this && (this.__version = '1.1.1');
this && (this.__version = '2.0.0');

// default values that could be overriden in i18n() construct
var defaults = {
Expand Down Expand Up @@ -131,7 +131,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, [removeContext(messages[plural.plural]), n].concat(Array.prototype.slice.call(arguments, 3)));
return strfmt.apply(this, [removeContext(messages[plural.plural])].concat(Array.prototype.slice.call(arguments, 3)));
};

return {
Expand Down
2 changes: 1 addition & 1 deletion dist/gettext.cjs.min.js

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

4 changes: 2 additions & 2 deletions dist/gettext.esm.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*! gettext.js - Guillaume Potier - MIT Licensed */
var i18n = function (options) {
options = options || {};
this && (this.__version = '1.1.1');
this && (this.__version = '2.0.0');

// default values that could be overriden in i18n() construct
var defaults = {
Expand Down Expand Up @@ -129,7 +129,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, [removeContext(messages[plural.plural]), n].concat(Array.prototype.slice.call(arguments, 3)));
return strfmt.apply(this, [removeContext(messages[plural.plural])].concat(Array.prototype.slice.call(arguments, 3)));
};

return {
Expand Down
Loading

0 comments on commit ca305c0

Please sign in to comment.