Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove lang file versions #6146

Merged
merged 2 commits into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion resources/lang/az-AZ.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: english
author: ElgarL
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/bg-BG.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: english
author: ElgarL
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/cs-CZ.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: english
author: ElgarL
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/da-DK.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: dansk
author: LifeOnEarth
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/de-DE.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: german
author: ElgarL, translated by Articdive, Wolf2323, BlocK, Yasu-San and enterih
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/en-US.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
name: Towny
version: 0.155
language: english
author: ElgarL
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/es-419.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.150
language: es-419
author: VreyaViress, EuGNosii
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/es-AR.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: spanish
author: Seruhio, Alvarote1998, Beelzebu, ElMoha943, Ngzhiwei
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/es-CL.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: es-cl
author: GNosii
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/es-EC.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: spanish
author: Seruhio, Alvarote1998, Beelzebu, ElMoha943, Ngzhiwei
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/es-ES.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: spanish
author: Seruhio, Alvarote1998, Beelzebu, ElMoha943, Ngzhiwei
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/es-MX.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: spanish
author: Seruhio, Alvarote1998, Beelzebu, ElMoha943, Ngzhiwei
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/es-UY.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: spanish
author: Seruhio, Alvarote1998, Beelzebu, ElMoha943, Ngzhiwei
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/es-VE.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: spanish
author: Seruhio, Alvarote1998, Beelzebu, ElMoha943, Ngzhiwei
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/fr-FR.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: french
author: Noiknez,TheCalypso,Cidalex,Mitsu,ARNPIK,Dzious,PainOchoco
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/he-IL.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: english
author: ElgarL
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/id-ID.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: Indonesian
author: ElgarL
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/it-IT.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: italian
author: Leonardo_DGS
website: 'https://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/ja-JP.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: english
author: ElgarL
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/ko-KR.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: 한국어
author: Daybreak 새벽
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/nl-NL.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: Dutch
author: ElgarL
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/no-NO.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: norwegian
author: Nectuz, Walbern
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/pl-PL.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: polish
author: ElgarL
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/pt-BR.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: português (Brasil)
author: BannerGames, Plugner
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/pt-PT.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: english
author: ElgarL
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/ro-RO.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: Romanian
author: ElgarL
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/ru-RU.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: русский
author: ElgarL (Plugin developer), Communar, Egor33345 (Russian Translation)
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/sr-CS.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: english
author: ElgarL
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/sv-SE.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: swedish
author: ElgarL
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/th-TH.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: english
author: ElgarL
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/tl-PH.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: english
author: ElgarL
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/tr-TR.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: Turkish
author: ElgarL
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/uk-UA.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: english
author: ElgarL
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/vi-VN.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: english
author: ElgarL
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/zh-CN.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: Chinese
author: ElgarL
website: 'http://townyadvanced.github.io/'
Expand Down
1 change: 0 additions & 1 deletion resources/lang/zh-TW.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: Towny
version: 0.155
language: zh-TW
author: ElgarL
website: 'http://townyadvanced.github.io/'
Expand Down
54 changes: 21 additions & 33 deletions src/com/palmergames/bukkit/towny/object/TranslationLoader.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.palmergames.bukkit.towny.object;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Collections;
Expand All @@ -20,10 +21,11 @@
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.compress.utils.FileNameUtils;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.Nullable;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;

