Skip to content
This repository was archived by the owner on Dec 31, 2024. It is now read-only.
This repository was archived by the owner on Dec 31, 2024. It is now read-only.

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

Description

@louisameline

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions