Skip to content
This repository has been archived by the owner on May 1, 2021. It is now read-only.

Commit

Permalink
replaced file copy with in out streams. copy cant handle utf8
Browse files Browse the repository at this point in the history
  • Loading branch information
rainbowdashlabs committed Jul 11, 2020
1 parent a301867 commit d6ae95a
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions src/main/java/de/eldoria/eldoutilities/localization/Localizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -143,17 +142,36 @@ private void createOrUpdateLocaleFiles() {
// Create the property files if they do not exists.
for (String includedLocale : includedLocales) {
String filename = localesPrefix + "_" + includedLocale + ".properties";
try (InputStream inputStream = plugin.getResource(filename)) {
File localeFile = Paths.get(messages.toString(), filename).toFile();
if (localeFile.exists() || inputStream == null) {
continue;

File localeFile = Paths.get(messages.toString(), filename).toFile();
if (localeFile.exists()) {
continue;
}

StringBuilder builder = new StringBuilder();
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
new FileInputStream(ClassLoader.getSystemClassLoader().getResource(filename).getFile()),
StandardCharsets.UTF_8))) {
String line = bufferedReader.readLine();
while (line != null) {
builder.append(line).append("\n");
line = bufferedReader.readLine();
}
Files.copy(inputStream, localeFile.toPath());
plugin.getLogger().info("Created default locale " + filename);
} catch (IOException e) {
plugin.getLogger().log(Level.WARNING, "Failed to create default message file " + filename, e);
return;
plugin.getLogger().log(Level.WARNING, "Could not load resource " + filename + ".", e);
} catch (NullPointerException e) {
plugin.getLogger().log(Level.WARNING, "Locale " + includedLocale + " could not be loaded but should exists.", e);
continue;
}

try (OutputStreamWriter outputStream = new OutputStreamWriter(new FileOutputStream(localeFile), StandardCharsets.UTF_8)) {
outputStream.write(builder.toString());

} catch (IOException e) {
plugin.getLogger().log(Level.WARNING, "Failed to create default message file " + localeFile.getName() + ".", e);
continue;
}
plugin.getLogger().info("Created default locale " + filename);
}

List<File> localeFiles = new ArrayList<>();
Expand Down Expand Up @@ -251,7 +269,7 @@ private void createOrUpdateLocaleFiles() {
outputStream.write("# File automatically updated at "
+ DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss").format(LocalDateTime.now()) + "\n");
for (Map.Entry<String, String> entry : treemap.entrySet()) {
plugin.getLogger().info("Writing:" + entry.getKey() + "=" + entry.getValue().replace("\n", "\\n"));
plugin.getLogger().info("Writing: " + entry.getKey() + "=" + entry.getValue().replace("\n", "\\n"));
outputStream.write(entry.getKey() + "=" + entry.getValue().replace("\n", "\\n") + "\n");
}

Expand Down

0 comments on commit d6ae95a

Please sign in to comment.