Skip to content

Teach CheckTranslationReference about translations in Lua scripts#21490

Merged
Mailaender merged 2 commits intoOpenRA:bleedfrom
RoosterDragon:lint-lua-translations
Jul 22, 2024
Merged

Teach CheckTranslationReference about translations in Lua scripts#21490
Mailaender merged 2 commits intoOpenRA:bleedfrom
RoosterDragon:lint-lua-translations

Conversation

@RoosterDragon
Copy link
Copy Markdown
Member

@RoosterDragon RoosterDragon commented Jul 19, 2024

Using the glory of regex, we can scrape any Lua script files that a map includes and locate calls to the UserInterface.Translate method. We can then treat them in the same way as C# fields marked with a TranslationReferenceAttribute. This allows the lint check to validate the translation invoked in the .lua script has a matching entry in the translation .ftl files, with all the required arguments (if any).

We can also locate any calls to AddPrimaryObjective or AddSecondaryObjective defined by the utils.lua script, which also accept translation keys.

The are a couple of restrictions:

  • When linting the map, we don't check for keys in the ftl file that are unused. This is because the linter doesn't load all the keys when checking maps.
  • In order to validate translation arguments with the regex, we require the Lua script to pass the table of arguments inline at the callsite. If it does not, we raise a warning so the user can adjust the code.

Example by modifying allies-01:

Media.DisplayMessage(UserInterface.Translate("tanya-rules-of-engagement"), UserInterface.Translate("tanya"))

to be:

Media.DisplayMessage(UserInterface.Translate("tanya-rules-of-engagement-2"), UserInterface.Translate("tanya", { ["some-arg"] = 1 }))
local argsFromAVariable = { ["some-arg"] = 1 }
UserInterface.Translate("tanya", argsFromAVariable)

Run the utility with ra --check-yaml

gives the following logs:

Testing map: 01:   In the Thick of It
OpenRA.Utility(1,1): Warning: Script allies01.lua:30 calls UserInterface.Translate with key `tanya` and translate args passed as `argsFromAVariable`. Inline the args at the callsite for lint analysis.
OpenRA.Utility(1,1): Error: Missing variable `some-arg` for key `tanya` in ra|languages/lua/en.ftl
OpenRA.Utility(1,1): Warning: Missing key `tanya-rules-of-engagement-2` in `en` language in map translation files required by Script allies01.lua:28

We could look to later extend this to other Lua calls where a translation-key should be passed, but this seems like a nice starting point.

Recommend viewing the diff with whitespace changes hidden.

Assists with #10475

Using the glory of regex, we can scrape any Lua script files that a map includes and locate calls to the UserInterface.Translate method. We can then treat them in the same way as C# fields marked with a TranslationReferenceAttribute. This allows the lint check to validate the translation invoked in the .lua script has a matching entry in the translation .ftl files, with all the required arguments (if any).

We can also locate any calls to AddPrimaryObjective or AddSecondaryObjective defined by the utils.lua script, which also accept translation keys.

The are a couple of restrictions:
- When linting the map, we don't check for keys in the ftl file that are unused. This is because the linter doesn't load all the keys when checking maps.
- In order to validate translation arguments with the regex, we require the Lua script to pass the table of arguments inline at the callsite. If it does not, we raise a warning so the user can adjust the code.
@RoosterDragon RoosterDragon force-pushed the lint-lua-translations branch from 83a059d to cd4941d Compare July 21, 2024 14:39
@RoosterDragon
Copy link
Copy Markdown
Member Author

Updated to also check for AddPrimaryObjective and AddSecondaryObjective calls. This would've detected the issue from #21481, for example.

Copy link
Copy Markdown
Member

@Mailaender Mailaender left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works as promised.

@Mailaender Mailaender merged commit bebe3f7 into OpenRA:bleed Jul 22, 2024
@Mailaender Mailaender mentioned this pull request Jul 22, 2024
16 tasks
@Mailaender
Copy link
Copy Markdown
Member

Changelog

@RoosterDragon RoosterDragon deleted the lint-lua-translations branch July 22, 2024 16:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants