1
0
mirror of https://github.com/GeyserMC/PackConverter.git synced 2026-01-06 15:41:51 +00:00

Cleaner lang converter implementation

This commit is contained in:
rtm516
2023-06-06 22:05:48 +01:00
parent e6a26d7e4c
commit 1dd7997aab
4 changed files with 148 additions and 10 deletions

View File

@@ -48,6 +48,7 @@ import java.util.HashMap;
import java.util.Map;
import static org.geysermc.pack.util.FileUtil.exportJson;
import static org.geysermc.pack.util.FileUtil.exportProperties;
/**
* Represents a Bedrock resource pack.
@@ -68,6 +69,7 @@ public class BedrockResourcePack {
private TerrainTexture terrainTexture;
private Map<String, Attachables> attachables;
private SoundDefinitions soundDefinitions;
private Languages languages;
private Map<String, RenderControllers> renderControllers;
public BedrockResourcePack(@NotNull Path directory) {
@@ -213,6 +215,25 @@ public class BedrockResourcePack {
this.soundDefinitions = soundDefinitions;
}
/**
* Get the languages of the resource pack.
*
* @return the languages of the resource pack
*/
@Nullable
public Languages languages() {
return this.languages;
}
/**
* Set the languages of the resource pack.
*
* @param languages the languages of the resource pack
*/
public void languages(@Nullable Languages languages) {
this.languages = languages;
}
/**
* Add an item to the resource pack.
*
@@ -325,6 +346,20 @@ public class BedrockResourcePack {
this.soundDefinitions.soundDefinitions().put(id, soundDefinition);
}
/**
* Add a language to the resource pack.
*
* @param languageCode the language code
* @param translationStrings the translation strings
*/
public void addLanguage(@NotNull String languageCode, @NotNull Map<String, String> translationStrings) {
if (this.languages == null) {
this.languages = new Languages();
}
this.languages.language(languageCode, translationStrings);
}
/**
* Exports the resource pack to the specified directory.
*
@@ -363,5 +398,13 @@ public class BedrockResourcePack {
if (this.soundDefinitions != null) {
exportJson(GSON, this.directory.resolve("sounds/sound_definitions.json"), this.soundDefinitions);
}
if (this.languages != null) {
exportJson(GSON, this.directory.resolve("texts/languages.json"), this.languages.languageCodes());
for (Map.Entry<String, Map<String, String>> language : this.languages.languages().entrySet()) {
exportProperties(this.directory.resolve("texts/" + language.getKey() + ".lang"), language.getValue());
}
}
}
}

View File

@@ -0,0 +1,88 @@
/*
* Copyright (c) 2019-2023 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/PackConverter
*
*/
package org.geysermc.pack.bedrock.resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public final class Languages {
private final Map<String, Map<String, String>> languages = new HashMap<>();
public Map<String, Map<String, String>> languages() {
return Collections.unmodifiableMap(this.languages);
}
/**
* Get the language data for a specific language
*
* @param language The language code to get the data for
* @return The language data, or an empty map if the language does not exist
*/
public Map<String, String> language(String language) {
return this.languages.getOrDefault(getLanguageCode(language), Map.of());
}
/**
* Set the language data for a specific language
*
* @param language The language code to set the data for
* @param data The translation strings to put against the language
*/
public void language(String language, Map<String, String> data) {
this.languages.put(getLanguageCode(language), data);
}
public String translation(String language, String key) {
return this.language(getLanguageCode(language)).getOrDefault(key, key);
}
/**
* Set the translation for a specific language
*
* @param language The language code to set the translation for
* @param key The key to set the translation for
* @param value The value to set the translation to
*/
public void translation(String language, String key, String value) {
this.language(getLanguageCode(language)).put(key, value);
}
/**
* Get a list of all the language codes
*
* @return A list of all the language codes
*/
public String[] languageCodes() {
return this.languages.keySet().toArray(new String[0]);
}
private String getLanguageCode(String language) {
String[] parts = language.split("_");
return parts[0] + "_" + parts[1].toUpperCase();
}
}

View File

@@ -33,6 +33,8 @@ import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.Properties;
/**
* Utility class for files.
@@ -60,4 +62,18 @@ public class FileUtil {
gson.toJson(object, writer);
}
}
public static void exportProperties(@NotNull Path location, @NotNull Map<String, String> properties) throws IOException {
if (Files.notExists(location.getParent())) {
Files.createDirectories(location.getParent());
}
if (Files.notExists(location)) {
Files.createFile(location);
}
Properties propertiesFile = new Properties();
propertiesFile.putAll(properties);
propertiesFile.store(Files.newOutputStream(location), null);
}
}