Skip to content

Commit c565c8a

Browse files
committed
translate: add 'language' parameter
It is convenient to fetch a translation for a specific language without running 'translate.initTranslations()' to change the default language. 'language' property in the config could be set outside of translate.js but code that imports only 'vue-gettext' may not have access to the config. Use-case: vue-gettext is used for all translations in a nodejs server that accepts language as a request parameter (or query). It should be able to pass the language to '$gettext()' if it is not the default language.
1 parent b0fcd60 commit c565c8a

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ import {translate} from 'vue-gettext';
457457
const {gettext: $gettext, gettextInterpolate} = translate;
458458

459459
const str = $gettext('Hello, %{name}');
460+
const strFR = $gettext('Hello, %{name}', 'fr');
460461
const interpolated = gettextInterpolate(str, { name: 'Jerom' })
461462
```
462463

src/translate.js

+12-8
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,12 @@ const translate = {
115115
* Also makes the string discoverable by gettext-extract.
116116
*
117117
* @param {String} msgid - The translation key
118+
* @param {String} language - The language ID (e.g. 'fr_FR' or 'en_US')
118119
*
119120
* @return {String} The translated string
120121
*/
121-
'gettext': function (msgid) {
122-
return translate.getTranslation(msgid)
122+
'gettext': function (msgid, language = _config.language) {
123+
return translate.getTranslation(msgid, 1, null, null, language)
123124
},
124125

125126
/*
@@ -128,11 +129,12 @@ const translate = {
128129
*
129130
* @param {String} context - The context of the string to translate
130131
* @param {String} msgid - The translation key
132+
* @param {String} language - The language ID (e.g. 'fr_FR' or 'en_US')
131133
*
132134
* @return {String} The translated string
133135
*/
134-
'pgettext': function (context, msgid) {
135-
return translate.getTranslation(msgid, 1, context)
136+
'pgettext': function (context, msgid, language = _config.language) {
137+
return translate.getTranslation(msgid, 1, context, null, language)
136138
},
137139

138140
/*
@@ -143,11 +145,12 @@ const translate = {
143145
* @param {String} msgid - The translation key
144146
* @param {String} plural - The plural form of the translation key
145147
* @param {Number} n - The number to switch between singular and plural
148+
* @param {String} language - The language ID (e.g. 'fr_FR' or 'en_US')
146149
*
147150
* @return {String} The translated string
148151
*/
149-
'ngettext': function (msgid, plural, n) {
150-
return translate.getTranslation(msgid, n, null, plural)
152+
'ngettext': function (msgid, plural, n, language = _config.language) {
153+
return translate.getTranslation(msgid, n, null, plural, language)
151154
},
152155

153156
/*
@@ -159,11 +162,12 @@ const translate = {
159162
* @param {String} msgid - The translation key
160163
* @param {String} plural - The plural form of the translation key
161164
* @param {Number} n - The number to switch between singular and plural
165+
* @param {String} language - The language ID (e.g. 'fr_FR' or 'en_US')
162166
*
163167
* @return {String} The translated string
164168
*/
165-
'npgettext': function (context, msgid, plural, n) {
166-
return translate.getTranslation(msgid, n, context, plural)
169+
'npgettext': function (context, msgid, plural, n, language = _config.language) {
170+
return translate.getTranslation(msgid, n, context, plural, language)
167171
},
168172

169173
/*

test/specs/translate.spec.js

+14
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ describe('Translate tests', () => {
9292
Vue.config.language = 'en_US'
9393
expect(undetectableGettext('Pending')).to.equal('Pending')
9494

95+
expect(undetectableGettext('Pending', 'fr_FR')).to.equal('En cours')
9596
})
9697

9798
it('tests the pgettext() method', () => {
@@ -104,6 +105,7 @@ describe('Translate tests', () => {
104105
Vue.config.language = 'en_US'
105106
expect(undetectablePgettext('Noun', 'Answer')).to.equal('Answer (noun)')
106107

108+
expect(undetectablePgettext('Noun', 'Answer', 'fr_FR')).to.equal('Réponse (nom)')
107109
})
108110

109111
it('tests the ngettext() method', () => {
@@ -116,6 +118,8 @@ describe('Translate tests', () => {
116118
Vue.config.language = 'en_US'
117119
expect(undetectableNgettext('%{ carCount } car', '%{ carCount } cars', 2)).to.equal('%{ carCount } cars')
118120

121+
expect(undetectableNgettext('%{ carCount } car', '%{ carCount } cars', 2, 'fr_FR')).to.equal('%{ carCount } véhicules')
122+
119123
// If no translation exists, display the default singular form (if n < 2).
120124
Vue.config.language = 'fr_FR'
121125
expect(undetectableNgettext('Untranslated %{ n } item', 'Untranslated %{ n } items', -1))
@@ -148,6 +152,9 @@ describe('Translate tests', () => {
148152
expect(undetectableNpgettext('Verb', '%{ carCount } car (verb)', '%{ carCount } cars (verb)', 1))
149153
.to.equal('%{ carCount } car (verb)')
150154

155+
expect(undetectableNpgettext('Noun', '%{ carCount } car (noun)', '%{ carCount } cars (noun)', 2, 'fr_FR'))
156+
.to.equal('%{ carCount } véhicules (nom)')
157+
151158
// If no translation exists, display the default singular form (if n < 2).
152159
Vue.config.language = 'fr_FR'
153160
expect(undetectableNpgettext('Noun', 'Untranslated %{ n } item (noun)', 'Untranslated %{ n } items (noun)', 1))
@@ -265,6 +272,7 @@ describe('Translate tests without Vue', () => {
265272
config.language = 'en_US'
266273
expect(undetectableGettext('Pending')).to.equal('Pending')
267274

275+
expect(undetectableGettext('Pending', 'fr_FR')).to.equal('En cours')
268276
})
269277

270278
it('tests the pgettext() method', () => {
@@ -277,6 +285,7 @@ describe('Translate tests without Vue', () => {
277285
config.language = 'en_US'
278286
expect(undetectablePgettext('Noun', 'Answer')).to.equal('Answer (noun)')
279287

288+
expect(undetectablePgettext('Noun', 'Answer', 'fr_FR')).to.equal('Réponse (nom)')
280289
})
281290

282291
it('tests the ngettext() method', () => {
@@ -289,6 +298,8 @@ describe('Translate tests without Vue', () => {
289298
config.language = 'en_US'
290299
expect(undetectableNgettext('%{ carCount } car', '%{ carCount } cars', 2)).to.equal('%{ carCount } cars')
291300

301+
expect(undetectableNgettext('%{ carCount } car', '%{ carCount } cars', 2, 'fr_FR')).to.equal('%{ carCount } véhicules')
302+
292303
// If no translation exists, display the default singular form (if n < 2).
293304
config.language = 'fr_FR'
294305
expect(undetectableNgettext('Untranslated %{ n } item', 'Untranslated %{ n } items', -1))
@@ -321,6 +332,9 @@ describe('Translate tests without Vue', () => {
321332
expect(undetectableNpgettext('Verb', '%{ carCount } car (verb)', '%{ carCount } cars (verb)', 1))
322333
.to.equal('%{ carCount } car (verb)')
323334

335+
expect(undetectableNpgettext('Noun', '%{ carCount } car (noun)', '%{ carCount } cars (noun)', 1, 'fr_FR'))
336+
.to.equal('%{ carCount } véhicule (nom)')
337+
324338
// If no translation exists, display the default singular form (if n < 2).
325339
config.language = 'fr_FR'
326340
expect(undetectableNpgettext('Noun', 'Untranslated %{ n } item (noun)', 'Untranslated %{ n } items (noun)', 1))

0 commit comments

Comments
 (0)