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

Possibility to translate "message" properties of json configs #7472

Closed
ghost opened this issue Jan 18, 2024 · 6 comments
Closed

Possibility to translate "message" properties of json configs #7472

ghost opened this issue Jan 18, 2024 · 6 comments
Labels
status: needs discussion triage needs further discussion

Comments

@ghost
Copy link

ghost commented Jan 18, 2024

What is the problem you're trying to solve?

When a custom stylelint config is saved in .js format, we are able to write the message property in 2 ways:

Scenario 1: JS - Hardcoded message with variable

export const customConfig = {
  'unit-disallowed-list': [
    [
      'px',
    ],
    {
      message: 'print this custom message when this error is triggered: { unit }',
    },
  ],
};

or even:

Scenario 2: JS - translated message with variable

import i18next from 'i18next';
import translationFile from './pt-BR.json';

i18next.init({
  debug: false,
  lng: 'es',
  resources: {
    es: {
      translation: translationFile,
    },
  },
});
  
export const customConfig = {
  'unit-disallowed-list': [
    [
      'px',
    ],
    {
      message: (unit) => {
        return i18next.t('unit-disallowed-list', { unit });
      },
    },
  ],
};

Both scenarios are going to be applied as expected:

import customConfig from './customConfig.js';

export const stylelintConfig = {
  rules: {
    ...customConfig,
  }
};

And in the terminal, both the variables and translated messages are processed when they are printed.


My doubt is: instead of saving customConfig in .js format, when saving it in .json format, how can I continue to achieve these 2 scenarios?

Attempt 1: Json - Hardcoded message with variable

...
"message": "print this custom message when this error is triggered: { unit }",
...

The variable {unit} is no longer recognized by Stylelint.

Attemp 2: Json - translated message with variable

...
"message": "(unit) => { return i18next.t('unit-disallowed-list', { unit }); }",
...

or

...
"message": "{{translatedMessage}} { unit }",
...

The variable {unit} is no longer recognized by Stylelint, so I can't even try to translate it with an external library like i18next.

What solution would you like to see?

A solution I would like to achieve for the short term is to be able to: convert a custom Stylelint config from the .js format to the .json format, and to maintain the possibility of saving a custom message that contains default variables provided by Stylelint, such as {unit} in the example above.
Better yet, would be if: 1 - Stylelint could provide translations by default, so I don't need to deal with these conversions of file format and inserting a variable in the .json file; .. and if Stylelint doesn't want to include translations by default, then it would still be good if Stylelint could at least provide a helper function to enable us to provide a translated .js or .json file fullfilled with messages in another language, instead of english, and this somehow would be merged with custom configs that exist in the .json format. As a result, when it comes the time to print messages in terminal, these would be shown translated.

@Mouvedia Mouvedia added the status: needs discussion triage needs further discussion label Jan 18, 2024
@ybiquitous
Copy link
Member

In JSON config, can you try %s? Document:

`.stylelintrc.json`:
<!-- prettier-ignore -->
```json
{
"color-no-hex": [true, {
"message": "Don't use hex colors like \"%s\""
}]
}
```
With formats that don't support a function like JSON, you can use a `printf`-like format (e.g., `%s`). On the other hand, with JS format, you can use both a `printf`-like format and a function.

@ghost
Copy link

ghost commented Jan 19, 2024

This more immediate request worked for now. Thank you. I didn't find this information before...

I believe it remain open other suggestions in this thread.

@ghost
Copy link

ghost commented Jan 20, 2024

Important to say that the variable "%s", as provided by @ybiquitous, doesn't work currently in all rules. This seems to be a Stylelint bug. For example: "color-named", "font-family-name-quotes" and "font-weight-notation" rules. A translated message for these rules, saved in .json file, is correctly being shown in the terminal, except its "%s" variable.

@ybiquitous
Copy link
Member

ybiquitous commented Jan 20, 2024

Right, the feature is still experimental, so there are some rules not supporting it. Since we have a plan to stabilize this feature (#6966), we welcome your contribution if you're interested.

EDIT: PR #6570 is an example of adding the feature support.

@jeddy3
Copy link
Member

jeddy3 commented Jan 20, 2024

Closing as a dupe of #6966.

Please consider contributing to that issue if you have time so that we can provide the feature for all rules.

@jeddy3 jeddy3 closed this as completed Jan 20, 2024
@ghost
Copy link

ghost commented Jan 23, 2024

oh i dont understand a lot of code, sorry, i wish i could, you were nice
boys,please don't forget to add these features when you can, ok?
we want to talk more in our mother language, english not anymore in english

xiexie!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs discussion triage needs further discussion
Development

No branches or pull requests

3 participants