
nl.rrd.wool.i18n.WoolTranslationParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of wool-core Show documentation
Show all versions of wool-core Show documentation
WOOL is a simple, powerful dialogue framework for creating virtual agent conversations.
The newest version!
/*
* Copyright 2019 Roessingh Research and Development.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package nl.rrd.wool.i18n;
import com.fasterxml.jackson.core.type.TypeReference;
import nl.rrd.wool.exception.LineNumberParseException;
import nl.rrd.wool.exception.ParseException;
import nl.rrd.wool.json.JsonMapper;
import nl.rrd.wool.model.WoolNodeBody;
import nl.rrd.wool.parser.WoolBodyParser;
import nl.rrd.wool.parser.WoolBodyToken;
import nl.rrd.wool.parser.WoolBodyTokenizer;
import nl.rrd.wool.utils.FileUtils;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* This class can parse a WOOL translation file. The file should contain a
* JSON object with key-value pairs as exported by POEditor. There are two types
* of key-value pairs:
*
*
* - Key and value are strings. The key is a translatable in one language,
* and the value is a translatable in another language. A translatable should be
* the string representation of a {@link WoolTranslatable WoolTranslatable}.
* That is a text that may include variables and <<input>>
* commands.
* - The key is a string the value is a JSON object. In this case the key is
* a context string, and the value contains translatable key-value pairs.
*
*
* This parser ignores context strings and returns a flat map of
* translatables. This means that it does not support different translations of
* the same string with different contexts.
*
* @author Dennis Hofs (RRD)
*/
public class WoolTranslationParser {
public static WoolTranslationParserResult parse(URL url)
throws IOException {
try (InputStream input = url.openStream()) {
return parse(input);
}
}
public static WoolTranslationParserResult parse(File file)
throws IOException {
try (InputStream input = new FileInputStream(file)) {
return parse(input);
}
}
public static WoolTranslationParserResult parse(InputStream input)
throws IOException{
return parse(new InputStreamReader(input, StandardCharsets.UTF_8));
}
public static WoolTranslationParserResult parse(Reader reader)
throws IOException {
WoolTranslationParserResult result = new WoolTranslationParserResult();
Map translations =
new LinkedHashMap<>();
String json = FileUtils.readFileString(reader);
if (json.trim().isEmpty()) {
result.getWarnings().add("Empty translation file");
result.setTranslations(translations);
return result;
}
Map map;
try {
map = JsonMapper.parse(json, new TypeReference
© 2015 - 2025 Weber Informatics LLC | Privacy Policy