lokalise / i18n-ally

🌍 All in one i18n extension for VS Code
https://marketplace.visualstudio.com/items?itemName=lokalise.i18n-ally
MIT License
4.01k stars 323 forks source link

Support for Setting Default Values from Language Files in Refactor Templates #1031

Open andrew-bierman opened 1 year ago

andrew-bierman commented 1 year ago

Is your feature related to a specific framework or general for this extension

General. But my experience is using vue-18n

Is your feature request related to a problem? Please describe.

I am looking to set up the i18n Ally extension to automatically add default values from language files when refactoring templates. The goal is to have the original text from the language file used as a default value in the refactored code. Here's an illustration of the desired outcome:

<p>Hello world!</p>
⬇
<p>{{ $t('helloWorld', 'Hello world!') }}
OR
<p>{{ $t('helloWorld', { defaultValue: 'Hello world!' }) }}</p>

I attempted to achieve this by modifying the refactor.templates setting in my settings.json file as follows:

"i18n-ally.refactor.templates": [
    {
        "default": true,
        "template": "$t('{key}'{args}{text})",
    },
]

I expected {text} to be replaced by the original text from the language file, but it appears verbatim as {text} in the refactored code.

I have explored the extension's source code and documentation but could not find a way to reference the original text in the refactor.templates configuration.

Could you provide guidance on how to configure i18n Ally to support this, or is this a feature that could be considered for a future release? The ability to automatically set default values from language files during the refactoring process would be a highly valuable addition to the i18n Ally extension.

Describe the solution you'd like

Ideally, I'd like a way to specify in the refactor.templates settings how to reference the original text from the language file. Perhaps a new placeholder could be introduced, like {originalText} or {defaultValue}, which would be replaced by the original text from the language file when refactoring. This way, the refactored code would have the original text as a default value, either as a second argument to $t or as a property in an options object passed to $t. Here's how it might look:

"i18n-ally.refactor.templates": [
    {
        "default": true,
        "template": "$t('{key}'{args}, '{originalText}')",
    },
    {
        "default": true,
        "template": "$t('{key}'{args}, { defaultValue: '{originalText}' })",
    }
]

In these examples, {originalText} would be replaced by the original text from the language file. This would provide a flexible and straightforward way to include default values from language files in the refactored code.

Additional context

The ability to set default values from language files during the refactoring process would greatly streamline the process of internationalizing a codebase, especially one with a large number of hard-coded strings. It would ensure that the original text is preserved as a default value, making the application more robust in cases where a translation is missing or unavailable. This feature would significantly enhance the utility and user-friendliness of the i18n Ally extension, making it even more valuable for developers working on internationalized applications.

Thank you for your time and consideration. I look forward to any guidance you can provide or to seeing this feature in a future release of i18n Ally.

andrew-bierman commented 1 year ago

For reference, this is the i18n / i18next feature I'm talking about: https://www.i18next.com/translation-function/essentials#passing-a-default-value

frozencap commented 3 weeks ago

yes please