-
Notifications
You must be signed in to change notification settings - Fork 874
feat: support translation key format resource pack desc #5965
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
Open
pynickle
wants to merge
16
commits into
HMCL-dev:main
Choose a base branch
from
pynickle:feat/resourcepack-desc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
f60da7b
feat: support translation key format resource pack desc
pynickle 80ed75e
fix: resolve behavioral differences
pynickle 8185110
fix: fix test error
pynickle eb71fe4
style: s
pynickle d4d64c9
fix:resolve review
pynickle 5d54281
Merge branch 'main' into feat/resourcepack-desc
pynickle 975c584
fix: resolve country
pynickle 37dc41a
style: improve readability
pynickle 97b1156
Merge branch 'main' into feat/resourcepack-desc
pynickle 2adf9a5
style: imp
pynickle c82ca6b
fix: preserve canonical Minecraft region for bare-language locales
pynickle 732390c
perf: resolve gemini
pynickle f6ed10c
Merge branch 'main' into feat/resourcepack-desc
pynickle 5535859
Merge branch 'main' into feat/resourcepack-desc
pynickle 3092511
ref: make translatable text reusable
pynickle 40ab171
style: fix style
pynickle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
HMCLCore/src/main/java/org/jackhuang/hmcl/resourcepack/ResourcePackDescriptionResolver.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| /* | ||
| * Hello Minecraft! Launcher | ||
| * Copyright (C) 2026 huangyuhui <huanghongxun2008@126.com> and contributors | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
| package org.jackhuang.hmcl.resourcepack; | ||
|
|
||
| import com.google.gson.JsonElement; | ||
| import com.google.gson.JsonObject; | ||
| import kala.compress.archivers.zip.ZipArchiveEntry; | ||
| import org.jackhuang.hmcl.mod.LocalAddonFile; | ||
| import org.jackhuang.hmcl.mod.modinfo.PackMcMeta; | ||
| import org.jackhuang.hmcl.util.gson.JsonUtils; | ||
| import org.jackhuang.hmcl.util.i18n.MinecraftTranslatedTextResolver; | ||
| import org.jackhuang.hmcl.util.tree.ArchiveFileTree; | ||
| import org.jackhuang.hmcl.util.tree.ZipFileTree; | ||
| import org.jetbrains.annotations.Nullable; | ||
| import org.jetbrains.annotations.Unmodifiable; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.*; | ||
|
|
||
| final class ResourcePackDescriptionResolver { | ||
| private ResourcePackDescriptionResolver() { | ||
| } | ||
|
|
||
| static @Nullable LocalAddonFile.Description resolveFromFolder(Path root, Locale locale) throws IOException { | ||
| Path mcmeta = root.resolve("pack.mcmeta"); | ||
| String mcmetaText = Files.readString(mcmeta); | ||
| return resolve(mcmetaText, locale, new FolderTranslationLookup(root)); | ||
| } | ||
|
|
||
| static @Nullable LocalAddonFile.Description resolveFromZip(ZipFileTree tree, Locale locale) throws IOException { | ||
| String mcmetaText = tree.readTextEntry("/pack.mcmeta"); | ||
| return resolve(mcmetaText, locale, new ZipTranslationLookup(tree)); | ||
| } | ||
|
|
||
| static @Nullable LocalAddonFile.Description resolve(String mcmetaText, Locale locale, MinecraftTranslatedTextResolver.TranslationLookup translationLookup) { | ||
| JsonObject json = JsonUtils.fromMaybeMalformedJson(mcmetaText, JsonObject.class); | ||
| if (json == null) { | ||
| return null; | ||
| } | ||
|
|
||
| JsonObject pack = getJsonObject(json, "pack"); | ||
| if (pack == null) { | ||
| return null; | ||
| } | ||
|
|
||
| JsonElement description = pack.get("description"); | ||
| if (description instanceof JsonObject descriptionObject && descriptionObject.has("translate")) { | ||
| String translated = MinecraftTranslatedTextResolver.resolve(descriptionObject, locale, translationLookup); | ||
| return translated != null ? PackMcMeta.parseDescription(translated) : null; | ||
| } | ||
|
|
||
| return PackMcMeta.parseDescription(description); | ||
| } | ||
|
|
||
| private static @Nullable JsonObject getJsonObject(JsonObject object, String memberName) { | ||
| JsonElement element = object.get(memberName); | ||
| return element instanceof JsonObject jsonObject ? jsonObject : null; | ||
| } | ||
|
|
||
| private static final class FolderTranslationLookup implements MinecraftTranslatedTextResolver.TranslationLookup { | ||
| private final Path root; | ||
|
|
||
| private FolderTranslationLookup(Path root) { | ||
| this.root = root; | ||
| } | ||
|
|
||
| @Override | ||
| public @Unmodifiable List<String> listNamespaces() throws IOException { | ||
| Path assets = root.resolve("assets"); | ||
| if (!Files.isDirectory(assets)) { | ||
| return List.of(); | ||
| } | ||
|
|
||
| try (var stream = Files.list(assets)) { | ||
| return stream | ||
| .filter(Files::isDirectory) | ||
| .map(path -> path.getFileName().toString()) | ||
| .sorted(String.CASE_INSENSITIVE_ORDER) | ||
| .toList(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public @Nullable String findTranslation(String namespace, String languageFileName, String key) throws IOException { | ||
| Path langFile = root.resolve("assets").resolve(namespace).resolve("lang").resolve(languageFileName); | ||
| if (!Files.isRegularFile(langFile)) { | ||
| return null; | ||
| } | ||
|
|
||
| Map<String, String> translations = JsonUtils.fromJsonFile(langFile, JsonUtils.mapTypeOf(String.class, String.class)); | ||
| return translations != null ? translations.get(key) : null; | ||
| } | ||
| } | ||
|
|
||
| private static final class ZipTranslationLookup implements MinecraftTranslatedTextResolver.TranslationLookup { | ||
| private final ZipFileTree tree; | ||
|
|
||
| private ZipTranslationLookup(ZipFileTree tree) { | ||
| this.tree = tree; | ||
| } | ||
|
|
||
| @Override | ||
| public @Unmodifiable List<String> listNamespaces() { | ||
| ArchiveFileTree.Dir<ZipArchiveEntry> assets = tree.getDirectory("assets"); | ||
| if (assets == null) { | ||
| return List.of(); | ||
| } | ||
|
|
||
| return assets.getSubDirs().keySet().stream() | ||
| .sorted(String.CASE_INSENSITIVE_ORDER) | ||
| .toList(); | ||
| } | ||
|
|
||
| @Override | ||
| public @Nullable String findTranslation(String namespace, String languageFileName, String key) throws IOException { | ||
| String path = "assets/" + namespace + "/lang/" + languageFileName; | ||
| ZipArchiveEntry entry = tree.getEntry(path); | ||
| if (entry == null) { | ||
| return null; | ||
| } | ||
|
|
||
| Map<String, String> translations = JsonUtils.fromNonNullJson(tree.readTextEntry(entry), JsonUtils.mapTypeOf(String.class, String.class)); | ||
| return translations.get(key); | ||
|
pynickle marked this conversation as resolved.
pynickle marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.