diff --git a/README.md b/README.md index 5770551..eb9586e 100644 --- a/README.md +++ b/README.md @@ -457,6 +457,7 @@ import {translate} from 'vue-gettext'; const {gettext: $gettext, gettextInterpolate} = translate; const str = $gettext('Hello, %{name}'); +const strFR = $gettext('Hello, %{name}', 'fr'); const interpolated = gettextInterpolate(str, { name: 'Jerom' }) ``` diff --git a/src/translate.js b/src/translate.js index d5cbe99..def910f 100644 --- a/src/translate.js +++ b/src/translate.js @@ -115,11 +115,12 @@ const translate = { * Also makes the string discoverable by gettext-extract. * * @param {String} msgid - The translation key + * @param {String} language - The language ID (e.g. 'fr_FR' or 'en_US') * * @return {String} The translated string */ - 'gettext': function (msgid) { - return translate.getTranslation(msgid) + 'gettext': function (msgid, language = _config.language) { + return translate.getTranslation(msgid, 1, null, null, language) }, /* @@ -128,11 +129,12 @@ const translate = { * * @param {String} context - The context of the string to translate * @param {String} msgid - The translation key + * @param {String} language - The language ID (e.g. 'fr_FR' or 'en_US') * * @return {String} The translated string */ - 'pgettext': function (context, msgid) { - return translate.getTranslation(msgid, 1, context) + 'pgettext': function (context, msgid, language = _config.language) { + return translate.getTranslation(msgid, 1, context, null, language) }, /* @@ -143,11 +145,12 @@ const translate = { * @param {String} msgid - The translation key * @param {String} plural - The plural form of the translation key * @param {Number} n - The number to switch between singular and plural + * @param {String} language - The language ID (e.g. 'fr_FR' or 'en_US') * * @return {String} The translated string */ - 'ngettext': function (msgid, plural, n) { - return translate.getTranslation(msgid, n, null, plural) + 'ngettext': function (msgid, plural, n, language = _config.language) { + return translate.getTranslation(msgid, n, null, plural, language) }, /* @@ -159,11 +162,12 @@ const translate = { * @param {String} msgid - The translation key * @param {String} plural - The plural form of the translation key * @param {Number} n - The number to switch between singular and plural + * @param {String} language - The language ID (e.g. 'fr_FR' or 'en_US') * * @return {String} The translated string */ - 'npgettext': function (context, msgid, plural, n) { - return translate.getTranslation(msgid, n, context, plural) + 'npgettext': function (context, msgid, plural, n, language = _config.language) { + return translate.getTranslation(msgid, n, context, plural, language) }, /* diff --git a/test/specs/translate.spec.js b/test/specs/translate.spec.js index 225c0ba..93dec43 100644 --- a/test/specs/translate.spec.js +++ b/test/specs/translate.spec.js @@ -92,6 +92,7 @@ describe('Translate tests', () => { Vue.config.language = 'en_US' expect(undetectableGettext('Pending')).to.equal('Pending') + expect(undetectableGettext('Pending', 'fr_FR')).to.equal('En cours') }) it('tests the pgettext() method', () => { @@ -104,6 +105,7 @@ describe('Translate tests', () => { Vue.config.language = 'en_US' expect(undetectablePgettext('Noun', 'Answer')).to.equal('Answer (noun)') + expect(undetectablePgettext('Noun', 'Answer', 'fr_FR')).to.equal('Réponse (nom)') }) it('tests the ngettext() method', () => { @@ -116,6 +118,8 @@ describe('Translate tests', () => { Vue.config.language = 'en_US' expect(undetectableNgettext('%{ carCount } car', '%{ carCount } cars', 2)).to.equal('%{ carCount } cars') + expect(undetectableNgettext('%{ carCount } car', '%{ carCount } cars', 2, 'fr_FR')).to.equal('%{ carCount } véhicules') + // If no translation exists, display the default singular form (if n < 2). Vue.config.language = 'fr_FR' expect(undetectableNgettext('Untranslated %{ n } item', 'Untranslated %{ n } items', -1)) @@ -148,6 +152,9 @@ describe('Translate tests', () => { expect(undetectableNpgettext('Verb', '%{ carCount } car (verb)', '%{ carCount } cars (verb)', 1)) .to.equal('%{ carCount } car (verb)') + expect(undetectableNpgettext('Noun', '%{ carCount } car (noun)', '%{ carCount } cars (noun)', 2, 'fr_FR')) + .to.equal('%{ carCount } véhicules (nom)') + // If no translation exists, display the default singular form (if n < 2). Vue.config.language = 'fr_FR' expect(undetectableNpgettext('Noun', 'Untranslated %{ n } item (noun)', 'Untranslated %{ n } items (noun)', 1)) @@ -265,6 +272,7 @@ describe('Translate tests without Vue', () => { config.language = 'en_US' expect(undetectableGettext('Pending')).to.equal('Pending') + expect(undetectableGettext('Pending', 'fr_FR')).to.equal('En cours') }) it('tests the pgettext() method', () => { @@ -277,6 +285,7 @@ describe('Translate tests without Vue', () => { config.language = 'en_US' expect(undetectablePgettext('Noun', 'Answer')).to.equal('Answer (noun)') + expect(undetectablePgettext('Noun', 'Answer', 'fr_FR')).to.equal('Réponse (nom)') }) it('tests the ngettext() method', () => { @@ -289,6 +298,8 @@ describe('Translate tests without Vue', () => { config.language = 'en_US' expect(undetectableNgettext('%{ carCount } car', '%{ carCount } cars', 2)).to.equal('%{ carCount } cars') + expect(undetectableNgettext('%{ carCount } car', '%{ carCount } cars', 2, 'fr_FR')).to.equal('%{ carCount } véhicules') + // If no translation exists, display the default singular form (if n < 2). config.language = 'fr_FR' expect(undetectableNgettext('Untranslated %{ n } item', 'Untranslated %{ n } items', -1)) @@ -321,6 +332,9 @@ describe('Translate tests without Vue', () => { expect(undetectableNpgettext('Verb', '%{ carCount } car (verb)', '%{ carCount } cars (verb)', 1)) .to.equal('%{ carCount } car (verb)') + expect(undetectableNpgettext('Noun', '%{ carCount } car (noun)', '%{ carCount } cars (noun)', 1, 'fr_FR')) + .to.equal('%{ carCount } véhicule (nom)') + // If no translation exists, display the default singular form (if n < 2). config.language = 'fr_FR' expect(undetectableNpgettext('Noun', 'Untranslated %{ n } item (noun)', 'Untranslated %{ n } items (noun)', 1))