Skip to content
This repository was archived by the owner on Nov 10, 2021. It is now read-only.

Commit a0c97ff

Browse files
committed
Merge branch 'release/0.0.1'
2 parents 42af7ef + fbc55ce commit a0c97ff

8 files changed

Lines changed: 440 additions & 33 deletions

File tree

pom.xml

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0"
2-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
55

6-
<groupId>com.froxynetwork</groupId>
7-
<artifactId>FroxyGame</artifactId>
8-
<version>0.0.1</version>
9-
<packaging>jar</packaging>
6+
<groupId>com.froxynetwork</groupId>
7+
<artifactId>FroxyGame</artifactId>
8+
<version>0.0.1</version>
9+
<packaging>jar</packaging>
1010

11-
<name>FroxyGame</name>
12-
<url>https://github.com/FroxyNetwork/FroxyGame</url>
11+
<name>FroxyGame</name>
12+
<url>https://github.com/FroxyNetwork/FroxyGame</url>
1313

14-
<properties>
15-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16-
<maven.compiler.source>1.8</maven.compiler.source>
17-
<maven.compiler.target>1.8</maven.compiler.target>
18-
</properties>
19-
<developers>
20-
<developer>
21-
<name>0ddlyoko</name>
22-
<email>0ddlyokoOfficial@gmail.be</email>
23-
<roles>
24-
<role>Developer</role>
25-
</roles>
26-
</developer>
27-
</developers>
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
<maven.compiler.source>1.8</maven.compiler.source>
17+
<maven.compiler.target>1.8</maven.compiler.target>
18+
</properties>
19+
<developers>
20+
<developer>
21+
<name>0ddlyoko</name>
22+
<email>0ddlyokoOfficial@gmail.be</email>
23+
<roles>
24+
<role>Developer</role>
25+
</roles>
26+
</developer>
27+
</developers>
2828

29-
<!-- Configuration of repositories -->
30-
<repositories>
31-
<repository>
32-
<id>jitpack.io</id>
33-
<url>https://jitpack.io</url>
34-
</repository>
35-
</repositories>
36-
37-
<dependencies>
29+
<!-- Configuration of repositories -->
30+
<repositories>
31+
<repository>
32+
<id>jitpack.io</id>
33+
<url>https://jitpack.io</url>
34+
</repository>
35+
</repositories>
36+
37+
<dependencies>
3838
<!-- Logging -->
3939
<dependency>
4040
<groupId>org.slf4j</groupId>
@@ -58,10 +58,10 @@
5858
</dependency>
5959

6060
<!-- Network -->
61-
<dependency>
61+
<dependency>
6262
<groupId>com.github.froxynetwork</groupId>
6363
<artifactId>froxynetwork</artifactId>
6464
<version>0.1.2</version>
6565
</dependency>
66-
</dependencies>
66+
</dependencies>
6767
</project>

src/main/java/com/froxynetwork/froxygame/FroxyGame.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
package com.froxynetwork.froxygame;
22

3+
import static com.froxynetwork.froxygame.languages.LanguageManager.$;
4+
import static com.froxynetwork.froxygame.languages.LanguageManager.$_;
5+
6+
import java.io.File;
7+
8+
import com.froxynetwork.froxygame.languages.LanguageManager;
9+
import com.froxynetwork.froxygame.languages.Languages;
10+
311
/**
412
* MIT License
513
*
@@ -27,4 +35,25 @@
2735
*/
2836
public class FroxyGame {
2937

38+
public FroxyGame() {
39+
File lang = new File(getClass().getClassLoader().getResource("lang").getFile());
40+
LanguageManager.register(lang);
41+
// test.test in DEFAULT language
42+
System.out.println($("test.test"));
43+
// test.test2 in DEFAULT language
44+
System.out.println($("test.test2"));
45+
// test.test in French language
46+
System.out.println($("test.test", Languages.FRENCH));
47+
// test.test3 in French language. This message id doesn't exist in french, so the english message is returned
48+
System.out.println($("test.test3", Languages.FRENCH));
49+
// test.test3 in French language. This message id doesn't exist in french neither in english, so the id is returned
50+
System.out.println($_("test.test3", Languages.FRENCH));
51+
52+
System.out.println($("test.test4", "ah", "0ddlyoko"));
53+
System.out.println($("test.test4", Languages.FRENCH, "bh", "0ddlyoko"));
54+
}
55+
56+
public static void main(String[] args) {
57+
new FroxyGame();
58+
}
3059
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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

Comments
 (0)