Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ classes/
.settings
*.launch
.architectury-transformer/debug.log
**/resources/.cache/
**/src/generated/
!test/src/main/resources/
!test/src/main/resources/**

*.patch
*.DS_Store
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

- `RegistrateLib` 是本分支的库模组入口点,同时持有共享元数据/常量
`RegistrateLib` is the library mod entrypoint for the fork and also holds shared metadata/constants
- `src/test/java/com/modularmc/registrate/test/meta` 包含用于生成的桥接方法的维护辅助工具
`src/test/java/com/modularmc/registrate/test/meta` contains maintenance helpers for generated bridge methods
- `test/src/main/java/com/modularmc/registratetest` 包含用于生成的桥接方法的维护辅助工具
`test/src/main/java/com/modularmc/registratetest` contains maintenance helpers for generated bridge methods
- 当上游 Registrate 变更时,先更新实现类,然后对照 `docs/architecture.md` 重新验证包所有权
When upstream Registrate changes, update implementation classes first and then re-verify package ownership against `docs/architecture.md`
46 changes: 1 addition & 45 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ plugins {
id 'eclipse'
id 'idea'
id 'maven-publish'
id 'jacoco'
alias libs.plugins.modDevGradle
//alias libs.plugins.shadow
alias libs.plugins.spotless
alias libs.plugins.kotlin.jvm
alias libs.plugins.ksp
}

group = maven_group
Expand Down Expand Up @@ -41,24 +39,14 @@ sourceSets {
runtimeClasspath += main.output + main.runtimeClasspath
}

test {
compileClasspath += main.output + main.compileClasspath
runtimeClasspath += main.output + main.runtimeClasspath
resources {
srcDir 'src/test/generated/resources'
}
}

extra {
compileClasspath += main.output
runtimeClasspath += main.output + main.runtimeClasspath
}

clientExtra {
compileClasspath += client.output + extra.compileClasspath
// The normal client run also mounts the test mod, so include the test source set
// here to keep generated test resources synchronized into build/resources/test.
runtimeClasspath += client.output + client.runtimeClasspath + extra.runtimeClasspath + test.output + test.runtimeClasspath
runtimeClasspath += client.output + client.runtimeClasspath + extra.runtimeClasspath
}
}

Expand All @@ -67,8 +55,6 @@ configurations {
clientRuntimeOnly.canBeResolved = false
extraRuntimeOnly.canBeResolved = false
clientExtraRuntimeOnly.canBeResolved = false
testImplementation.canBeResolved = false
testRuntimeOnly.canBeResolved = false

runtimeClasspath.extendsFrom localRuntime
extraRuntimeClasspath.extendsFrom extraLocalRuntime
Expand All @@ -93,33 +79,3 @@ tasks.withType(JavaCompile).configureEach {
options.compilerArgs << '-Xmaxerrs' << '2000'
options.compilerArgs << "-Xlint:-removal"
}

tasks.named('test') {
// The test source set is exercised by NeoForge's GameTest server.
failOnNoDiscoveredTests = false
}

jacoco {
applyTo runGameTestServer
}

tasks.named('runGameTestServer') {
jacoco.excludes = ['com.mojang.*', 'net.minecraft.*', 'net.neoforged.*']
}

jacocoTestReport {
dependsOn runGameTestServer
executionData runGameTestServer
sourceSets sourceSets.test
reports {
csv.required = false
xml.required = true
html.outputLocation = layout.buildDirectory.dir('coverage')
xml.outputLocation = layout.buildDirectory.file('coverage/coverage.xml')
}
afterEvaluate {
classDirectories.setFrom(classDirectories.asFileTree.matching {
exclude '**/client/**', '**/mixins/**'
})
}
}
7 changes: 0 additions & 7 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ dependencies {
jarJar(api(libs.kotlin.stdlib.get())) {
exclude group: 'org.jetbrains', module: 'annotations'
}
testImplementation(libs.testframework.get())
testAnnotationProcessor(project(':localization-processor'))
kspTest(project(':localization-processor'))

// Standard runtime mods //
localRuntime(libs.testframework)

renderNurseCfg(libs.renderNurse)

//////////////////////////////////////////////////////
Expand Down
4 changes: 2 additions & 2 deletions docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ Treat the root package plus `builders` and `util.entry` as the main downstream-f
Event-bus plumbing and other implementation-only helpers belong under `internal`
- 实例级 NeoForge 生命周期编排放在 `internal.lifecycle`
Instance-scoped NeoForge lifecycle orchestration belongs in `internal.lifecycle`
- 仅测试用的框架放在 `src/test/java`
Test-only scaffolding belongs under `src/test/java`
- 仅测试用的框架放在 `test/src/main/java`
Test-only scaffolding belongs under `test/src/main/java`

## 现代化原则 / Modernization Principles

Expand Down
94 changes: 94 additions & 0 deletions docs/localization-exchange.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Registrate Localization Exchange",
"type": "object",
"required": ["formatVersion", "modId", "sourceLocale", "entries"],
"properties": {
"formatVersion": { "const": 1 },
"modId": { "type": "string" },
"sourceLocale": { "$ref": "#/$defs/locale" },
"entries": {
"type": "array",
"items": { "$ref": "#/$defs/entry" }
}
},
"additionalProperties": false,
"$defs": {
"locale": {
"type": "string",
"pattern": "^[a-z]{2}_[a-z]{2}$"
},
"revision": {
"type": "string",
"pattern": "^[0-9a-f]{64}$"
},
"entry": {
"type": "object",
"required": ["namespace", "key", "source", "context", "translations"],
"properties": {
"namespace": { "type": "string" },
"key": { "type": "string" },
"source": { "$ref": "#/$defs/sourceText" },
"context": { "$ref": "#/$defs/context" },
"translations": {
"type": "array",
"items": { "$ref": "#/$defs/translation" }
}
},
"additionalProperties": false
},
"sourceText": {
"type": "object",
"required": ["locale", "text", "revision"],
"properties": {
"locale": { "$ref": "#/$defs/locale" },
"text": { "type": "string" },
"revision": { "$ref": "#/$defs/revision" }
},
"additionalProperties": false
},
"context": {
"type": "object",
"required": ["domains", "type", "glossaryIds", "unitIds", "placeholderSignature"],
"properties": {
"domains": { "type": "array", "items": { "type": "string" } },
"type": { "type": ["string", "null"] },
"glossaryIds": { "type": "array", "items": { "type": "string" } },
"unitIds": { "type": "array", "items": { "type": "string" } },
"placeholderSignature": {
"type": "object",
"propertyNames": { "pattern": "^(0|[1-9][0-9]*)$" },
"additionalProperties": { "type": "integer", "minimum": 1 }
}
},
"additionalProperties": false
},
"translation": {
"type": "object",
"required": ["locale", "text", "source", "sourceRevision", "status", "locked"],
"properties": {
"locale": { "$ref": "#/$defs/locale" },
"text": { "type": "string" },
"source": { "$ref": "#/$defs/translationSource" },
"sourceRevision": { "$ref": "#/$defs/revision" },
"status": {
"type": ["string", "null"],
"enum": ["stale", "draft", "in_review", "approved", null]
},
"locked": { "type": "boolean" }
},
"additionalProperties": false
},
"translationSource": {
"type": "object",
"required": ["kind", "location"],
"properties": {
"kind": {
"enum": ["fallback", "variant", "automatic", "glossary", "imported", "declared"]
},
"location": { "type": "string" }
},
"additionalProperties": false
}
}
}
34 changes: 19 additions & 15 deletions docs/localization.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@
```java
import com.modularmc.registrate.Registrate;
import com.modularmc.registrate.localization.MinecraftLocale;
import java.nio.file.Path;

Registrate registrate = Registrate.create(MOD_ID).sourceLocale(MinecraftLocale.EN_US);
registrate.export().localization()
.path(Path.of("build", "localization", "mymod.json"))
.build();
```

`path` is the destination JSON file. Relative paths resolve against the DataGen process working directory. The export request is fulfilled after language callbacks have run once; the same declarations produce both Minecraft language files and the exchange document.

## 声明文本

物品、方块、实体和流体的 Builder 从注册对象取 Minecraft 原生翻译 Key。通用 Builder 可用 `.localization(keyFunction, text -> ...)` 指定取 Key 的方式。
Expand All @@ -30,7 +36,7 @@ var title = registrate.addLocalization(new TranslationKey(MOD_ID, "gui.mymod.vol

`addLocalization` 返回可用于 GUI 的翻译组件。进度标题等跨侧文本可在模组构造期间声明,在服务端 DataGen 的进度回调中引用组件。`addLang`、`addRawLang` 和 `ProviderType.LANG` 回调都使用同一个源 Locale。

在 `registrate.terminology()` 中登记 `Domain`、`GlossaryTerm` 和 `MeasurementUnit`。Builder 的 `.defaults(defaults)` 可复用领域、文本类型和术语。条目的显式参数覆盖对应的默认值。
在 `registrate.terminology()` 中登记 `Domain`、`GlossaryTerm` 和 `MeasurementUnit`。用 `.localizationDefaults(defaults)` 设置模组级默认值,Builder 的 `.defaults(defaults)` 可设置条目级默认值。条目的显式参数覆盖对应默认值。

### 按语境选取术语

Expand All @@ -48,6 +54,12 @@ var title = registrate.addLocalization(new TranslationKey(MOD_ID, "gui.mymod.vol

转换会保留 Minecraft 参数、样式标记、URL、换行和已声明的单位符号。默认的 `en_us → en_ud` 同样遵循这些规则,并用显式索引保持倒置后的参数含义。

### 导出交换数据

导出文档的 `formatVersion` 当前为 `1`。每个条目保留输出命名空间、原始 Key、源文本、SHA-256 源修订、领域、TextType、词条与单位引用、占位符签名和译文候选。译文记录包含 Locale、文本、来源类型与位置、源修订、审核状态和锁定标记。自动 Locale 变体也会进入记录,`source.location` 标明原始声明与转换路径。

JSON Schema 位于 [`localization-exchange.schema.json`](localization-exchange.schema.json)。条目按命名空间和 Key 排序,译文按 Locale、来源类型和来源位置排序。显式译文的 `status` 为 `null`;导入译文保留当前审核状态和其审核时使用的源修订。

### 导入人工译文

在模组初始化时用 `importTranslation` 登记已读入的译文。提供译者审核时对应的源文本修订,以及可以定位到文件或平台条目的来源。
Expand All @@ -71,33 +83,25 @@ registrate.importTranslation(new ImportedTranslation(

DataGen 用模组当前选中的源文本核对修订。源文本变化会将译文标为 `STALE`。锁定译文仍可参与选择,未锁定的过期译文仅用于诊断,此时配置的术语、转换和回退规则可以继续解析目标 Locale。开发者显式声明优先,其次是锁定的导入译文和当前修订的普通导入译文。同一层级中,已批准、审核中和草稿依次参与选择。

## 接入注解处理器
## 注解声明

用 `./gradlew :localization-processor:jar` 构建与库版本对应的处理器。Java 源码使用 `annotationProcessor`,Kotlin 源码使用 KSP 插件和 `ksp`。混合项目可同时使用两项配置,主库作为模组依赖。

```groovy
dependencies {
def processor = files("libs/registratelib-26.1.2-${registratelib_version}-localization-processor.jar")
annotationProcessor(processor)
ksp(processor)
}
```
添加 RegistrateLib 模组依赖即可在 Java 和 Kotlin 源码中使用本地化注解。DataGen 从 NeoForge 的扫描结果读取所属模组文件中的声明。升级时移除 RegistrateLib 的 `annotationProcessor` 和 `ksp` 依赖。旧构建目录里有索引文件时,清理一次再构建。

`@Trans` 和 `@TransVariants` 可以声明在类型或字段上。`modId` 选择声明归属的模组和源 Locale。省略 `namespace` 时,资源命名空间取 `modId`。
`@Trans` 和 `@TransVariants` 可以声明在类型或字段上。文件只包含一个 Mod 时可以省略 `modId`,DataGen 使用该 Mod 的源 Locale。文件包含多个 Mod 时,每条声明都必须填写 `modId`。省略 `namespace` 时,资源命名空间取所属 Mod 的 ID。

```java
@TransVariants(modId = "mymod", keyPrefix = "gui.mymod.power", value = {
@TransVariants(keyPrefix = "gui.mymod.power", value = {
@Variant(id = "low", source = "Low power"),
@Variant(id = "high", source = "High power")
})
final class Labels {
@Trans(modId = "mymod", key = "gui.mymod.container", source = "Test Container",
@Trans(key = "gui.mymod.container", source = "Test Container",
translations = @Translation(locale = MinecraftLocale.ZH_CN, text = "测试容器"))
static final String CONTAINER = "gui.mymod.container";
}
```

Kotlin 中在带有后备字段的属性上使用 `@field:Trans` 或 `@field:TransVariants`。两个处理器按 Mod ID 写入各自的版本化声明索引,DataGen 从所属模组读取并合并。变体的 Key 由前缀和 ID 拼接,例如 `gui.mymod.power.low`。同一命名空间、Key 和 Locale 下,同级异值声明会报告双方来源。`%s` 的隐式索引按出现顺序分配,译文可用 `%1$s` 等显式索引重排。
Kotlin 中在带有后备字段的属性上使用 `@field:Trans` 或 `@field:TransVariants`。变体的 Key 由前缀和 ID 拼接,例如 `gui.mymod.power.low`。同一命名空间、Key 和 Locale 下,同级异值声明会报告双方来源,并用类名、字段名和变体 ID 定位注解。`%s` 的隐式索引按出现顺序分配,译文可用 `%1$s` 等显式索引重排。

## 注册兼容性

Expand Down
3 changes: 0 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ shadow = "9.6.1"
spotless = "8.11.0"
modDevGradle = "2.0.148"
kotlin = "2.4.21"
ksp = "2.3.14"
jetbrains-annotations = "26.1.0"
jspecify = "1.0.0"
renderNurse = "0.0.26"
Expand All @@ -17,7 +16,6 @@ minecraft = { module = "com.mojang:minecraft", version.ref = "mine
neoForge = { module = "net.neoforged:neoforge", version.ref = "neoForge" }
testframework = { module = "net.neoforged:testframework", version.ref = "neoForge" }
kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" }
ksp-api = { module = "com.google.devtools.ksp:symbol-processing-api", version.ref = "ksp" }
renderNurse = { module = "net.neoforged:render-nurse", version.ref = "renderNurse" }
mixin = { module = "org.spongepowered:mixin", version.ref = "mixin" }
jetbrains-annotations = { module = "org.jetbrains:annotations", version.ref = "jetbrains-annotations" }
Expand All @@ -28,4 +26,3 @@ modDevGradle = { id = "net.neoforged.moddev", version.ref = "modDevGr
shadow = { id = "com.gradleup.shadow", version.ref = "shadow" }
spotless = { id = "com.diffplug.spotless", version.ref = "spotless" }
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
4 changes: 0 additions & 4 deletions gradle/scripts/jars.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ jar {
exclude "**/SimplifiedToTraditional.properties"
}

tasks.named('assemble') {
dependsOn(':localization-processor:jar')
}

tasks.register('slimJar', Jar) {
dependsOn("classes")
archiveClassifier = "slim"
Expand Down
Loading
Loading