implement json5 parser strategy

This commit is contained in:
marhali 2022-02-22 21:46:32 +01:00
parent 85073f7f5b
commit 27931daca6
3 changed files with 61 additions and 1 deletions

View File

@ -1,6 +1,7 @@
package de.marhali.easyi18n.io.parser;
import de.marhali.easyi18n.io.parser.json.JsonParserStrategy;
import de.marhali.easyi18n.io.parser.json5.Json5ParserStrategy;
import de.marhali.easyi18n.io.parser.properties.PropertiesParserStrategy;
import de.marhali.easyi18n.io.parser.yaml.YamlParserStrategy;
@ -10,6 +11,7 @@ import de.marhali.easyi18n.io.parser.yaml.YamlParserStrategy;
*/
public enum ParserStrategyType {
JSON(JsonParserStrategy.class),
JSON5(Json5ParserStrategy.class),
YAML(YamlParserStrategy.class),
YML(YamlParserStrategy.class),
PROPERTIES(PropertiesParserStrategy.class),

View File

@ -0,0 +1,58 @@
package de.marhali.easyi18n.io.parser.json5;
import com.intellij.openapi.vfs.VirtualFile;
import de.marhali.easyi18n.io.parser.ParserStrategy;
import de.marhali.easyi18n.model.SettingsState;
import de.marhali.easyi18n.model.TranslationData;
import de.marhali.easyi18n.model.TranslationFile;
import de.marhali.easyi18n.model.TranslationNode;
import de.marhali.json5.Json5;
import de.marhali.json5.Json5Element;
import de.marhali.json5.Json5Object;
import org.jetbrains.annotations.NotNull;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Objects;
/**
* Json5 file format parser strategy
* @author marhali
*/
public class Json5ParserStrategy extends ParserStrategy {
private static final Json5 JSON5 = Json5.builder(builder ->
builder.allowInvalidSurrogate().trailingComma().indentFactor(4).build());
public Json5ParserStrategy(@NotNull SettingsState settings) {
super(settings);
}
@Override
public void read(@NotNull TranslationFile file, @NotNull TranslationData data) throws Exception {
data.addLocale(file.getLocale());
VirtualFile vf = file.getVirtualFile();
TranslationNode targetNode = super.getOrCreateTargetNode(file, data);
try (Reader reader = new InputStreamReader(vf.getInputStream(), vf.getCharset())) {
Json5Element input = JSON5.parse(reader);
if(input != null && input.isJsonObject()) {
Json5Mapper.read(file.getLocale(), input.getAsJsonObject(), targetNode);
}
}
}
@Override
public void write(@NotNull TranslationData data, @NotNull TranslationFile file) throws Exception {
TranslationNode targetNode = super.getTargetNode(data, file);
Json5Object output = new Json5Object();
Json5Mapper.write(file.getLocale(), output, Objects.requireNonNull(targetNode));
VirtualFile vf = file.getVirtualFile();
vf.setBinaryContent(JSON5.serialize(output).getBytes(vf.getCharset()));
}
}

View File

@ -17,7 +17,7 @@ settings.path.text=Locales directory
settings.strategy.title=Translation file structure
settings.strategy.folder=Single Directory;Modularized: Locale / Namespace;Modularized: Namespace / Locale
settings.strategy.folder.tooltip=What is the folder structure of your translation files?
settings.strategy.parser=JSON;YAML;YML;Properties;ARB
settings.strategy.parser=JSON;JSON5;YAML;YML;Properties;ARB
settings.strategy.parser.tooltip=Which file parser should be used to process your translation files?
settings.strategy.file-pattern.tooltip=Defines a wildcard matcher to filter relevant translation files. For example *.json, *.???.
settings.preview=Preview locale