bbepis / XUnity.AutoTranslator

MIT License
1.98k stars 292 forks source link

Regex + RichText translation #302

Open zclimber opened 2 years ago

zclimber commented 2 years ago

Hello! I have a string in a game that I want to create a regex for, that looks like <color\=white>総ユニット数:18/30</color> (<color\=#80ffffff>未配置:1</color> <color\=#80ff80ff>配置済:9</color> <color\=#ffaa80ff>略奪:8</color> <color\=#ff4040ff>負傷:0</color> )

I try to create a regex for it like this: r:"^<color\\=(#?\w+)>総ユニット数:(\d+)\/(\d+)<\/color> \(<color\\=#80ffffff>未配置:(\d+)<\/color> <color\\=#80ff80ff>配置済:(\d+)<\/color> <color\\=#ffaa80ff>略奪:(\d+)<\/color> <color\\=#ff4040ff>負傷:(\d+)<\/color> \)$"=<color\=$1>Total number of units:$2/$3</color> (<color\=#80ffffff>Unassigned:$4</color> <color\=#80ff80ff>Deployed: $5</color> <color\=#ffaa80ff>Looting:$6</color> <color\=#ff4040ff>Injured: $7</color> )

But it does not work. I have checked, the whole string matches the regex. I have also tried setting MaxTextParserRecursion=2 and using separate regexes for rich text parts, like this:


r:"未配置:(\d+)"=Unassigned:$1
r:"配置済:(\d+)"=Deployed:$1
r:"略奪:(\d+)"=Looting:$1
r:"負傷:(\d+)"=Injured:$1```

It also didn't work.
Can anybody help me with it?
zclimber commented 2 years ago

Okay, turns out that TemplateAllNumberAway=True messes up matching, and the string that Translator tries to match to regex looks like this: <color=white>総ユニット数:{{A}}</color> (<color=#{{B}}ffffff>未配置:{{C}}</color> <color=#{{D}}ff{{E}}ff>配置済:{{F}}</color> <color=#ffaa{{G}}ff>略奪:{{H}}</color> <color=#ff{{I}}ff>負傷:{{J}}</color> )

zclimber commented 2 years ago

So what worked for me was to add several substituted strings that correspond to RichText parts:

未配置:{{A}}=Unassigned:{{A}}
配置済:{{A}}=Deployed:{{A}}
略奪:{{A}}=Looting:{{A}}
負傷:{{A}}=Injured:{{A}}

Still, problem of regex matching with TemplateAllNumberAway=True probably needs some work.

pipja commented 2 years ago

I have a similar problem with regex matching: XUnity output this translation line: 每一点根骨提供基础法术强度+4%,速度+0.1\n\n当前:基础法术强度 14.0=Each point of root bone provides +4% base spell strength and +0.1 speed\n\nCurrent: Base Spell Strength 14.0

When I tried to replace it with this regex it fails to match: sr:"每一点根骨提供基础法术强度+4%,速度+0.1\n\n当前:基础法术强度 ([\d\.]+)"=Each point of root bone provides +4% base spell strength and +0.1 speed\n\nCurrent: Base Spell Strength $1

The text should be identical in both cases, and I have TemplateAllNumberAway=False in the config but it seems like that does no effect.

EDIT: After looking at another Regex issue thread, I realised my mistakes: the "+"s and the "\n"s were failing to match. Here is an updated regex that works: sr:"每一点体魄提供基础攻击强度\+4%,速度\+0.1\\n\\n当前:基础攻击强度 ([\d\.]+)"=Each point of Physique provides +4% to base attack strength and +0.1 to speed\n\nCurrent: Base Attack Strength $1