Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BC Break] Change the way we choose the plural form + string replacement #67

Merged
merged 4 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -120,17 +120,30 @@ i18n.setLocale('fr');
* **`pgettext(msgctxt, msgid)`**: Translate a string specified by context. Shorthand is **`_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`). No shorthand.


### 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, ...)` you could
reuse 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