question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to pass args to the i18n messages

See original GitHub issue

Versions:

  • VueJs: 2.5.17
  • Vee-Validate: 2.1.0-beta.9

Description:

Hey there, it’s a question. I want to know how to pass args to the i18n messages, I don’t found any example.

for better explanation, an example:

I have a validator min (minlength)

export default (value, [length]) => {
    length = Number(length);
    return value.length >= length;
}; 

And I’ve a validation message in i18n object

Digite no mínimo {length} caracteres

I want to use the length arg to generate the i18n message correctly. (To manage the i18n messages, I’m using the vue-i18n library.)

I saw in this line the I18nDictionary.getMessage pass a data param to Vuei18n.t , but I can’t understand hot to use exactly (if it’s possible)

Thank you!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
ssnetxyzcommented, Sep 21, 2018

I’m not related to this project, but here is how I did it. VueI18n does not accept functions, only strings. But you can use {0} for the first function argument and {1} for the second (and so on). So In my case:

const i18n = new VueI18n({ locale: 'ja', messages });
Vue.use(VeeValidate, { i18n });
VeeValidate.Validator.extend('is_japanese', {    
    validate: value => reAllKanji.test(value)
});

messages.js

export default {
  ja: {
    validation: {
      messages: {
        is_japanese: "{0}は日本人ではありません!",
        min: '{0}は{1}文字以上で入力してください',
      }
    }
  },
 en: { ... 

I do however believe you can use VeeValidate.Validator.localize() to convert your vee-validate message functions to strings…

So far I think vee-validate handles localization better than Vuei18n.

3reactions
henriquecustodiacommented, Sep 21, 2018

Thank you @ssnetxyz, using {1} I can use the param value in message.

Digite no mínimo {1} caracteres
  • The {0} is field name
  • The {1} is param values
  • The {2} is data values

For advanced interpolation I think is necessary create a custom formatter, because I can’t use named params in the message, but it is just curiosity haha

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to pass arguments in I18n.translate - Stack Overflow
You can pass the params after the key. I18n.translate('error.messages.greater_than_or_equal_to', count: 2).
Read more >
How To Pass a Variable to Your Translation String With i18next
Here, we looked at how to pass one or more parameters to your i18next translation strings. This can be easily achieved by taking...
Read more >
Interpolation - i18next documentation
By default, the values get escaped to mitigate XSS attacks. You can toggle escaping off, by either putting - before the key, or...
Read more >
Message Format Syntax - Vue I18n
The first argument is message.hello as the locale messages key, and the second argument is an object with msg property as a parameter...
Read more >
passing parameter to the i18n text from xml view in fiori app
So you have to create a formatter and pass all the strings to the formatter and then do it inside JS. In JS...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found