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

Support passing array-like object in list formatting. #213

Closed
myst729 opened this issue Aug 24, 2017 · 1 comment
Closed

Support passing array-like object in list formatting. #213

myst729 opened this issue Aug 24, 2017 · 1 comment

Comments

@myst729
Copy link
Contributor

myst729 commented Aug 24, 2017

⚠️ This may be a big architectural change. However, I think it's worth doing for future extendability.

In need of propose a new feature (#214), I spent some time reading the source code. And found that list formatting is implemented individually with named formatting. For messages interpolated with "numbers":

messages: {
  en: {
    msg: 'This is {0} and {1}.'
  }
}

I could only pass an array:

<div>{{ $t('msg', ['bold', 'italic']) }}</div>

Passing an object (that look like an array) is not allowed:

<div>{{ $t('msg', {'0': 'bold', '1': 'italic'}) }}</div>

Following warning message appears, and the translation fails:

[vue-i18n] Type of token 'list' and format of value 'named' don't match!

This is a bit too strict to the users. And it doesn't behave consistently with JavaScript.

// These two expressions output the same result: ['0', '1']
Object.keys(['bold', 'italic'])
Object.keys({'0': 'bold', '1': 'italic'})

Most i18n libs or components like Dojo, Angular, etc are making list formatting syntax sugar of named formatting. Since in JavaScript, arrays are objects too.

This benefits both sides:

  • To the users, it brings flexibility of formatting messages interpolated with "numbers".
  • To the developers, reuse of such mechanism makes it cleaning under the hood. And easier to ship new features.
@myst729
Copy link
Contributor Author

myst729 commented Aug 28, 2017

The change is actually much easier than I thought, and has been implemented in #218

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants