|
| 1 | +package org.javawebstack.framework.util; |
| 2 | + |
| 3 | +import org.javawebstack.graph.GraphArray; |
| 4 | +import org.javawebstack.graph.GraphObject; |
| 5 | + |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.Locale; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +public class I18N { |
| 11 | + |
| 12 | + private Locale defaultLocale = Locale.ENGLISH; |
| 13 | + private final Map<Locale, Map<String, I18NString>> translations = new HashMap<>(); |
| 14 | + |
| 15 | + public void setDefaultLocale(Locale defaultLocale) { |
| 16 | + this.defaultLocale = defaultLocale; |
| 17 | + } |
| 18 | + |
| 19 | + public Locale getDefaultLocale() { |
| 20 | + return defaultLocale; |
| 21 | + } |
| 22 | + |
| 23 | + public void set(Locale locale, String key, Map<String, Object> fields){ |
| 24 | + if(!translations.containsKey(locale)) |
| 25 | + translations.put(locale, new HashMap<>()); |
| 26 | + Map<String, I18NString> strings = translations.get(locale); |
| 27 | + strings.put(key, new I18NString(fields)); |
| 28 | + translations.put(locale, strings); |
| 29 | + } |
| 30 | + |
| 31 | + public void set(String key, Map<String, Object> fields){ |
| 32 | + set(getDefaultLocale(), key, fields); |
| 33 | + } |
| 34 | + |
| 35 | + public void add(GraphArray array){ |
| 36 | + add(getDefaultLocale(), array); |
| 37 | + } |
| 38 | + |
| 39 | + public void add(Locale locale, GraphArray array){ |
| 40 | + GraphObject object = new GraphObject(); |
| 41 | + array.forEach(e -> object.set(e.object().get("key").string(), e)); |
| 42 | + add(locale, object); |
| 43 | + } |
| 44 | + |
| 45 | + public void add(GraphObject object){ |
| 46 | + add(getDefaultLocale(), object); |
| 47 | + } |
| 48 | + |
| 49 | + public void add(Locale locale, GraphObject object){ |
| 50 | + for(String key : object.keys()){ |
| 51 | + Map<String, Object> fields = new HashMap<>(); |
| 52 | + object.forEach((k, v) -> fields.put(k, v.isNumber() ? v.number() : v.string())); |
| 53 | + set(locale, key, fields); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public I18NString getString(Locale locale, String key){ |
| 58 | + if(!translations.containsKey(locale)) |
| 59 | + locale = getDefaultLocale(); |
| 60 | + if(!translations.containsKey(locale)) |
| 61 | + return new I18NString(key); |
| 62 | + Map<String, I18NString> strings = translations.get(locale); |
| 63 | + if(!strings.containsKey(key) && locale.equals(getDefaultLocale())) |
| 64 | + return new I18NString(key); |
| 65 | + strings = translations.get(getDefaultLocale()); |
| 66 | + return strings.containsKey(key) ? strings.get(key) : new I18NString(key); |
| 67 | + } |
| 68 | + |
| 69 | + public I18NString getString(String key){ |
| 70 | + return getString(getDefaultLocale(), key); |
| 71 | + } |
| 72 | + |
| 73 | + public String translate(Locale locale, String key, Object... params){ |
| 74 | + return getString(locale, key).translate(params); |
| 75 | + } |
| 76 | + |
| 77 | + public String translate(String key, Object... params){ |
| 78 | + return translate(getDefaultLocale(), key, params); |
| 79 | + } |
| 80 | + |
| 81 | + public static class I18NString { |
| 82 | + private Map<String, Object> fields; |
| 83 | + private I18NString(){} |
| 84 | + public I18NString(String message){ |
| 85 | + this(new HashMap<String, Object>(){{ |
| 86 | + put("message", message); |
| 87 | + }}); |
| 88 | + } |
| 89 | + public I18NString(Map<String, Object> fields){ |
| 90 | + this.fields = fields; |
| 91 | + } |
| 92 | + public Object getField(String key){ |
| 93 | + return fields.get(key); |
| 94 | + } |
| 95 | + public String getString(String key){ |
| 96 | + if(!fields.containsKey(key)) |
| 97 | + return null; |
| 98 | + return getField(key).toString(); |
| 99 | + } |
| 100 | + public Integer getInt(String key){ |
| 101 | + if(!fields.containsKey(key)) |
| 102 | + return null; |
| 103 | + if(fields.get(key) instanceof Integer) |
| 104 | + return (Integer) fields.get(key); |
| 105 | + return Integer.parseInt(getString(key)); |
| 106 | + } |
| 107 | + public String translate(Object... params){ |
| 108 | + String message = getString("message"); |
| 109 | + for(int i=0; i<params.length; i++) |
| 110 | + message = message.replace("{"+i+"}", params[i].toString()); |
| 111 | + return message; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments