feat(Add Selection): add LocalizeItAction for localizing selected text

This commit introduces a new action, `LocalizeItAction`, into the easyi18n toolset, which allows users to automatically apply localization to a selected string in the text editor. The action is now accessible from the editor's context menu. This provides a more efficient way to localize selected strings compared to manually adding them.
This commit is contained in:
JPilson 2023-12-09 14:43:40 +01:00
parent 6f7982615f
commit 352dc7c47c
2 changed files with 50 additions and 0 deletions

View File

@ -0,0 +1,43 @@
package de.marhali.easyi18n.action;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import de.marhali.easyi18n.dialog.AddDialog;
import de.marhali.easyi18n.model.KeyPath;
import org.jetbrains.annotations.NotNull;
/**
* The LocalizeItAction class represents an IntelliJ IDEA action that localizes selected text.
*
* <p>When this action is performed, it retrieves the selected text from the editor, checks if it is not empty,
* and then displays a dialog to add the selected text as a localized string key to the project.
*
* <p>If the selected text is empty or the project is null, the action does nothing.
*
* <p>This class extends the AnAction class provided by IntelliJ IDEA.
*/
class LocalizeItAction extends AnAction {
@Override
public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
DataContext dataContext = anActionEvent.getDataContext();
Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
if (editor == null)
return;
String text = editor.getSelectionModel().getSelectedText();
if (text == null || text.isEmpty())
return;
Project project = anActionEvent.getProject();
if (project == null) {
throw new RuntimeException("Project is null!");
}
AddDialog dialog = new AddDialog(project, new KeyPath(text), text);
dialog.showAndHandle();
}
}

View File

@ -22,6 +22,13 @@
> >
<add-to-group group-id="NewGroup"/> <add-to-group group-id="NewGroup"/>
</action> </action>
<action id="de.marhali.easyi18n.action.LocalizeItAction"
class="de.marhali.easyi18n.action.LocalizeItAction"
text="Localize It"
description="Apply localization to the selected string"
icon="/icons/translate13.svg">
<add-to-group group-id="EditorPopupMenu" anchor="last"/>
</action>
</actions> </actions>
<extensions defaultExtensionNs="com.intellij"> <extensions defaultExtensionNs="com.intellij">