Skip to content

Commit 58de1fb

Browse files
committed
initial work on API and two impls
1 parent cfc9ffc commit 58de1fb

23 files changed

Lines changed: 600 additions & 0 deletions

File tree

build.gradle

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

191+
implementation "org.quiltmc.parsers:json:${project.rootProject.quilt_parsers_version}"
192+
191193
implementation project.dependencies.project(path: ':libraries:core', configuration: 'namedElements')
192194
}
193195

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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
setUpLibrary(project)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
entrypoint_init = net.ornithemc.osl.resource.loader.impl.ResourceLoader
7+
entrypoint_client_init = net.ornithemc.osl.resource.loader.impl.ResourceLoader
8+
osl_dependencies = core:>=0.7.0-,entrypoints:>=0.5.0-,lifecycle-events:>=0.6.0-
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
setUpModule(project,
2+
'entrypoints-mc13w16a-mc1.14.4',
3+
'lifecycle-events-mc19w04a-mc1.14.4',
4+
'resource-loader-mc19w08a-mc1.14.4'
5+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
min_mc_version = 19w08a
2+
max_mc_version = 1.14.4
3+
minecraft_dependency = >=1.13-alpha.17.43.a <=1.14.4
4+
5+
minecraft_version = 1.14.4
6+
feather_build = 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package net.ornithemc.osl.localization.impl.mixin.client;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
import org.spongepowered.asm.mixin.Final;
9+
import org.spongepowered.asm.mixin.Mixin;
10+
import org.spongepowered.asm.mixin.Shadow;
11+
import org.spongepowered.asm.mixin.injection.At;
12+
import org.spongepowered.asm.mixin.injection.Inject;
13+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
14+
15+
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
16+
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
17+
import com.llamalad7.mixinextras.sugar.Local;
18+
19+
import net.minecraft.client.resource.language.Locale;
20+
import net.minecraft.resource.Identifier;
21+
import net.minecraft.resource.Resource;
22+
import net.minecraft.resource.manager.ResourceManager;
23+
24+
import net.ornithemc.osl.localization.impl.Localization;
25+
import net.ornithemc.osl.localization.impl.language.Translations;
26+
27+
@Mixin(Locale.class)
28+
public abstract class LocaleMixin {
29+
30+
@Final
31+
@Shadow
32+
private Map<String, String> translations;
33+
34+
@Shadow
35+
private void load(List<Resource> resources) { }
36+
37+
@Inject(
38+
method = "load(Lnet/minecraft/resource/manager/ResourceManager;Ljava/util/List;)V",
39+
at = @At(
40+
value = "INVOKE",
41+
target = "Lnet/minecraft/resource/manager/ResourceManager;getResources(Lnet/minecraft/resource/Identifier;)Ljava/util/List;"
42+
)
43+
)
44+
private void osl$localization$loadLanguage(ResourceManager resourceManager, List<String> languageCodes, CallbackInfo ci,
45+
@Local(ordinal = 0) String languageCode, @Local(ordinal = 2) String namespace) {
46+
String[] paths = new String[] {
47+
String.format("lang/%s.lang", languageCode),
48+
String.format("lang/%s.json", languageCode.toLowerCase(java.util.Locale.ROOT)),
49+
String.format("lang/%s.lang", languageCode.toLowerCase(java.util.Locale.ROOT))
50+
};
51+
52+
for (String path : paths) {
53+
try {
54+
load(resourceManager.getResources(new Identifier(namespace, path)));
55+
} catch (IOException ignored) {
56+
}
57+
}
58+
}
59+
60+
@Inject(
61+
method = "load(Lnet/minecraft/resource/manager/ResourceManager;Ljava/util/List;)V",
62+
at = @At(
63+
value = "TAIL"
64+
)
65+
)
66+
private void osl$localization$languageLoaded(ResourceManager resourceManager, List<String> languageCodes, CallbackInfo ci) {
67+
// create a new translator every time in case another mod messes with the map
68+
Localization.languageLoaded(languageCodes.get(languageCodes.size() - 1), Translations.wrap(translations));
69+
}
70+
71+
@WrapOperation(
72+
method = "load(Ljava/util/List;)V",
73+
at = @At(
74+
value = "INVOKE",
75+
target = "Lnet/minecraft/client/resource/language/Locale;load(Ljava/io/InputStream;)V"
76+
)
77+
)
78+
private void osl$localization$loadTranslations(Locale instance, InputStream is, Operation<Void> original, @Local Resource resource) throws IOException{
79+
if (resource.getLocation().getPath().endsWith(".lang")){
80+
Translations.loadFromLang(is, translations::put);
81+
} else {
82+
original.call(instance, is); // load .json
83+
}
84+
}
85+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"schemaVersion": 1,
3+
"id": "osl-resource-loader",
4+
"version": "0.6.0-alpha.1+mc19w08a-mc1.14.4",
5+
"environment": "*",
6+
"entrypoints": {
7+
"init": [
8+
"net.ornithemc.osl.resource.loader.impl.ResourceLoader"
9+
],
10+
"client-init": [
11+
"net.ornithemc.osl.resource.loader.impl.ResourceLoader"
12+
]
13+
},
14+
"mixins": [
15+
"osl.resource-loader.mixins.json"
16+
],
17+
"depends": {
18+
"fabricloader": "\u003e\u003d0.17.3",
19+
"minecraft": "\u003e\u003d1.14-alpha.19.8.a \u003c\u003d1.14.4",
20+
"osl-core": "\u003e\u003d0.7.0-",
21+
"osl-entrypoints": "\u003e\u003d0.5.0-",
22+
"osl-lifecycle-events": "\u003e\u003d0.6.0-"
23+
},
24+
"name": "OSL Resource Loader",
25+
"description": "Resource loading API and events.",
26+
"authors": [
27+
"OrnitheMC"
28+
],
29+
"contact": {
30+
"homepage": "https://ornithemc.net/",
31+
"issues": "https://github.com/OrnitheMC/ornithe-standard-libraries/issues",
32+
"sources": "https://github.com/OrnitheMC/ornithe-standard-libraries"
33+
},
34+
"license": "Apache-2.0"
35+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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.LocaleMixin"
10+
],
11+
"server": [
12+
],
13+
"injectors": {
14+
"defaultRequire": 1
15+
}
16+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
setUpModule(project,
2+
'entrypoints-mcin-20091223-1459-mc1.5.2',
3+
'resource-loader-mca1.2.2-mc11w48a'
4+
)

0 commit comments

Comments
 (0)