-
Notifications
You must be signed in to change notification settings - Fork 20
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
Initial localization support #37
Open
NebelNidas
wants to merge
14
commits into
FabricMC:dev
Choose a base branch
from
NebelNidas:i18n
base: dev
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
14 commits
Select commit
Hold shift + click to select a range
848b19e
Initial localization support
NebelNidas c3071eb
Remove MCP dir from translation strings
NebelNidas 0ee988f
Return a `Translatable` instead of translating implicitly
NebelNidas 2686e02
Merge upstream
NebelNidas e533fc1
Only test supported languages
NebelNidas b83edf9
Replace `assertTrue` with `assertEquals`
NebelNidas 5ebad57
Move lang files into `mappingio` directory
NebelNidas 8b4431b
Optimize bundle loading
NebelNidas b36e543
Merge upstream
NebelNidas 3f67d1c
Some API changes
NebelNidas f1d5555
Add translation key getter
NebelNidas 9d81e04
Adjust Javadoc
NebelNidas 60e0c7c
Remove note about translation key stability guarantees
NebelNidas c218104
Change field order
NebelNidas 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 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 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 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,111 @@ | ||
/* | ||
* Copyright (c) 2023 FabricMC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.fabricmc.mappingio.i18n; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.PrintWriter; | ||
import java.io.Reader; | ||
import java.io.StringWriter; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.HashMap; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.PropertyResourceBundle; | ||
import java.util.ResourceBundle; | ||
import java.util.concurrent.locks.Lock; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
@ApiStatus.Internal | ||
public class I18n { | ||
private I18n() { | ||
} | ||
|
||
public static String translate(String key, Locale locale, Object... args) { | ||
return String.format(translate(key, locale), args); | ||
} | ||
|
||
public static String translate(String key, Locale locale) { | ||
try { | ||
return load(locale).getString(key); | ||
} catch (Exception e) { | ||
System.err.println("Exception while translating key " + key + " to locale " + locale.toLanguageTag() + ": " + getStackTrace(e)); | ||
if (locale == fallbackLocale) return key; | ||
|
||
try { | ||
return load(fallbackLocale).getString(key); | ||
} catch (Exception e2) { | ||
System.err.println("Exception while translating key " + key + " to fallback locale: " + getStackTrace(e2)); | ||
return key; | ||
} | ||
} | ||
} | ||
|
||
private static ResourceBundle load(Locale locale) { | ||
ResourceBundle bundle = messageBundles.get(locale); | ||
|
||
if (bundle != null) { | ||
return bundle; | ||
} | ||
|
||
bundlesLock.lock(); | ||
|
||
try { | ||
if ((bundle = messageBundles.get(locale)) != null) { | ||
return bundle; | ||
} | ||
|
||
return load0(locale); | ||
} finally { | ||
bundlesLock.unlock(); | ||
} | ||
} | ||
|
||
private static ResourceBundle load0(Locale locale) { | ||
ResourceBundle resBundle; | ||
String resName = String.format("/mappingio/lang/%s.properties", locale.toLanguageTag().replace('-', '_').toLowerCase(Locale.ROOT)); | ||
URL resUrl = I18n.class.getResource(resName); | ||
|
||
if (resUrl == null) { | ||
throw new RuntimeException("Locale resource not found: " + resName); | ||
} | ||
|
||
try (Reader reader = new InputStreamReader(resUrl.openStream(), StandardCharsets.UTF_8)) { | ||
resBundle = new PropertyResourceBundle(reader); | ||
messageBundles.put(locale, resBundle); | ||
return resBundle; | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to load " + resName, e); | ||
} | ||
} | ||
|
||
private static String getStackTrace(Throwable t) { | ||
if (t == null) return null; | ||
|
||
StringWriter sw = new StringWriter(); | ||
PrintWriter pw = new PrintWriter(sw); | ||
t.printStackTrace(pw); | ||
return sw.toString(); | ||
} | ||
|
||
private static final Lock bundlesLock = new ReentrantLock(); | ||
private static final Locale fallbackLocale = Locale.US; | ||
private static final Map<Locale, ResourceBundle> messageBundles = new HashMap<>(); | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/net/fabricmc/mappingio/i18n/Translatable.java
This file contains 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,40 @@ | ||
/* | ||
* Copyright (c) 2024 FabricMC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.fabricmc.mappingio.i18n; | ||
|
||
import java.util.Locale; | ||
|
||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
@ApiStatus.NonExtendable | ||
public interface Translatable { | ||
@ApiStatus.Internal | ||
static Translatable of(String translationKey) { | ||
return new TranslatableImpl(translationKey); | ||
} | ||
|
||
/** | ||
* Translates this translatable to the specified locale, with a fallback to en_US. | ||
*/ | ||
String translate(Locale locale); | ||
|
||
/** | ||
* Returns the translation key of this translatable, allowing consumers to provide their own translations | ||
* via custom localization facilities. | ||
*/ | ||
String translationKey(); | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/net/fabricmc/mappingio/i18n/TranslatableImpl.java
This file contains 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,37 @@ | ||
/* | ||
* Copyright (c) 2024 FabricMC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.fabricmc.mappingio.i18n; | ||
|
||
import java.util.Locale; | ||
|
||
final class TranslatableImpl implements Translatable { | ||
TranslatableImpl(String translationKey) { | ||
this.translationKey = translationKey; | ||
} | ||
|
||
@Override | ||
public String translate(Locale locale) { | ||
return I18n.translate(translationKey, locale); | ||
} | ||
|
||
@Override | ||
public String translationKey() { | ||
return translationKey; | ||
} | ||
|
||
private final String translationKey; | ||
} |
This file contains 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,14 @@ | ||
format.tiny_file = Tiny File | ||
format.tiny_2_file = Tiny v2 File | ||
format.enigma_file = Enigma File | ||
format.enigma_dir = Enigma Directory | ||
format.proguard_file = ProGuard File | ||
format.srg_file = SRG File | ||
format.xsrg_file = XSRG File | ||
format.jam_file = JAM File | ||
format.csrg_file = CSRG File | ||
format.tsrg_file = TSRG File | ||
format.tsrg_2_file = TSRG v2 File | ||
format.intellij_migration_map_file = IntelliJ Migration Map File | ||
format.recaf_simple_file = Recaf Simple File | ||
format.jobf_file = JOBF File |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of this shouldnt even be translated, other than maybe "File". Im not really sure this should be handled in mapping-io.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some languages will need to hyphenate individual words, switch their order around, or even outright translate the format names. For example in German, you'd have
Tiny-v2-Datei
andIntelliJ-Migrationszuordnungsdatei
(at least when going with the German language pack's translation).I think this makes sense being part of Mapping-IO natively, as I don't want to duplicate the translation files across a dozen different applications like Enigma, Matcher and JADX, just to name a few. And you're not forced to use MIO's translations either, you can always delegate to a custom localization system via
Translatable#translationKey()
. Not to mention that these facilities are required for #88 and #94 to provide user-friendly warnings and error messages e.g. in Enigma.