Skip to content

Commit

Permalink
Merge branch 'janlazo-translate-gettext-lang'
Browse files Browse the repository at this point in the history
  • Loading branch information
kemar committed Dec 18, 2020
2 parents 939e056 + 87f5f24 commit bfd7d49
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
```

Expand Down
20 changes: 12 additions & 8 deletions src/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},

/*
Expand All @@ -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)
},

/*
Expand All @@ -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)
},

/*
Expand All @@ -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)
},

/*
Expand Down
14 changes: 14 additions & 0 deletions test/specs/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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))
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit bfd7d49

Please sign in to comment.