Expand Down Expand Up @@ -125,12 +127,12 @@ void loadTranslationsIntoMemory() {
// Load bundled language files
for (String lang : getLangFileNamesFromPlugin()) {
try (InputStream is = clazz.getResourceAsStream("/lang/" + lang + ".yml")) {
if (is == null) {
if (is == null)
throw new TownyInitException("Could not find " + "'/lang/" + lang + ".yml'" + " in the JAR", TownyInitException.TownyError.LOCALIZATION);
}

Map<String, Object> values = new Yaml(new SafeConstructor()).load(is);

saveReferenceFile(values.get("version"), lang);
saveReferenceFile(lang);

lang = lang.replace("-", "_"); // Locale#toString uses underscores instead of dashes
if (!newTranslations.containsKey(lang))
Expand All @@ -153,11 +155,11 @@ private Set<String> getLangFileNamesFromPlugin() {
final URI uri;
try {
uri = clazz.getResource("").toURI();
final FileSystem fs = FileSystems.newFileSystem(uri, Collections.emptyMap());
Files.list(fs.getRootDirectories().iterator().next().resolve("/lang"))
.filter(p -> TownySettings.isLanguageEnabled(FileNameUtils.getBaseName(p.toString().replace("-", "_"))))
.forEach(p -> lang.add(FileNameUtils.getBaseName(p.toString())));
fs.close();

try (final FileSystem fs = FileSystems.newFileSystem(uri, Collections.emptyMap()); Stream<Path> stream = Files.list(fs.getRootDirectories().iterator().next().resolve("/lang"))) {
stream.filter(p -> TownySettings.isLanguageEnabled(FileNameUtils.getBaseName(p.toString().replace("-", "_"))))
.forEach(p -> lang.add(FileNameUtils.getBaseName(p.toString())));
}
} catch (URISyntaxException | IOException e) {
e.printStackTrace();
}
Expand All @@ -167,42 +169,28 @@ private Set<String> getLangFileNamesFromPlugin() {
/**
* Saves a copy of the language file for admin reference.
*
* @param currentVersion Object what will be the version number of the lang file.
* @param lang String locale and file name to be used for the reference file.
*/
private void saveReferenceFile(@Nullable Object currentVersion, String lang) {
if (currentVersion == null)
return;

private void saveReferenceFile(String lang) {
// Resolves langfolder/reference/whatever_language.yml
Path langPath = langFolderPath.resolve("reference").resolve(lang + ".yml");
// Files.copy takes care of the creation of lang.yml AS LONG AS the parent directory exists
// Which we take care of right before the languages are looped through.

// Get the resource
try (InputStream resource = clazz.getResourceAsStream("/lang/" + lang + ".yml")) {
if (resource == null)
return;

if (resource == null) {
throw new TownyInitException("Could not find " + "'/lang/" + lang + ".yml'" + " in the JAR.", TownyInitException.TownyError.LOCALIZATION);
}
try (BufferedReader br = new BufferedReader(new InputStreamReader(resource)); Stream<String> lines = Files.lines(langPath)) {
String string = br.lines().collect(Collectors.joining("\n"));

// Check the existing lang file for the version, and if necessary, replace it.
try (InputStream is = Files.newInputStream(langPath)) {
Map<String, Object> values = new Yaml(new SafeConstructor()).load(is);
if (values == null || (double) currentVersion != (double) values.get("version")) {
is.close();
// Remove the old reference file.
langPath.toFile().delete();
// Copy resource to location.
Files.copy(resource, langPath);
}
} catch (NoSuchFileException e) {
// We haven't got this file in the reference folder yet.
Files.copy(resource, langPath);
// If the contents of the jar's lang file don't match the saved reference file's contents, replace the contents.
if (!string.equals(lines.collect(Collectors.joining("\n"))))
FileMgmt.writeString(langPath, string);
}
resource.close();
} catch (IOException e) {
throw new TownyInitException("Failed to copy " + "'/lang/" + lang + ".yml'" + " from the JAR to '" + langPath + " during a langauge file update.'", TownyInitException.TownyError.LOCALIZATION, e);
plugin.getLogger().warning("Failed to copy " + "'/lang/" + lang + ".yml'" + " from the JAR to '" + langPath.toAbsolutePath() + " during a language file update.'");
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/com/palmergames/util/FileMgmt.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -460,4 +461,9 @@ public static void savePlotData(PlotBlockData data, File file, String path) {
}
}

public static void writeString(Path path, String string) throws IOException {
try (FileOutputStream fos = new FileOutputStream(path.toFile())) {
fos.write(string.getBytes(StandardCharsets.UTF_8));
}
}
}