Skip to content

Commit 9cd411c

Browse files
committed
[localization] initial work on API and two impls
1 parent 7605fd2 commit 9cd411c

41 files changed

Lines changed: 1384 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ def setUpLibrary(project) {
202202
implementation "org.apache.logging.log4j:log4j-api:2.19.0"
203203
implementation "org.apache.logging.log4j:log4j-core:2.19.0"
204204

205+
implementation "org.quiltmc.parsers:json:${project.rootProject.quilt_parsers_version}"
206+
205207
implementation project.dependencies.project(path: ':libraries:core', configuration: 'namedElements')
206208

207209
libraries.each { libraryPath ->

libraries/localization/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Localization API
2+
3+
The Localization API provides a consistent access point for localized text, and adds support for localization in versions that do not natively do so.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
setUpLibrary(project)
2+
3+
dependencies {
4+
implementation 'com.google.code.gson:gson:2.8.0'
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
library_id = localization
2+
library_name = Localization
3+
library_description = Localization API and events.
4+
library_version = 0.1.0-alpha.1
5+
6+
osl_dependencies = core:>=0.7.0,executors:>=0.1.0,text-components:>=0.1.0-,resource-loader:>=0.7.0-
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
setUpModule(project)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
min_mc_version = 11w49a
2+
max_mc_version = 1.5.2
3+
minecraft_dependency = >=1.1-alpha.11.49.a <=1.5.2
4+
5+
minecraft_version = 1.5.2
6+
client_nests_build = 6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package net.ornithemc.osl.localization.impl.mixin.client;
2+
3+
import java.util.Properties;
4+
5+
import org.spongepowered.asm.mixin.Mixin;
6+
import org.spongepowered.asm.mixin.gen.Accessor;
7+
8+
import net.minecraft.locale.Language;
9+
10+
@Mixin(Language.class)
11+
public interface LanguageAccess {
12+
13+
@Accessor("translations")
14+
Properties accessTranslations();
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package net.ornithemc.osl.localization.impl.mixin.client;
2+
3+
import java.util.Properties;
4+
5+
import org.spongepowered.asm.mixin.Mixin;
6+
import org.spongepowered.asm.mixin.Shadow;
7+
import org.spongepowered.asm.mixin.injection.At;
8+
import org.spongepowered.asm.mixin.injection.Inject;
9+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
10+
11+
import com.llamalad7.mixinextras.sugar.Local;
12+
13+
import net.minecraft.locale.Language;
14+
15+
import net.ornithemc.osl.localization.impl.Localization;
16+
import net.ornithemc.osl.resource.loader.api.resource.manager.ResourceManager;
17+
18+
@Mixin(Language.class)
19+
public class LanguageMixin {
20+
21+
@Shadow
22+
private static Language INSTANCE;
23+
24+
@Shadow
25+
private Properties translations;
26+
@Shadow
27+
private String currentCode;
28+
29+
@Inject(
30+
method = "<clinit>",
31+
at = @At(
32+
value = "TAIL"
33+
)
34+
)
35+
private static void osl$localization$initLocale(CallbackInfo ci) {
36+
Localization.getLocale().wrap(((LanguageAccess) INSTANCE).accessTranslations());
37+
}
38+
39+
@Inject(
40+
method = "load",
41+
at = @At(
42+
value = "TAIL"
43+
)
44+
)
45+
private void osl$localization$reloadLanguageManager(CallbackInfo ci) {
46+
// each ServerPlayerEntity also holds an instance of this class
47+
if ((Language) (Object) this == INSTANCE) {
48+
Localization.reloadLanguageManager();
49+
}
50+
}
51+
52+
@Inject(
53+
method = "loadLanguage",
54+
at = @At(
55+
value = "HEAD"
56+
)
57+
)
58+
private void osl$localization$setLanguage(CallbackInfo ci) {
59+
// each ServerPlayerEntity also holds an instance of this class
60+
if ((Language) (Object) this == INSTANCE) {
61+
Localization.getLanguageManager().setSelectedLanguage(this.currentCode);
62+
}
63+
}
64+
65+
@Inject(
66+
method = "loadLanguage",
67+
at = @At(
68+
value = "INVOKE",
69+
target = "Lnet/minecraft/locale/Language;loadTranslations(Ljava/util/Properties;Ljava/lang/String;)V"
70+
)
71+
)
72+
private void osl$localization$loadExtraTranslations(CallbackInfo ci, @Local String language, @Local Properties translations) {
73+
// each ServerPlayerEntity also holds an instance of this class
74+
if ((Language) (Object) this == INSTANCE) {
75+
// the translations map is replaced with each reload
76+
Localization.getLocale().wrap(translations);
77+
Localization.getLocale().loadLanguage(ResourceManager.client(), language);
78+
}
79+
}
80+
81+
@Inject(
82+
method = "loadLanguage",
83+
at = @At(
84+
value = "TAIL"
85+
)
86+
)
87+
private void osl$localization$localeReloaded(CallbackInfo ci) {
88+
// each ServerPlayerEntity also holds an instance of this class
89+
if ((Language) (Object) this == INSTANCE) {
90+
Localization.getLocale().setLastUpdateTime();
91+
}
92+
}
93+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"schemaVersion": 1,
3+
"id": "osl-localization",
4+
"version": "0.1.0-alpha.1+mc11w49a-mc1.5.2",
5+
"environment": "*",
6+
"mixins": [
7+
"osl.localization.mixins.json"
8+
],
9+
"depends": {
10+
"fabricloader": "\u003e\u003d0.18.0",
11+
"minecraft": "\u003e\u003d1.1-alpha.11.49.a \u003c\u003d1.5.2",
12+
"osl-core": "\u003e\u003d0.7.0",
13+
"osl-executors": "\u003e\u003d0.1.0",
14+
"osl-text-components": "\u003e\u003d0.1.0-",
15+
"osl-resource-loader": "\u003e\u003d0.7.0-"
16+
},
17+
"name": "OSL Localization",
18+
"description": "Localization API and events.",
19+
"authors": [
20+
"OrnitheMC"
21+
],
22+
"contact": {
23+
"homepage": "https://ornithemc.net/",
24+
"issues": "https://github.com/OrnitheMC/ornithe-standard-libraries/issues",
25+
"sources": "https://github.com/OrnitheMC/ornithe-standard-libraries"
26+
},
27+
"license": "Apache-2.0"
28+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"required": true,
3+
"minVersion": "0.8",
4+
"package": "net.ornithemc.osl.localization.impl.mixin",
5+
"compatibilityLevel": "JAVA_8",
6+
"mixins": [
7+
],
8+
"client": [
9+
"client.LanguageAccess",
10+
"client.LanguageMixin"
11+
],
12+
"server": [
13+
],
14+
"injectors": {
15+
"defaultRequire": 1
16+
}
17+
}

0 commit comments

Comments
 (0)