I want to add a contextual menu in InDesign. I use this command: app.menus.item("Text Context Menu").submenus.item("MY_MENU")
It works, but when I switch my local to another than english (ex.: fr_FR) , it won’t add because I have to use this: app.menus.item("Menu contextuel Texte").submenus.item("MY_MENU")
I’ve tried with ‘index’ attribut, but it changes depending on the language.
How can I add menu items without knowing the name which is language dependent.
As a smart solution here is to identify the Menu using UXP API you can use the menuID property rather than the menu name, wich is consistent across different languages. then you can use entrypoints in your manifest file to define commands and panels dynamically. this method ensures the proper integration of menu items irrespective of the language settings.
I don’t know where @YesserKira got that constant, and whether it works for the particular InDesign menu. Even when it does, I’d find it suboptimal if UXP reinvents the wheel of string IDs referring to the application string database.
On the other hand, I’d appreciate a UXP subsystem supporting third party localizations similar to ExtendScript’s ScriptUI localization features. Sorry, I digress.
If you continue with the traditional way to refer to translatable strings with the “$ID/” prefix, as mentioned by @PeterKahrel , have a look at app.findKeyStrings() to find key string candidates from your localized string, and use app.translateKeyString() to resolve that key string. e.g. app.translateKeyString(“Text Context Menu”) should yield the menu name, even if the prefix approach fails for UXP.
InDesign Strings, including those for menus and actions, are localized. You find the localized name e.g. from the menu.name iterating the menus collection, as you already got it.
To undo that localize and find the key string, use app.findKeyStrings(“Menu contextuel Texte”), it should produce an array of candidates. In your case, the key string would be “RtMouseText”, and you’d add the translation prefix “$ID/” as also suggested by Peter.
There is one problem though, at least with the function app.translateKeyString() the automatic translation does not work. You may have a similar problem with app.menus.item(“$ID …, I did not try.
Therefor use an explicit translation, via ExtendScript.
The result of the function app_translateKeyString() below should then be your “Menu contextuel Texte”, or “Text-Kontextmenü” or … for your further use.
const { ScriptLanguage,
app } = require("indesign");
function alert(){
app.doScript(
"alert.apply(this,arguments)",
ScriptLanguage.JAVASCRIPT,
[...arguments]);
}
function app_translateKeyString(str){
return app.doScript(
"app.translateKeyString.apply(app,arguments)",
ScriptLanguage.JAVASCRIPT,
[...arguments]);
}
try {
var name = app_translateKeyString("$ID/RtMouseText");
alert(name);
} catch( ex ) {
alert(ex.toString());
}
I’m using app.doScript here to invoke ExtendScript because UXP still has other issues
alert has a better implementation in ES
app.translateKeyString does not work as expected (bug ID-4266107 is “toFix”).
While this looks a bit unfortunate, many features of UXP work without such hoops. Also unlike other hosts we actually have a way to invoke ExtendScript.