|
| 1 | +package com.froxynetwork.froxygame.languages; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.File; |
| 5 | +import java.io.FileNotFoundException; |
| 6 | +import java.io.FileReader; |
| 7 | +import java.io.IOException; |
| 8 | +import java.util.HashMap; |
| 9 | + |
| 10 | +import org.slf4j.Logger; |
| 11 | +import org.slf4j.LoggerFactory; |
| 12 | + |
| 13 | +import lombok.AllArgsConstructor; |
| 14 | +import lombok.Getter; |
| 15 | + |
| 16 | +/** |
| 17 | + * MIT License |
| 18 | + * |
| 19 | + * Copyright (c) 2019 FroxyNetwork |
| 20 | + * |
| 21 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 22 | + * of this software and associated documentation files (the "Software"), to deal |
| 23 | + * in the Software without restriction, including without limitation the rights |
| 24 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 25 | + * copies of the Software, and to permit persons to whom the Software is |
| 26 | + * furnished to do so, subject to the following conditions: |
| 27 | + * |
| 28 | + * The above copyright notice and this permission notice shall be included in |
| 29 | + * all copies or substantial portions of the Software. |
| 30 | + * |
| 31 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 32 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 33 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 34 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 35 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 36 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 37 | + * SOFTWARE. |
| 38 | + * |
| 39 | + * @author 0ddlyoko |
| 40 | + */ |
| 41 | +/** |
| 42 | + * A language |
| 43 | + */ |
| 44 | +public interface Language { |
| 45 | + |
| 46 | + /** |
| 47 | + * @return The language associated to this language |
| 48 | + */ |
| 49 | + public Languages getLanguage(); |
| 50 | + |
| 51 | + /** |
| 52 | + * Return specific language associated to specific id with parameters |
| 53 | + * |
| 54 | + * @param id The id of the message |
| 55 | + * @param params The parameters. Can be empty |
| 56 | + * @return Specific message if found, or the id |
| 57 | + */ |
| 58 | + public String get(String id, String... params); |
| 59 | + |
| 60 | + /** |
| 61 | + * Register messages in specific file |
| 62 | + * |
| 63 | + * @param file Specific file with messages inside |
| 64 | + */ |
| 65 | + public void register(File file); |
| 66 | + |
| 67 | + @AllArgsConstructor |
| 68 | + @Getter |
| 69 | + public class LanguageImpl implements Language { |
| 70 | + |
| 71 | + private final Logger LOG = LoggerFactory.getLogger(getClass()); |
| 72 | + |
| 73 | + private Languages language; |
| 74 | + |
| 75 | + private HashMap<String, String> translates; |
| 76 | + |
| 77 | + public LanguageImpl(Languages language) { |
| 78 | + this.language = language; |
| 79 | + this.translates = new HashMap<>(); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public String get(String id, String... params) { |
| 84 | + String msg = translates.get(id); |
| 85 | + if (msg == null) |
| 86 | + return id; |
| 87 | + for (String str : params) |
| 88 | + msg = msg.replaceFirst("\\{\\}", str); |
| 89 | + return msg; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void register(File file) { |
| 94 | + LOG.info("Initializing file {}", file.getName()); |
| 95 | + try (BufferedReader br = new BufferedReader(new FileReader(file))) { |
| 96 | + String line; |
| 97 | + int lines = 0; |
| 98 | + int success = 0; |
| 99 | + int empty = 0; |
| 100 | + int error = 0; |
| 101 | + while ((line = br.readLine()) != null) { |
| 102 | + LOG.debug("Reading line {}", line); |
| 103 | + lines++; |
| 104 | + if ("".equalsIgnoreCase(line.trim())) { |
| 105 | + LOG.warn("Line {} is empty !", lines); |
| 106 | + empty++; |
| 107 | + continue; |
| 108 | + } |
| 109 | + String[] strs = line.split(" : "); |
| 110 | + if (strs.length != 2) { |
| 111 | + LOG.warn("Error while parsing line {}, found {}", lines, line); |
| 112 | + error++; |
| 113 | + continue; |
| 114 | + } |
| 115 | + String oldValue = translates.put(strs[0], strs[1]); |
| 116 | + if (oldValue != null) |
| 117 | + LOG.warn("Warning : key {} is aleady registered as value = {}", strs[0], oldValue); |
| 118 | + success++; |
| 119 | + } |
| 120 | + LOG.info("Reading {} lines, {} successfully imported, {} empty and {} errors", lines, success, empty, error); |
| 121 | + } catch (FileNotFoundException ex) { |
| 122 | + LOG.error("File {} not found", file.getName()); |
| 123 | + LOG.error("", ex); |
| 124 | + return; |
| 125 | + } catch (IOException ex) { |
| 126 | + LOG.error("IOException with file {}", file.getName()); |
| 127 | + LOG.error("", ex); |
| 128 | + return; |
| 129 | + } |
| 130 | + LOG.info("File {} initialized !", file.getName()); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments