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

Request: add the path as argument to the custom formatter #484

Closed
louisameline opened this issue Dec 16, 2018 · 3 comments
Closed

Request: add the path as argument to the custom formatter #484

louisameline opened this issue Dec 16, 2018 · 3 comments

Comments

@louisameline
Copy link

Hi, I'd like to have the path of the message as a parameter of my interpolate function:

class CustomFormatter {
     interpolate (path, message, values) {
       return ['resolved message string']
     }
}

Use case : let's assume my messages are like this:

const messages = {
  en: {
    message: {
      firstDay: 'The first day of the first week of this year was {date}',
      otherMessage: 'lorem ipsum {year}'
    }
  },
  ...
}

The date variable in the output will depend on the current year, but also on internationalization because the first day of the week is Monday in Europe and Sunday in the USA for example. So in Europe it would be like "Monday, January 1st, 2018", while it would be "Saturday, January 9th, 2018" in the USA.

So I'd like to handle this within a i18n custom formatter that would like this :

this.$t('firstDay')

Right now I have to do:

class CustomFormatter {
     interpolate (message, values) {
       if (message == 'The first day of the first week of this year was {date}') {
             // do calculations to determine the first day of the first week
       }
      else {
        // do something else
      }
       return result
     }
}

It's an issue to have to use the message for a comparison, I'd rather do

class CustomFormatter {
     interpolate (path, message, values) {
       if (path == 'firstDay') {
             // do calculations to determine the first day of the first week
       }
      else {
        // do something else
      }
       return result
     }
}

From a wider perspective, the idea behind this request is that the custom formatter is currently pretty useless unless you want to apply the same formatting to all messages. I'd like to be able to insert more logic in a formatter.

Also, I'd like to be able to tell i18n that my formatter will not process all messages. Maybe if my formatter returns false for example, i18n should format the message as usual.

I hope I was clear :) Thank you for your work.

@Raiondesu
Copy link
Contributor

Hello, @louisameline
Would it be ok for the argument to be at the last position instead of the first, like so:

class CustomFormatter {
     interpolate (message, values, path) {
       if (path == 'firstDay') {
             // do calculations to determine the first day of the first week
       }
      else {
        // do something else
      }
       return result
     }
}

?

@louisameline
Copy link
Author

Hello, it's less logical but it's backwards compatible, so yes if you prefer :)

Raiondesu added a commit to Raiondesu/vue-i18n that referenced this issue Dec 19, 2018
kazupon pushed a commit that referenced this issue Dec 21, 2018
…er (#489) by @Raiondesu

* ⚡improvement(types): typed autocomplete in date and number format options

Replace Number and Date format options with standard TS `Intl` types, while also adding guiding TS autocomplete to them.

This change exterminates the confusion of which types to follow, while preserving backwards-compatibility for types and adding optional autocomplete.

* ⚡improvement(types/test): add type constraints to format options

It's useful to check static variables types whenever possible.

* ⚡new(formatter): add path to the formatter arguments
and allow optional formatting

* ⚡improvement(types): backward-compatibility for #484

* 📃docs(formatter): add case for the undefined return type

* ⚡improvement(types): include the possible undefined return type

* ✅tests(issues): make formatter return undefined in test for #484

* update(formatter): change default foramtter invocation
from undefined to null
@louisameline
Copy link
Author

Great, thank you.

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

4 participants