How to use UserInterface.getText method?

How to use UserInterface.getText method?

kandennti
Mentor Mentor
1,217 Views
6 Replies
Message 1 of 7

How to use UserInterface.getText method?

kandennti
Mentor
Mentor

Please let me know if you know of any.

 

I believe the UserInterface.getText method can be used to get the language corresponding to the language being activated.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-284B455C-E192-41F7-B5D6-B086BF34E370 

 

I couldn't find what the module names (xml files) listed in the documentation are.

I looked in the "StringTable" folder and found it in this path. (Win)

C:\Users\<USER_NAME>\AppData\Local\Autodesk\webdeploy\production\<Ver_Folder>\StringTable

 

I actually tested with something like this.

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core

def run(context):
    ui = adsk.core.UserInterface.cast(None)
    try:
        app: adsk.core.Application = adsk.core.Application.get()
        ui = app.userInterface

        txt = ui.getText(
            'DesignVariationsUI10',
            'ActivateGenWorkingModel',
            'NG'
        )

        app.log(txt)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

The return value is defaultValue.
Does anyone have specific code that actually works?

0 Likes
Accepted solutions (2)
1,218 Views
6 Replies
Replies (6)
Message 2 of 7

Jorge_Jaramillo
Collaborator
Collaborator

Hi @kandennti 

 

I'm getting the same result as you:

 

 ui.getText('Fusion360App10','lclzSafeModeClose','defaultVal')='defaultVal'
 ui.getText('Fusion360App10.xml','lclzSafeModeClose','defaultVal')='defaultVal'
 ui.getText('Fusion360App','lclzSafeModeClose','defaultVal')='defaultVal'

 

I checked it in many ways, and none of them worked.

 

Don't know what's going in here.

 

Regards,
Jorge

Message 3 of 7

kandennti
Mentor
Mentor

@Jorge_Jaramillo  Thanks for the reply.

 

I decided to look into it myself since there was no point in waiting.

0 Likes
Message 4 of 7

kandennti
Mentor
Mentor
Accepted solution

I have created a langText function that works the same as the getText method I had in mind and have attached it.
I have also extended it with an optional fourth argument to allow the language to be specified.
If omitted, it will be the language of the running Fusion360.

 

It is intended to be imported and used.
I created a test code like this and checked it.

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core
from .LanguageTextHelper import langText

def run(context):
    ui = adsk.core.UserInterface.cast(None)
    try:
        app: adsk.core.Application = adsk.core.Application.get()
        ui = app.userInterface


        res = langText(
            'DesignVariationsUI10',
            'ActivateGenWorkingModel',
            'NG',
        )
        print(f'function langText:{res}')


        res = langText(
            'DesignVariationsUI10',
            'ActivateGenWorkingModel',
            'NG',
            'de-DE',
        )
        print(f'Specify language as the fourth argument:{res}')


        res = ui.getText(
            'DesignVariationsUI10',
            'ActivateGenWorkingModel',
            'NG',
        )
        print(f'Original getText method:{res}')


        # It is not very desirable, but it is not working, so we override it.
        ui.getText = langText
        res = ui.getText(
            'DesignVariationsUI10',
            'ActivateGenWorkingModel',
            'NG',
        )
        print(f'Overridden getText method:{res}')

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

Here is the execution result.
(I am using it in a Japanese environment)

function langText:ジェネレーティブ モデルをアクティブ化
Specify language as the fourth argument:Generatives Modell aktivieren
Original getText method:NG
Overridden getText method:ジェネレーティブ モデルをアクティブ化

 

 

I also prepared a support function to dump "module" and "id" and the languages that can be used, which I questioned in the getText method.
You can check them by doing this.

        # Language
        from .LanguageTextHelper import dumpLanguageNames
        langNames = dumpLanguageNames()
        print(langNames)

        # module
        from .LanguageTextHelper import dumpModuleNames
        moduleNames = dumpModuleNames()
        print(moduleNames)
        moduleNames_es = dumpModuleNames('es-ES')

        # id
        from .LanguageTextHelper import dumpIds
        ids = dumpIds('DesignVariationsUI10')
        print(ids)
        ids_it = dumpIds('DesignVariationsUI10', 'it-IT')

 

This is of little interest to English speakers, but I hope it will be useful to users of other languages.

Message 5 of 7

BrianEkins
Mentor
Mentor
Accepted solution

I couldn't get this to work either, and I talked to someone at Autodesk about it. There ended up being two problems. The first is with the documentation. It says the module name argument should not include the .xml suffix of the file. This is true, but it should also not contain the version. For example, the file "NuCommands10.xml" would be specified as "NuCommands".

 

The second problem is a bug in the function. If Fusion is set to use English, getText will always return what you specified for the default argument. It should work correctly for all other languages.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 6 of 7

Jorge_Jaramillo
Collaborator
Collaborator

Hi,

 

I can confirm what @BrianEkins says regarding other languages.

With Spanish it works, without the ".xml" extension and without the version "10" (I wonder where this version number is coming from).

 

Test script:

 

def get_localized_text():
    try:
        app.log(f"{app.preferences.generalPreferences.userLanguage=}")
        app.log(f"{ui.getText('Fusion360App','lclzSafeModeClose','defaultVal')=}")
        app.log(f"{ui.getText('Fusion360App10','lclzSafeModeClose','defaultVal')=}")
        app.log(f"{ui.getText('Fusion360App10.xml','lclzSafeModeClose','defaultVal')=}")
    except:
        app.log(f'ERROR: {traceback.format_exc()}')

 

 

With Spanish user language enabled (it worked now; I made this test before without success):

wtallerdemadera_0-1661824962232.png

 

With English user language enabled:

wtallerdemadera_1-1661825174349.png

 

Regards,
Jorge

Message 7 of 7

kandennti
Mentor
Mentor

@BrianEkins Thanks for the reply.

 

Thank you for showing me how to use it.
I was not aware that you could not put the version in the module.

 

I have not checked everything, but for the English, I see in the xml file that the value for "translation" is empty.
I think that is the reason why it returns "defaultValue".

1.png