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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use i18n.format after adding formatter function with new API #1913

Closed
marcalexiei opened this issue Feb 17, 2023 · 1 comment
Closed

Comments

@marcalexiei
Copy link
Member

marcalexiei commented Feb 17, 2023

馃悰 Bug Report

Using i18n.format function doesn't work with new formatters (i18next.services.formatter.add).

When setting format function inside interpolation configuration I can safely format values without passing through i18n.t.

i18n.format(variable, 'formatter')

Now when adding a formatter with the new syntax this no longer works and a warning is issued in console:

image

The affected line should be this one

On typescript definition FormatFunction has format, lng, options parameters marked as optional.
The fix could just add default value to options param as {}. From the looks of it it shouldn't break other checks.

To Reproduce

  1. Go to the code sandbox
  2. Check the console

"test" string is not formatted

import { default as i18n } from "i18next";

i18n.init({
  lng: "en",
  resources: {
    en: {
      translation: { text: "my {{value, uppercase}}" }
    }
  },
  interpolation: {
    escapeValue: false // react already safes from xss
  },
  debug: true
});

i18n.services.formatter.add("uppercase", (value: string) => {
  console.info("formatter function");
  return value.toUpperCase();
});

console.info("using t with formatted inside resource ===>", i18n.t("translation:text", { value: "value" }));

console.info("using i18n.format ===>", i18n.format("test", "uppercase"));

Expected behaviour

Format function is applied to the value passed to i18n.format so the result should be TEST.

Your Environment

  • runtime version: i.e. Chrome 110.0.5481.100
  • i18next version: i.e. 22.4.9
  • os: MacOS
@adrai adrai closed this as completed in ffc12d6 Feb 17, 2023
@adrai
Copy link
Member

adrai commented Feb 17, 2023

v22.4.10 should address this

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