Add i18n flavor template setting

A new setting, `flavorTemplate` is introduced to specify how strings are replaced with i18n representation. This includes code modifications for handling this new feature and also updates in the messages.properties file for the associated UI text. The feature is especially useful for customizing how localization strings are generated in different project setups.
This commit is contained in:
JPilson 2023-12-16 17:50:29 +01:00
parent 352dc7c47c
commit a709db810e
7 changed files with 42 additions and 3 deletions

View File

@ -33,4 +33,5 @@ public interface ProjectSettings {
// Experimental Configuration
boolean isAlwaysFold();
String getFlavorTemplate();
}

View File

@ -26,6 +26,7 @@ import java.util.ResourceBundle;
/**
* Configuration panel with all possible options for this plugin.
*
* @author marhali
*/
public class ProjectSettingsComponent extends ProjectSettingsComponentState {
@ -64,10 +65,12 @@ public class ProjectSettingsComponent extends ProjectSettingsComponentState {
.addVerticalGap(24)
.addComponent(new TitledSeparator(bundle.getString("settings.experimental.title")))
.addComponent(constructAlwaysFoldField())
.addLabeledComponent(bundle.getString("settings.experimental.flavor-template"), constructFlavorTemplate(), 1, false)
.addComponentFillVertically(new JPanel(), 0)
.getPanel();
}
private JComponent constructPresetField() {
preset = new ComboBox<>(Preset.values());
preset.setToolTipText(bundle.getString("settings.preset.tooltip"));
@ -219,6 +222,12 @@ public class ProjectSettingsComponent extends ProjectSettingsComponentState {
return alwaysFold;
}
private JComponent constructFlavorTemplate() {
flavorTemplate = new ExtendableTextField(20);
flavorTemplate.setToolTipText(bundle.getString("settings.experimental.flavor-template-tooltip"));
return flavorTemplate;
}
private ItemListener handleParserChange() {
return e -> {
if (e.getStateChange() == ItemEvent.SELECTED) {

View File

@ -40,6 +40,8 @@ public class ProjectSettingsComponentState {
// Experimental configuration
protected JCheckBox alwaysFold;
protected JTextField flavorTemplate;
protected ProjectSettingsState getState() {
// Every field needs to provide its state
ProjectSettingsState state = new ProjectSettingsState();
@ -63,6 +65,7 @@ public class ProjectSettingsComponentState {
state.setAssistance(assistance.isSelected());
state.setAlwaysFold(alwaysFold.isSelected());
state.setFlavorTemplate(flavorTemplate.getText());
return state;
}
@ -88,5 +91,7 @@ public class ProjectSettingsComponentState {
assistance.setSelected(state.isAssistance());
alwaysFold.setSelected(state.isAlwaysFold());
flavorTemplate.setText(state.getFlavorTemplate());
}
}

View File

@ -39,6 +39,7 @@ public class ProjectSettingsState implements ProjectSettings {
// Experimental configuration
@Property private Boolean alwaysFold;
@Property private String flavorTemplate;
public ProjectSettingsState() {
this(new DefaultPreset());
@ -65,6 +66,7 @@ public class ProjectSettingsState implements ProjectSettings {
this.assistance = defaults.isAssistance();
this.alwaysFold = defaults.isAlwaysFold();
this.flavorTemplate = defaults.getFlavorTemplate();
}
@Override
@ -143,6 +145,11 @@ public class ProjectSettingsState implements ProjectSettings {
return alwaysFold;
}
@Override
public String getFlavorTemplate() {
return this.flavorTemplate;
}
public void setLocalesDirectory(String localesDirectory) {
this.localesDirectory = localesDirectory;
}
@ -202,6 +209,9 @@ public class ProjectSettingsState implements ProjectSettings {
public void setAlwaysFold(Boolean alwaysFold) {
this.alwaysFold = alwaysFold;
}
public void setFlavorTemplate(String flavorTemplate){
this.flavorTemplate = flavorTemplate;
}
@Override
public boolean equals(Object o) {
@ -222,7 +232,8 @@ public class ProjectSettingsState implements ProjectSettings {
&& Objects.equals(previewLocale, that.previewLocale)
&& Objects.equals(nestedKeys, that.nestedKeys)
&& Objects.equals(assistance, that.assistance)
&& Objects.equals(alwaysFold, that.alwaysFold);
&& Objects.equals(alwaysFold, that.alwaysFold)
&& Objects.equals(flavorTemplate,that.flavorTemplate);
}
@Override
@ -230,7 +241,7 @@ public class ProjectSettingsState implements ProjectSettings {
return Objects.hash(
localesDirectory, folderStrategy, parserStrategy, filePattern, includeSubDirs,
sorting, namespaceDelimiter, sectionDelimiter, contextDelimiter, pluralDelimiter,
defaultNamespace, previewLocale, nestedKeys, assistance, alwaysFold
defaultNamespace, previewLocale, nestedKeys, assistance, alwaysFold,flavorTemplate
);
}
@ -252,6 +263,7 @@ public class ProjectSettingsState implements ProjectSettings {
", nestedKeys=" + nestedKeys +
", assistance=" + assistance +
", alwaysFold=" + alwaysFold +
", flavorTemplate=" + flavorTemplate +
'}';
}
}

View File

@ -86,4 +86,9 @@ public class ReactI18NextPreset implements ProjectSettings {
public boolean isAlwaysFold() {
return false;
}
@Override
public String getFlavorTemplate() {
return "$i18n.t";
}
}

View File

@ -85,4 +85,9 @@ public class VueI18nPreset implements ProjectSettings {
public boolean isAlwaysFold() {
return false;
}
@Override
public String getFlavorTemplate() {
return "$i18n.t";
}
}

View File

@ -60,6 +60,8 @@ settings.editor.assistance.tooltip=Activates editor support to reference, auto-c
settings.experimental.title=Experimental Configuration
settings.experimental.always-fold.title=Always fold translation keys
settings.experimental.always-fold.tooltip=Forces the editor to always display the value behind a translation key. The value cannot be unfolded when this function is active.
settings.experimental.flavor-template =I18n flavor template
settings.experimental.flavor-template-tooltip = Specify How to replace strings with i18n representation.
error.io=An error occurred while processing translation files. \n\
Config: {0} => {1} ({2}) \n\
Path: {3} \n\