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

Allowing LocaleMessage to be Functions #969

Closed
philippevezina opened this issue Aug 11, 2020 · 7 comments · Fixed by #972
Closed

Allowing LocaleMessage to be Functions #969

philippevezina opened this issue Aug 11, 2020 · 7 comments · Fixed by #972
Labels
Status: Proposal Request for comments Type: Feature Includes new features

Comments

@philippevezina
Copy link

I am trying to do something pretty complex. Here is an example that I have in French of different translations:

  • Manger de la soupe
  • Manger une pomme
  • Manger du pain
  • Manger de l'orge

As you can see, the article preceding the noun will vary based on gender and phonetics.

Proposal

If we could have functions as a LocaleMessage, it would allow doing these more complex translations.
Example

export default {
  fr: {
    sentence: function (values) {
       let article
       if (values.word.startsWith(...)) {
         article = "de l'"
       } else {
         article = 'du'
       }
       return `Manger ${article} ${value['word']}`
    }
  }
}

Thoughts? Comments?

@maxKimoby
Copy link

maxKimoby commented Aug 11, 2020

Yeah, seems like a good idea. Is it doable?

@philippevezina
Copy link
Author

It could mostly be useful to handle complex grammar rules in some languages such as gender (masculine/feminine) that we find in Latin languages.

@exoego exoego added Status: PR Welcome Welcome to Pull Request Type: Feature Includes new features help wanted Extra attention is needed labels Aug 11, 2020
@exoego
Copy link
Collaborator

exoego commented Aug 11, 2020

Great thanks for proposal!
This sounds worth to have for many languages.
Pull requests are welcome 🙏

@kazupon
Copy link
Owner

kazupon commented Aug 12, 2020

Hmm, 🤔
We have to take that into account in vue-i18n-next as well.
https://github.com/intlify/vue-i18n-next/blob/882a4a1185bd95bbdb30e389301b165651fc8a01/src/core/context.ts#L44-L58

vue-i18n-next compiles the locale text messages into functions to improve performance.

code generate example: https://github.com/intlify/vue-i18n-next/blob/master/test/message/__snapshots__/compiler.test.ts.snap
runtime code: https://github.com/intlify/vue-i18n-next/blob/master/src/message/runtime.ts

@philippevezina
Copy link
Author

@kazupon Should two PRs be done? One for this repo and on for vue-i18n-next?

@kazupon
Copy link
Owner

kazupon commented Aug 12, 2020

@philippevezina

This proposal is good too me, because it will be fully customizable for each language.

To adopt this proposal into vue-i18n, I will try to think in a bit of function I/O specs, in order to maintain compatibility with vue-i18n-next.

In any case, this proposal was a good inspiration for the vue-i18n-next compiler like the Vue render functions.
I would like to improve the specification of the msg function generated by the vue-i18n-next compiler by including this proposal. :)

@kazupon kazupon added Status: Proposal Request for comments and removed Status: PR Welcome Welcome to Pull Request help wanted Extra attention is needed labels Aug 12, 2020
@kazupon
Copy link
Owner

kazupon commented Aug 13, 2020

@philippevezina

I've implemented the message function.
#972

You can find the type definitions (message function I/O) of this function here.
https://github.com/kazupon/vue-i18n/pull/972/files#diff-88bec6beae84369c0376b56b3bb88fe1R9-R14

For about use case, please see this tests.
https://github.com/kazupon/vue-i18n/pull/972/files#diff-d8f3b43f12dcdbccd0af8f77c6e968c9R1-R55

Basically, I just ported from message function which is implemented in vue-i18n-next.
You can only use named formatting and list formatting.
You can't use Linked locale messages and pluralization functions because they are not parse processed in the current implementation. This is a limitation of vue-i18n v8.20 later.

Feedback, welcome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Proposal Request for comments Type: Feature Includes new features
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants