| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | import com.google.common.base.Splitter;
|
| 4 | import com.google.common.collect.Iterables;
|
| 5 | import com.google.common.collect.Maps;
|
| 6 | import java.io.IOException;
|
| 7 | import java.io.InputStream;
|
| 8 | import java.util.IllegalFormatException;
|
| 9 | import java.util.Map;
|
| 10 | import java.util.regex.Pattern;
|
| 11 | import org.apache.commons.io.Charsets;
|
| 12 | import org.apache.commons.io.IOUtils;
|
| 13 |
|
| 14 | public class StringTranslate
|
| 15 | {
|
| 16 | /**
|
| 17 | * Pattern that matches numeric variable placeholders in a resource string, such as "%d", "%3$d", "%.2f"
|
| 18 | */
|
| 19 | private static final Pattern numericVariablePattern = Pattern.compile("%(\\d+\\$)?[\\d\\.]*[df]");
|
| 20 |
|
| 21 | /**
|
| 22 | * A Splitter that splits a string on the first "=". For example, "a=b=c" would split into ["a", "b=c"].
|
| 23 | */
|
| 24 | private static final Splitter equalSignSplitter = Splitter.on('=').limit(2);
|
| 25 |
|
| 26 | /** Is the private singleton instance of StringTranslate. */
|
| 27 | private static StringTranslate instance = new StringTranslate();
|
| 28 | private final Map<String, String> languageList = Maps.<String, String>newHashMap();
|
| 29 |
|
| 30 | /**
|
| 31 | * The time, in milliseconds since epoch, that this instance was last updated
|
| 32 | */
|
| 33 | private long lastUpdateTimeInMilliseconds;
|
| 34 |
|
| 35 | public StringTranslate()
|
| 36 | {
|
| 37 | try
|
| 38 | {
|
| 39 | InputStream inputstream = StringTranslate.class.getResourceAsStream("/assets/minecraft/lang/en_US.lang");
|
| 40 |
|
| 41 | for (String s : IOUtils.readLines(inputstream, Charsets.UTF_8))
|
| 42 | {
|
| 43 | if (!s.isEmpty() && s.charAt(0) != 35)
|
| 44 | {
|
| 45 | String[] astring = (String[])Iterables.toArray(equalSignSplitter.split(s), String.class);
|
| 46 |
|
| 47 | if (astring != null && astring.length == 2)
|
| 48 | {
|
| 49 | String s1 = astring[0];
|
| 50 | String s2 = numericVariablePattern.matcher(astring[1]).replaceAll("%$1s");
|
| 51 | this.languageList.put(s1, s2);
|
| 52 | }
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | this.lastUpdateTimeInMilliseconds = System.currentTimeMillis();
|
| 57 | }
|
| 58 | catch (IOException var7)
|
| 59 | {
|
| 60 | ;
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | /**
|
| 65 | * Return the StringTranslate singleton instance
|
| 66 | */
|
| 67 | static StringTranslate getInstance()
|
| 68 | {
|
| 69 | return instance;
|
| 70 | }
|
| 71 |
|
| 72 | /**
|
| 73 | * Replaces all the current instance's translations with the ones that are passed in.
|
| 74 | */
|
| 75 | public static synchronized void replaceWith(Map<String, String> p_135063_0_)
|
| 76 | {
|
| 77 | instance.languageList.clear();
|
| 78 | instance.languageList.putAll(p_135063_0_);
|
| 79 | instance.lastUpdateTimeInMilliseconds = System.currentTimeMillis();
|
| 80 | }
|
| 81 |
|
| 82 | /**
|
| 83 | * Translate a key to current language.
|
| 84 | */
|
| 85 | public synchronized String translateKey(String key)
|
| 86 | {
|
| 87 | return this.tryTranslateKey(key);
|
| 88 | }
|
| 89 |
|
| 90 | /**
|
| 91 | * Translate a key to current language applying String.format()
|
| 92 | */
|
| 93 | public synchronized String translateKeyFormat(String key, Object... format)
|
| 94 | {
|
| 95 | String s = this.tryTranslateKey(key);
|
| 96 |
|
| 97 | try
|
| 98 | {
|
| 99 | return String.format(s, format);
|
| 100 | }
|
| 101 | catch (IllegalFormatException var5)
|
| 102 | {
|
| 103 | return "Format error: " + s;
|
| 104 | }
|
| 105 | }
|
| 106 |
|
| 107 | /**
|
| 108 | * Tries to look up a translation for the given key; spits back the key if no result was found.
|
| 109 | */
|
| 110 | private String tryTranslateKey(String key)
|
| 111 | {
|
| 112 | String s = (String)this.languageList.get(key);
|
| 113 | return s == null ? key : s;
|
| 114 | }
|
| 115 |
|
| 116 | /**
|
| 117 | * Returns true if the passed key is in the translation table.
|
| 118 | */
|
| 119 | public synchronized boolean isKeyTranslated(String key)
|
| 120 | {
|
| 121 | return this.languageList.containsKey(key);
|
| 122 | }
|
| 123 |
|
| 124 | /**
|
| 125 | * Gets the time, in milliseconds since epoch, that this instance was last updated
|
| 126 | */
|
| 127 | public long getLastUpdateTimeInMilliseconds()
|
| 128 | {
|
| 129 | return this.lastUpdateTimeInMilliseconds;
|
| 130 | }
|
| 131 | }
|
| 132 |
|