From 4941c39f2e09b28c4685d7d7134628c26b310ed8 Mon Sep 17 00:00:00 2001 From: Adrienne Walker Date: Wed, 29 Nov 2023 23:15:45 -0800 Subject: [PATCH] raidboss: improve translation of timeline netregex Follow-up to #5939. This translates each parameter (only if needed) rather than the entire sync. It also prints out only the untranslated parameters in the find missing translations script / coverage report. This also improves the output of the translated timeline utility to use translated parameters and fixes a bug where it wouldn't have output any translated sync lines at all for netregex lines. --- ui/raidboss/timeline_parser.ts | 39 ++++++++++++++++------ util/find_missing_timeline_translations.ts | 36 ++++++++++++++------ 2 files changed, 55 insertions(+), 20 deletions(-) diff --git a/ui/raidboss/timeline_parser.ts b/ui/raidboss/timeline_parser.ts index 74995ac8a1..609246d2a5 100644 --- a/ui/raidboss/timeline_parser.ts +++ b/ui/raidboss/timeline_parser.ts @@ -3,7 +3,12 @@ import logDefinitions, { LogDefinitionTypes } from '../../resources/netlog_defs' import { buildNetRegexForTrigger } from '../../resources/netregexes'; import { UnreachableCode } from '../../resources/not_reached'; import Regexes from '../../resources/regexes'; -import { translateRegex, translateText } from '../../resources/translations'; +import { + AnonNetRegexParams, + translateRegex, + translateRegexBuildParamAnon, + translateText, +} from '../../resources/translations'; import { NetParams } from '../../types/net_props'; import { LooseTimelineTrigger, TriggerAutoConfig } from '../../types/trigger'; @@ -80,7 +85,7 @@ export type Error = { export type Sync = { id: number; - origRegexStr: string; + origInput: string | AnonNetRegexParams; regexType: 'parsed' | 'net'; regex: RegExp; start: number; @@ -500,13 +505,17 @@ export class TimelineParser { }); return line; } + + const translatedParams = translateRegexBuildParamAnon( + params, + this.options.ParserLanguage, + this.replacements, + ).params; return this.buildRegexSync( uniqueid, 'net', - `${netRegexType} ${syncCommand.netRegex}`, - // TODO: Use `translateRegexBuildParam` instead, store off params for use elsewhere. - // See https://github.com/quisquous/cactbot/pull/5939#discussion_r1399453530 - Regexes.parse(this.GetReplacedSync(buildNetRegexForTrigger(netRegexType, params))), + params, + Regexes.parse(buildNetRegexForTrigger(netRegexType, translatedParams)), syncCommand.args, seconds, lineNumber, @@ -545,7 +554,7 @@ export class TimelineParser { private buildRegexSync( uniqueid: number, regexType: 'parsed' | 'net', - regex: string, + origInput: string | AnonNetRegexParams, parsedRegex: RegExp, args: string | undefined, seconds: number, @@ -555,7 +564,7 @@ export class TimelineParser { ) { const sync: Sync = { id: uniqueid, - origRegexStr: regex, + origInput: origInput, regexType: regexType, regex: parsedRegex, start: seconds - 2.5, @@ -680,8 +689,18 @@ export class TimelineParser { if (lineText) line = line.replace(` "${lineText.name}"`, ` "${lineText.text}"`); const lineSync = lineToSync[lineNumber]; - if (lineSync) - line = line.replace(`sync /${lineSync.origRegexStr}/`, `sync /${lineSync.regex.source}/`); + if (lineSync) { + if (typeof lineSync.origInput === 'string') { + line = line.replace(`sync /${lineSync.origInput}/`, `sync /${lineSync.regex.source}/`); + } else { + const translatedParams = translateRegexBuildParamAnon( + lineSync.origInput, + timeline.options.ParserLanguage, + timeline.replacements, + ).params; + line = line.replace(/{[^}]*}/, `{ ${JSON.stringify(translatedParams)} }`); + } + } if (syncErrors?.[lineNumber]) line += ' #MISSINGSYNC'; diff --git a/util/find_missing_timeline_translations.ts b/util/find_missing_timeline_translations.ts index f1437feaed..14ecf07cd2 100644 --- a/util/find_missing_timeline_translations.ts +++ b/util/find_missing_timeline_translations.ts @@ -120,7 +120,7 @@ const findMissingTimeline = ( { type: 'replaceSync', items: new Set( - timeline.syncStarts.map((x) => ({ text: x.regex.source, line: x.lineNumber })), + timeline.syncStarts.map((x) => ({ text: x.origInput, line: x.lineNumber })), ), replace: trans.replaceSync || {}, label: 'sync', @@ -185,16 +185,32 @@ const findMissingTimeline = ( for (const testCase of testCases) { for (const item of testCase.items) { - if (isIgnored(item.text)) - continue; - let matched = false; - for (const regex in testCase.replace) { - if (item.text.match(Regexes.parse(regex))) { - matched = true; - break; + const origInput = item.text; + let errorStr: string | undefined; + if (typeof origInput === 'string') { + let matched = false; + if (isIgnored(origInput)) + continue; + for (const regex in testCase.replace) { + if (!origInput.match(Regexes.parse(regex))) { + matched = true; + break; + } + } + if (!matched) + errorStr = `"${origInput}"`; + } else { + const result = translateRegexBuildParamAnon(origInput, locale, [trans]); + const missingFields = result.missingFields; + if (!result.wasTranslated && missingFields !== undefined) { + const outputObj: { [key: string]: AnonNetRegexParams['string'] } = {}; + for (const field of missingFields) + outputObj[field] = origInput[field]; + errorStr = JSON.stringify(outputObj); } } - if (!matched) { + + if (errorStr !== undefined) { // Because we handle syncs separately from texts, in order to // sort them all properly together, create a key to be used with sort(). const sortKey = String(item.line).padStart(8, '0') + testCase.label; @@ -203,7 +219,7 @@ const findMissingTimeline = ( item.line, testCase.label, locale, - `"${item.text}"`, + errorStr, ]; } }