reformat translation files based on IDE preferences
Resolves #123 Resolves #128
This commit is contained in:
parent
090b7a0216
commit
26af16d0c8
@ -8,6 +8,7 @@
|
|||||||
- Indicate translations with duplicated values yellow
|
- Indicate translations with duplicated values yellow
|
||||||
- Multiple translation filters can be used together
|
- Multiple translation filters can be used together
|
||||||
- Option to consider subdirectories for modularized translation files
|
- Option to consider subdirectories for modularized translation files
|
||||||
|
- Reformat translation files based on IDE preferences
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
- Reengineered how translation filters are applied internally
|
- Reengineered how translation filters are applied internally
|
||||||
|
@ -54,7 +54,7 @@ public class DataStore {
|
|||||||
|
|
||||||
ApplicationManager.getApplication().runReadAction(() -> {
|
ApplicationManager.getApplication().runReadAction(() -> {
|
||||||
try {
|
try {
|
||||||
this.data = new IOHandler(settings).read();
|
this.data = new IOHandler(project, settings).read();
|
||||||
this.changeListener.updateLocalesPath(settings.getLocalesDirectory());
|
this.changeListener.updateLocalesPath(settings.getLocalesDirectory());
|
||||||
successResult.accept(true);
|
successResult.accept(true);
|
||||||
|
|
||||||
@ -80,7 +80,7 @@ public class DataStore {
|
|||||||
|
|
||||||
ApplicationManager.getApplication().runWriteAction(() -> {
|
ApplicationManager.getApplication().runWriteAction(() -> {
|
||||||
try {
|
try {
|
||||||
new IOHandler(settings).write(this.data);
|
new IOHandler(project, settings).write(this.data);
|
||||||
successResult.accept(true);
|
successResult.accept(true);
|
||||||
|
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
|
@ -1,8 +1,14 @@
|
|||||||
package de.marhali.easyi18n.io;
|
package de.marhali.easyi18n.io;
|
||||||
|
|
||||||
|
import com.intellij.codeInsight.actions.ReformatCodeProcessor;
|
||||||
|
import com.intellij.openapi.editor.Document;
|
||||||
|
import com.intellij.openapi.fileEditor.FileDocumentManager;
|
||||||
|
import com.intellij.openapi.project.Project;
|
||||||
import com.intellij.openapi.vfs.LocalFileSystem;
|
import com.intellij.openapi.vfs.LocalFileSystem;
|
||||||
import com.intellij.openapi.vfs.VirtualFile;
|
import com.intellij.openapi.vfs.VirtualFile;
|
||||||
|
|
||||||
|
import com.intellij.psi.PsiDocumentManager;
|
||||||
|
import com.intellij.psi.PsiFile;
|
||||||
import de.marhali.easyi18n.exception.EmptyLocalesDirException;
|
import de.marhali.easyi18n.exception.EmptyLocalesDirException;
|
||||||
import de.marhali.easyi18n.io.folder.FolderStrategy;
|
import de.marhali.easyi18n.io.folder.FolderStrategy;
|
||||||
import de.marhali.easyi18n.io.parser.ParserStrategy;
|
import de.marhali.easyi18n.io.parser.ParserStrategy;
|
||||||
@ -22,6 +28,7 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class IOHandler {
|
public class IOHandler {
|
||||||
|
|
||||||
|
private final @NotNull Project project;
|
||||||
private final @NotNull ProjectSettings settings;
|
private final @NotNull ProjectSettings settings;
|
||||||
|
|
||||||
private final @NotNull FolderStrategy folderStrategy;
|
private final @NotNull FolderStrategy folderStrategy;
|
||||||
@ -29,8 +36,8 @@ public class IOHandler {
|
|||||||
private final @NotNull ParserStrategyType parserStrategyType;
|
private final @NotNull ParserStrategyType parserStrategyType;
|
||||||
private final @NotNull ParserStrategy parserStrategy;
|
private final @NotNull ParserStrategy parserStrategy;
|
||||||
|
|
||||||
public IOHandler(@NotNull ProjectSettings settings) throws Exception {
|
public IOHandler(@NotNull Project project, @NotNull ProjectSettings settings) throws Exception {
|
||||||
|
this.project = project;
|
||||||
this.settings = settings;
|
this.settings = settings;
|
||||||
|
|
||||||
this.folderStrategy = settings.getFolderStrategy().getStrategy()
|
this.folderStrategy = settings.getFolderStrategy().getStrategy()
|
||||||
@ -76,7 +83,7 @@ public class IOHandler {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes the provided translation data to the local project files <br>
|
* Writes the provided translation data to the local project files <br>
|
||||||
* <b>Note:</b> This method must be called from an Write-Action-Context (see ApplicationManager)
|
* <b>Note:</b> This method must be called from a Write-Action-Context (see ApplicationManager)
|
||||||
* @param data Cached translation data to save
|
* @param data Cached translation data to save
|
||||||
* @throws IOException Write action failed
|
* @throws IOException Write action failed
|
||||||
*/
|
*/
|
||||||
@ -92,7 +99,25 @@ public class IOHandler {
|
|||||||
|
|
||||||
for(TranslationFile file : translationFiles) {
|
for(TranslationFile file : translationFiles) {
|
||||||
try {
|
try {
|
||||||
this.parserStrategy.write(data, file);
|
String content = this.parserStrategy.write(data, file);
|
||||||
|
|
||||||
|
if(content == null) {
|
||||||
|
// We should consider deleting the target translation file if it has no content
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Document document = FileDocumentManager.getInstance().getDocument(file.getVirtualFile());
|
||||||
|
|
||||||
|
assert document != null;
|
||||||
|
document.setText(content);
|
||||||
|
|
||||||
|
PsiFile psi = PsiDocumentManager.getInstance(project).getCachedPsiFile(document);
|
||||||
|
assert psi != null;
|
||||||
|
|
||||||
|
new ReformatCodeProcessor(psi, false).run();
|
||||||
|
|
||||||
|
FileDocumentManager.getInstance().saveDocument(document);
|
||||||
|
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
throw new IOException(file + "\n\n" + ex.getMessage(), ex);
|
throw new IOException(file + "\n\n" + ex.getMessage(), ex);
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ public class ModularNamespaceFolderStrategy extends FolderStrategy {
|
|||||||
|
|
||||||
for (VirtualFile localeFile : dir.getChildren()) {
|
for (VirtualFile localeFile : dir.getChildren()) {
|
||||||
if(localeFile.isDirectory()) {
|
if(localeFile.isDirectory()) {
|
||||||
if(settings.isIncludeSubDirs()) {
|
if(ns.isEmpty() || settings.isIncludeSubDirs()) {
|
||||||
files.addAll(findLocaleFiles(new KeyPath(ns, localeFile.getName()), localeFile));
|
files.addAll(findLocaleFiles(new KeyPath(ns, localeFile.getName()), localeFile));
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
@ -67,7 +67,8 @@ public class ModularNamespaceFolderStrategy extends FolderStrategy {
|
|||||||
for (Map.Entry<String, TranslationNode> entry : node.getChildren().entrySet()) {
|
for (Map.Entry<String, TranslationNode> entry : node.getChildren().entrySet()) {
|
||||||
String parentPath = localesPath + "/" + String.join("/", path);
|
String parentPath = localesPath + "/" + String.join("/", path);
|
||||||
|
|
||||||
if(super.exists(parentPath, entry.getKey())) { // Is directory - includeSubDirs
|
// Root-Node or is directory(includeSubDirs)
|
||||||
|
if(path.isEmpty() || super.exists(parentPath, entry.getKey())) {
|
||||||
files.addAll(createLocaleFiles(localesPath, locales, new KeyPath(path, entry.getKey()), type, entry.getValue()));
|
files.addAll(createLocaleFiles(localesPath, locales, new KeyPath(path, entry.getKey()), type, entry.getValue()));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
package de.marhali.easyi18n.io.parser;
|
package de.marhali.easyi18n.io.parser;
|
||||||
|
|
||||||
import de.marhali.easyi18n.model.*;
|
import de.marhali.easyi18n.model.*;
|
||||||
|
|
||||||
import de.marhali.easyi18n.model.KeyPath;
|
|
||||||
import de.marhali.easyi18n.settings.ProjectSettings;
|
import de.marhali.easyi18n.settings.ProjectSettings;
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
@ -28,11 +28,13 @@ public abstract class ParserStrategy {
|
|||||||
public abstract void read(@NotNull TranslationFile file, @NotNull TranslationData data) throws Exception;
|
public abstract void read(@NotNull TranslationFile file, @NotNull TranslationData data) throws Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes the relevant data to the translation file (consider namespace and locale)
|
* Constructs the relevant data to represents the specified translation file. (consider namespace and locale)
|
||||||
* @param data Translation data cache
|
* @param data Translation data cache
|
||||||
* @param file Target translation file
|
* @param file Target translation file
|
||||||
|
* @return String representing target translation file.
|
||||||
|
* Can be null to indicate that the file is not necessary and could be deleted
|
||||||
*/
|
*/
|
||||||
public abstract void write(@NotNull TranslationData data, @NotNull TranslationFile file) throws Exception;
|
public abstract @Nullable String write(@NotNull TranslationData data, @NotNull TranslationFile file) throws Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determines translation node to use for parsing
|
* Determines translation node to use for parsing
|
||||||
|
@ -44,13 +44,12 @@ public class JsonParserStrategy extends ParserStrategy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(@NotNull TranslationData data, @NotNull TranslationFile file) throws Exception {
|
public @NotNull String write(@NotNull TranslationData data, @NotNull TranslationFile file) throws Exception {
|
||||||
TranslationNode targetNode = super.getTargetNode(data, file);
|
TranslationNode targetNode = super.getTargetNode(data, file);
|
||||||
|
|
||||||
JsonObject output = new JsonObject();
|
JsonObject output = new JsonObject();
|
||||||
JsonMapper.write(file.getLocale(), output, Objects.requireNonNull(targetNode));
|
JsonMapper.write(file.getLocale(), output, Objects.requireNonNull(targetNode));
|
||||||
|
|
||||||
VirtualFile vf = file.getVirtualFile();
|
return GSON.toJson(output);
|
||||||
vf.setBinaryContent(GSON.toJson(output).getBytes(vf.getCharset()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,13 +46,12 @@ public class Json5ParserStrategy extends ParserStrategy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(@NotNull TranslationData data, @NotNull TranslationFile file) throws Exception {
|
public @NotNull String write(@NotNull TranslationData data, @NotNull TranslationFile file) throws Exception {
|
||||||
TranslationNode targetNode = super.getTargetNode(data, file);
|
TranslationNode targetNode = super.getTargetNode(data, file);
|
||||||
|
|
||||||
Json5Object output = new Json5Object();
|
Json5Object output = new Json5Object();
|
||||||
Json5Mapper.write(file.getLocale(), output, Objects.requireNonNull(targetNode));
|
Json5Mapper.write(file.getLocale(), output, Objects.requireNonNull(targetNode));
|
||||||
|
|
||||||
VirtualFile vf = file.getVirtualFile();
|
return JSON5.serialize(output);
|
||||||
vf.setBinaryContent(JSON5.serialize(output).getBytes(vf.getCharset()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ public class PropertiesParserStrategy extends ParserStrategy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(@NotNull TranslationData data, @NotNull TranslationFile file) throws Exception {
|
public @NotNull String write(@NotNull TranslationData data, @NotNull TranslationFile file) throws Exception {
|
||||||
TranslationNode targetNode = super.getTargetNode(data, file);
|
TranslationNode targetNode = super.getTargetNode(data, file);
|
||||||
TranslationData targetData = new TranslationData(data.getLocales(), targetNode);
|
TranslationData targetData = new TranslationData(data.getLocales(), targetNode);
|
||||||
|
|
||||||
@ -53,9 +53,7 @@ public class PropertiesParserStrategy extends ParserStrategy {
|
|||||||
|
|
||||||
try(StringWriter writer = new StringWriter()) {
|
try(StringWriter writer = new StringWriter()) {
|
||||||
output.store(writer, null);
|
output.store(writer, null);
|
||||||
|
return writer.toString();
|
||||||
VirtualFile vf = file.getVirtualFile();
|
|
||||||
vf.setBinaryContent(writer.toString().getBytes(vf.getCharset()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,13 +40,11 @@ public class YamlParserStrategy extends ParserStrategy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(@NotNull TranslationData data, @NotNull TranslationFile file) throws Exception {
|
public @NotNull String write(@NotNull TranslationData data, @NotNull TranslationFile file) throws Exception {
|
||||||
TranslationNode targetNode = super.getTargetNode(data, file);
|
TranslationNode targetNode = super.getTargetNode(data, file);
|
||||||
|
|
||||||
Section output = new MapSection();
|
Section output = new MapSection();
|
||||||
YamlMapper.write(file.getLocale(), output, targetNode);
|
YamlMapper.write(file.getLocale(), output, targetNode);
|
||||||
|
return Section.toString(output);
|
||||||
VirtualFile vf = file.getVirtualFile();
|
|
||||||
vf.setBinaryContent(Section.toString(output).getBytes(vf.getCharset()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user