Add flavorTemplate for i18n string replacement and refactor LocalizeItAction
The `flavorTemplate` has been introduced in `ProjectSettingsState` to allow developers to customize the i18n string replacement format. In addition, the `LocalizeItAction` class has been simplified by refactoring commented details into JavaDocs and adding methods to replace selected text and build replacement strings. A new utility class, `DocumentUtil`, checks the document type during string replacement.
This commit is contained in:
parent
c56812c888
commit
5b420a7bcc
@ -4,9 +4,7 @@ import com.intellij.openapi.actionSystem.AnAction;
|
|||||||
import com.intellij.openapi.actionSystem.AnActionEvent;
|
import com.intellij.openapi.actionSystem.AnActionEvent;
|
||||||
import com.intellij.openapi.actionSystem.CommonDataKeys;
|
import com.intellij.openapi.actionSystem.CommonDataKeys;
|
||||||
import com.intellij.openapi.actionSystem.DataContext;
|
import com.intellij.openapi.actionSystem.DataContext;
|
||||||
import com.intellij.openapi.application.ApplicationManager;
|
|
||||||
import com.intellij.openapi.command.WriteCommandAction;
|
import com.intellij.openapi.command.WriteCommandAction;
|
||||||
import com.intellij.openapi.editor.Document;
|
|
||||||
import com.intellij.openapi.editor.Editor;
|
import com.intellij.openapi.editor.Editor;
|
||||||
import com.intellij.openapi.project.Project;
|
import com.intellij.openapi.project.Project;
|
||||||
import de.marhali.easyi18n.dialog.AddDialog;
|
import de.marhali.easyi18n.dialog.AddDialog;
|
||||||
@ -16,14 +14,7 @@ import de.marhali.easyi18n.util.DocumentUtil;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The LocalizeItAction class represents an IntelliJ IDEA action that localizes selected text.
|
* Represents an action to localize text in the editor.
|
||||||
*
|
|
||||||
* <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 {
|
class LocalizeItAction extends AnAction {
|
||||||
|
|
||||||
@ -53,6 +44,14 @@ class LocalizeItAction extends AnAction {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replaces the selected text in the editor with a new text generated from the provided key.
|
||||||
|
*
|
||||||
|
* @param project the project where the editor belongs
|
||||||
|
* @param editor the editor where the text is selected
|
||||||
|
* @param key the key used to generate the replacement text
|
||||||
|
*/
|
||||||
private void replaceSelectedText(Project project, @NotNull Editor editor, @NotNull String key) {
|
private void replaceSelectedText(Project project, @NotNull Editor editor, @NotNull String key) {
|
||||||
int selectionStart = editor.getSelectionModel().getSelectionStart();
|
int selectionStart = editor.getSelectionModel().getSelectionStart();
|
||||||
int selectionEnd = editor.getSelectionModel().getSelectionEnd();
|
int selectionEnd = editor.getSelectionModel().getSelectionEnd();
|
||||||
@ -62,6 +61,14 @@ class LocalizeItAction extends AnAction {
|
|||||||
WriteCommandAction.runWriteCommandAction(editor.getProject(), () -> documentUtil.getDocument().replaceString(selectionStart, selectionEnd, replacement));
|
WriteCommandAction.runWriteCommandAction(editor.getProject(), () -> documentUtil.getDocument().replaceString(selectionStart, selectionEnd, replacement));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds a replacement string based on the provided flavor template, key, and document util.
|
||||||
|
*
|
||||||
|
* @param flavorTemplate the flavor template string
|
||||||
|
* @param key the key used to generate the replacement text
|
||||||
|
* @param documentUtil the document util object used to determine the document type
|
||||||
|
* @return the built replacement string
|
||||||
|
*/
|
||||||
private String buildReplacement(String flavorTemplate, String key, DocumentUtil documentUtil) {
|
private String buildReplacement(String flavorTemplate, String key, DocumentUtil documentUtil) {
|
||||||
if (documentUtil.isVue() || documentUtil.isJsOrTs()) return flavorTemplate + "('" + key + "')";
|
if (documentUtil.isVue() || documentUtil.isJsOrTs()) return flavorTemplate + "('" + key + "')";
|
||||||
|
|
||||||
|
@ -39,6 +39,14 @@ public class ProjectSettingsState implements ProjectSettings {
|
|||||||
|
|
||||||
// Experimental configuration
|
// Experimental configuration
|
||||||
@Property private Boolean alwaysFold;
|
@Property private Boolean alwaysFold;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The `flavorTemplate` specifies the format used for replacing strings with their i18n (internationalization) counterparts.
|
||||||
|
* For example:
|
||||||
|
* In many situations, the default representation for i18n follows the `$i18n.t('key')` pattern. However, this can vary depending on
|
||||||
|
* the specific framework or developers' preferences for handling i18n. The ability to dynamically change this template adds flexibility and customization
|
||||||
|
* to cater to different i18n handling methods.
|
||||||
|
*/
|
||||||
@Property private String flavorTemplate;
|
@Property private String flavorTemplate;
|
||||||
|
|
||||||
public ProjectSettingsState() {
|
public ProjectSettingsState() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user