mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-19 20:28:00 +00:00
Moved CardTranslation to forge-core. We need it to translate card info in forge-game.
This commit is contained in:
88
forge-core/src/main/java/forge/util/CardTranslation.java
Normal file
88
forge-core/src/main/java/forge/util/CardTranslation.java
Normal file
@@ -0,0 +1,88 @@
|
||||
package forge.util;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class CardTranslation {
|
||||
|
||||
private static Map <String, String> translatednames;
|
||||
private static Map <String, String> translatedtypes;
|
||||
private static Map <String, String> translatedoracles;
|
||||
private static String languageSelected = "en-US";
|
||||
|
||||
private static void readTranslationFile(String language, String languagesDirectory) {
|
||||
String filename = "cardnames-" + language + ".txt";
|
||||
|
||||
try (LineReader translationFile = new LineReader(new FileInputStream(languagesDirectory + filename), Charsets.UTF_8)) {
|
||||
for (String line : translationFile.readLines()) {
|
||||
String[] matches = line.split("\\|");
|
||||
if (matches.length >= 2) {
|
||||
translatednames.put(matches[0], matches[1]);
|
||||
}
|
||||
if (matches.length >= 3) {
|
||||
translatedtypes.put(matches[0], matches[2]);
|
||||
}
|
||||
if (matches.length >= 4) {
|
||||
translatedoracles.put(matches[0], matches[3].replace("\\n", "\n\n"));
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error reading translation file: cardnames-" + language + ".txt");
|
||||
}
|
||||
}
|
||||
|
||||
public static String getTranslatedName(String name) {
|
||||
if (needsTranslation()) {
|
||||
String tname = translatednames.get(name);
|
||||
return tname == null ? name : tname;
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
public static String getTranslatedType(String name, String originaltype) {
|
||||
if (needsTranslation()) {
|
||||
String ttype = translatedtypes.get(name);
|
||||
return ttype == null ? originaltype : ttype;
|
||||
}
|
||||
|
||||
return originaltype;
|
||||
}
|
||||
|
||||
public static String getTranslatedOracle(String name) {
|
||||
if (needsTranslation()) {
|
||||
String toracle = translatedoracles.get(name);
|
||||
return toracle == null ? "" : toracle;
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
public static HashMap<String, String> getTranslationTexts(String cardname, String altcardname) {
|
||||
HashMap<String, String> translations = new HashMap<>();
|
||||
translations.put("name", getTranslatedName(cardname));
|
||||
translations.put("oracle", getTranslatedOracle(cardname));
|
||||
translations.put("altname", getTranslatedName(altcardname));
|
||||
translations.put("altoracle", getTranslatedOracle(altcardname));
|
||||
return translations;
|
||||
}
|
||||
|
||||
private static boolean needsTranslation() {
|
||||
return !languageSelected.equals("en-US");
|
||||
}
|
||||
|
||||
public static void preloadTranslation(String language, String languagesDirectory) {
|
||||
languageSelected = language;
|
||||
|
||||
if (needsTranslation()) {
|
||||
translatednames = new HashMap<>();
|
||||
translatedtypes = new HashMap<>();
|
||||
translatedoracles = new HashMap<>();
|
||||
readTranslationFile(languageSelected, languagesDirectory);
|
||||
}
|
||||
}
|
||||
}
|
||||
170
forge-core/src/main/java/forge/util/LineReader.java
Normal file
170
forge-core/src/main/java/forge/util/LineReader.java
Normal file
@@ -0,0 +1,170 @@
|
||||
/*
|
||||
* Forge: Play Magic: the Gathering.
|
||||
* Copyright (C) 2011 Forge Team
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package forge.util;
|
||||
|
||||
/**
|
||||
* TODO: Write javadoc for this type.
|
||||
*
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/**
|
||||
* Represents the lines found in an {@link InputStream}. The lines are read one
|
||||
* at a time using {@link BufferedReader#readLine()} and may be streamed through
|
||||
* an iterator or returned all at once.
|
||||
*
|
||||
* <p>
|
||||
* This class does not handle any concurrency issues.
|
||||
*
|
||||
* <p>
|
||||
* The stream is closed automatically when the for loop is done :)
|
||||
*
|
||||
* <pre>
|
||||
* {@code
|
||||
* for(String line : new LineReader(stream))
|
||||
* // ...
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* <p>
|
||||
* An {@link IllegalStateException} will be thrown if any {@link IOException}s
|
||||
* occur when reading or closing the stream.
|
||||
*
|
||||
* @author Torleif Berger
|
||||
* http://creativecommons.org/licenses/by/3.0/
|
||||
* @see http://www.geekality.net/?p=1614
|
||||
*/
|
||||
public class LineReader implements Iterable<String>, Closeable {
|
||||
private final BufferedReader reader;
|
||||
|
||||
/**
|
||||
* Instantiates a new line reader.
|
||||
*
|
||||
* @param stream the stream
|
||||
*/
|
||||
public LineReader(final InputStream stream) {
|
||||
this(stream, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new line reader.
|
||||
*
|
||||
* @param stream the stream
|
||||
* @param charset the charset
|
||||
*/
|
||||
public LineReader(final InputStream stream, final Charset charset) {
|
||||
this.reader = new BufferedReader(new InputStreamReader(stream, charset));
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the underlying stream.
|
||||
*
|
||||
* @throws IOException Signals that an I/O exception has occurred.
|
||||
*/
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
this.reader.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes sure the underlying stream is closed.
|
||||
*
|
||||
* @throws Throwable the throwable
|
||||
*/
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
this.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an iterator over the lines remaining to be read.
|
||||
*
|
||||
* <p>
|
||||
* The underlying stream is closed automatically once
|
||||
*
|
||||
* @return This iterator.
|
||||
* {@link Iterator#hasNext()} returns false. This means that the stream
|
||||
* should be closed after using a for loop.
|
||||
*/
|
||||
@Override
|
||||
public Iterator<String> iterator() {
|
||||
return new LineIterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all lines remaining to be read and closes the stream.
|
||||
*
|
||||
* @return The lines read from the stream.
|
||||
*/
|
||||
public Collection<String> readLines() {
|
||||
final Collection<String> lines = new ArrayList<>();
|
||||
for (final String line : this) {
|
||||
lines.add(line);
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
private class LineIterator implements Iterator<String> {
|
||||
private String nextLine;
|
||||
|
||||
public String bufferNext() {
|
||||
try {
|
||||
return this.nextLine = LineReader.this.reader.readLine();
|
||||
} catch (final IOException e) {
|
||||
throw new IllegalStateException("I/O error while reading stream.", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
final boolean hasNext = (this.nextLine != null) || (this.bufferNext() != null);
|
||||
|
||||
if (!hasNext) {
|
||||
try {
|
||||
LineReader.this.reader.close();
|
||||
} catch (final IOException e) {
|
||||
throw new IllegalStateException("I/O error when closing stream.", e);
|
||||
}
|
||||
}
|
||||
|
||||
return hasNext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String next() {
|
||||
if (!this.hasNext()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
final String result = this.nextLine;
|
||||
this.nextLine = null;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user