marhali / easy-i18n

This is a IntelliJ IDE based plugin for internationalization. Supports the most common translation file types.
MIT License
80 stars 24 forks source link

feat(Localize Selected): add LocalizeItAction for localizing selected… #345

Closed JPilson closed 4 months ago

JPilson commented 8 months ago

Feature: Localize Selected Text

Body:

Implement a new feature for the IntelliJ IDEA plugin named LocalizeItAction. The main objective of Localize Action is to localize the selected text within the editor without having to open the plugin to add a new one. The action flow should

follow:

  1. User Selects a Text (String Value).
  2. User Invokes the menu , and Select the Localize-it option.
  3. If valid, showcase a dialog to add the selected text as a localized string key to the project.
  4. Then user can update the keys and translation

Should the selected text be empty or the project null, the action will be dismissed and do nothing.

The LocalizeItAction class extends the IntelliJ IDEA's AnAction class

Screenshot 2023-12-09 at 15 08 23 Screenshot 2023-12-09 at 15 09 27 Screenshot 2023-12-09 at 15 09 53
marhali commented 8 months ago

Hello, thanks for your contribution ✌️

There is already a quick action to create a new translation from within the code editor. Right-Click > Show Context Actions > Extract translation or ⌥ + ↵ > Extract translation

This action should target all literal expressions and support both key and value extraction. Let me now, if this is something we should add on top of it :)

JPilson commented 8 months ago

Hello, thanks for your contribution ✌️

There is already a quick action to create a new translation from within the code editor. Right-Click > Show Context Actions > Extract translation or ⌥ + ↵ > Extract translation

This action should target all literal expressions and support both key and value extraction. Let me now, if this is something we should add on top of it :)

Hey, thanks for quick reply I never noticed that there was something in the context actions, but I just noticed that it does not work inside template in a Vue project....

<template>
    <Btn text="Select" @click="getPicks"/>
</template>

If wanted to extract Select from the Btn the extract is not available maybe that something it should have.

Screenshot 2023-12-09 at 19 39 12

What do you think we have it available inside the Vue Template ?

srcmkr commented 8 months ago

@JPilson Can confirm this for Angular project, WebStorm 2023.3

image

JPilson commented 8 months ago

So ,. @marhali shall I adpt it to work on templates?

marhali commented 8 months ago

Yes, the context-based approach might be missing some cases to support translation extraction (e.g. XmlText, XmlAttributeValue, ...).

Your approach with the general "Localize it" action is always based on the selected text, right? I'm open to a discussion on how this functionality should be implemented/extended.

marhali commented 8 months ago

@JPilson Can confirm this for Angular project, WebStorm 2023.3

image

Is this a complete example? Wouldn't you also need to call an i18n service? For example $i18n.t(...) ?

JPilson commented 8 months ago

Yes indeed , I would have to replace the selected string with i18n.t() , I can work on it in the coming days

srcmkr commented 8 months ago

Is this a complete example? Wouldn't you also need to call an i18n service? For example $i18n.t(...) ?

Sure, for ngx-translate there are common two ways:

In HTML by "pipe": {{'modal.connect.message_placeholder'|translate}}

For example:

<button (click)="ExecuteSearch()" class="btn btn-primary">
    {{'buttons.button_search'|translate}}
</button>

with Parameter: {{'modal.connect.message_placeholder'|translate: {key: value} }} JSON: "message_placeholder": "I want to use {{key}}"

in Typescript / Code behind:

export class MyClass {
  constructor( private translate:TranslateService ) {}
  ...
  myFunction(value: string) {
    let translatedString = this.translate.instant('modal.connect.message_placeholder');
    let parameterTranslate = this.translate.instant('modal.connect.message_placeholder', {key: value} ),
  }
}

Both options are often used within one component (HTML + code behind).

JPilson commented 8 months ago

@marhali these last commits brings the following changes:

Implement selectable text replacement with i18n keys

Add i18n flavor template setting

image

image

This is valid for Vue, JS/TS... but not for Angular. Based on what @srcmkr shared it seems that Angular deals with i18n in bit diff way so this solution still needs to properly support Angular.

ABOUT UNIT TESTs NO tests were written yet , but If that is needed before this solution is used I can do it on the next weekend.

marhali commented 4 months ago

I will integrate this into the main branch... I18n flavour template is definitely a useful feature - thanks 👍

JPilson commented 4 months ago

@marhali my pleasure to contribute