| 1 | package net.minecraft.client.resources;
|
| 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.List;
|
| 10 | import java.util.Map;
|
| 11 | import java.util.regex.Pattern;
|
| 12 | import net.minecraft.util.ResourceLocation;
|
| 13 | import org.apache.commons.io.Charsets;
|
| 14 | import org.apache.commons.io.IOUtils;
|
| 15 |
|
| 16 | public class Locale
|
| 17 | {
|
| 18 | /** Splits on "=" */
|
| 19 | private static final Splitter splitter = Splitter.on('=').limit(2);
|
| 20 | private static final Pattern pattern = Pattern.compile("%(\\d+\\$)?[\\d\\.]*[df]");
|
| 21 | Map<String, String> properties = Maps.<String, String>newHashMap();
|
| 22 | private boolean unicode;
|
| 23 |
|
| 24 | /**
|
| 25 | * For each domain $D and language $L, attempts to load the resource $D:lang/$L.lang
|
| 26 | */
|
| 27 | public synchronized void loadLocaleDataFiles(IResourceManager resourceManager, List<String> languageList)
|
| 28 | {
|
| 29 | this.properties.clear();
|
| 30 |
|
| 31 | for (String s : languageList)
|
| 32 | {
|
| 33 | String s1 = String.format("lang/%s.lang", new Object[] {s});
|
| 34 |
|
| 35 | for (String s2 : resourceManager.getResourceDomains())
|
| 36 | {
|
| 37 | try
|
| 38 | {
|
| 39 | this.loadLocaleData(resourceManager.getAllResources(new ResourceLocation(s2, s1)));
|
| 40 | }
|
| 41 | catch (IOException var9)
|
| 42 | {
|
| 43 | ;
|
| 44 | }
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | this.checkUnicode();
|
| 49 | }
|
| 50 |
|
| 51 | public boolean isUnicode()
|
| 52 | {
|
| 53 | return this.unicode;
|
| 54 | }
|
| 55 |
|
| 56 | private void checkUnicode()
|
| 57 | {
|
| 58 | this.unicode = false;
|
| 59 | int i = 0;
|
| 60 | int j = 0;
|
| 61 |
|
| 62 | for (String s : this.properties.values())
|
| 63 | {
|
| 64 | int k = s.length();
|
| 65 | j += k;
|
| 66 |
|
| 67 | for (int l = 0; l < k; ++l)
|
| 68 | {
|
| 69 | if (s.charAt(l) >= 256)
|
| 70 | {
|
| 71 | ++i;
|
| 72 | }
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | float f = (float)i / (float)j;
|
| 77 | this.unicode = (double)f > 0.1D;
|
| 78 | }
|
| 79 |
|
| 80 | /**
|
| 81 | * Loads the locale data for the list of resources.
|
| 82 | */
|
| 83 | private void loadLocaleData(List<IResource> resourcesList) throws IOException
|
| 84 | {
|
| 85 | for (IResource iresource : resourcesList)
|
| 86 | {
|
| 87 | InputStream inputstream = iresource.getInputStream();
|
| 88 |
|
| 89 | try
|
| 90 | {
|
| 91 | this.loadLocaleData(inputstream);
|
| 92 | }
|
| 93 | finally
|
| 94 | {
|
| 95 | IOUtils.closeQuietly(inputstream);
|
| 96 | }
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | private void loadLocaleData(InputStream inputStreamIn) throws IOException
|
| 101 | {
|
| 102 | for (String s : IOUtils.readLines(inputStreamIn, Charsets.UTF_8))
|
| 103 | {
|
| 104 | if (!s.isEmpty() && s.charAt(0) != 35)
|
| 105 | {
|
| 106 | String[] astring = (String[])Iterables.toArray(splitter.split(s), String.class);
|
| 107 |
|
| 108 | if (astring != null && astring.length == 2)
|
| 109 | {
|
| 110 | String s1 = astring[0];
|
| 111 | String s2 = pattern.matcher(astring[1]).replaceAll("%$1s");
|
| 112 | this.properties.put(s1, s2);
|
| 113 | }
|
| 114 | }
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | /**
|
| 119 | * Returns the translation, or the key itself if the key could not be translated.
|
| 120 | */
|
| 121 | private String translateKeyPrivate(String translateKey)
|
| 122 | {
|
| 123 | String s = (String)this.properties.get(translateKey);
|
| 124 | return s == null ? translateKey : s;
|
| 125 | }
|
| 126 |
|
| 127 | /**
|
| 128 | * Calls String.format(translateKey(key), params)
|
| 129 | */
|
| 130 | public String formatMessage(String translateKey, Object[] parameters)
|
| 131 | {
|
| 132 | String s = this.translateKeyPrivate(translateKey);
|
| 133 |
|
| 134 | try
|
| 135 | {
|
| 136 | return String.format(s, parameters);
|
| 137 | }
|
| 138 | catch (IllegalFormatException var5)
|
| 139 | {
|
| 140 | return "Format error: " + s;
|
| 141 | }
|
| 142 | }
|
| 143 | }
|
| 144 |
|