Romanchuk / angular-i18next

angular v10+ integration with i18next v19.4+
MIT License
131 stars 33 forks source link

Interpolation Format issues with numbers / dates or i18nextCap #108

Closed tzahari closed 1 year ago

tzahari commented 1 year ago

Hello,

If I not configure the interpolation: format: option in the i18next.init() then date & numbers formates are shown correctly. But the i18nextCap is not working. i18next::formatter: there was no format function for cap

If I configure the interpolation format with: interpolation: { format: I18NextModule.interpolationFormat(defaultInterpolationFormat) } Then the i18nextCap is working but the formatting of dates / numbers are not working anymore.

Any hint to fix this?

Thanks a lot Best Regards Thomas

Romanchuk commented 1 year ago

@tzahari Sorry for long waiting. Had no time for this project. Could you share some examples? Your full interpolation settings and what are you trying to format.

But the i18nextCap is not working

i18nextCapPipe is the same to i18NextPipe except it calls .t() method with format: 'cap'. So you need a 'cap' formatter to be registered, That is what I18NextModule.interpolationFormat() do:


export function defaultInterpolationFormat(
  value: any,
  format?: string,
  lng?: string
): string {
  if (!value) return value;
  switch (format) {
    case 'upper':
    case 'uppercase':
      return value.toUpperCase();
    case 'lower':
    case 'lowercase':
      return value.toLowerCase();
    case 'cap':
    case 'capitalize':
      return value.charAt(0).toUpperCase() + value.slice(1);
    case null:
    case undefined:
    case 'none':
    default:
      return value;
  }
}

So 18nextCap in order to work need defaultInterpolationFormat (It is what original i18next doensn't have)

Pass to I18NextModule.interpolationFormat your custom formater function to have both: 1 - 'cap' formatter and etc, 2 - your own formatters with dates, numbers, whatever

Romanchuk commented 1 year ago

Closed due inactivity