Jump to content
Ultimaker Community of 3D Printing Experts
  • Sign Up
Cuq

Python Script how to get the label in my native langage

Recommended Posts

Posted · Python Script how to get the label in my native langage

Hello,

 

I'm looking for a solution to get the translation of  a label  parameter. I can get the label or the description inthe default langage but I don't know how to get it the the right translated text.

 

        #   layer_height
        GetVal = Application.getInstance().getGlobalContainerStack().getProperty("layer_height", "value")
        GetLabel = Application.getInstance().getGlobalContainerStack().getProperty("layer_height", "label")

 

Share this post


Link to post
Share on other sites
Posted · Python Script how to get the label in my native langage

Getting the label indeed gets the untranslated one. You can translate it by passing it though the localisation catalog (i18nCatalog)


It would look a bit like this (it's untested code, so it might not work)

from UM.i18n import i18nCatalog

catalog = i18nCatalog("Cura")

untranslated_label = Application.getInstance().getGlobalContainerStack().getProperty("layer_height", "label")
translated_label = catalog.i18n(untranslated_label)

 

Share this post


Link to post
Share on other sites
Posted · Python Script how to get the label in my native langage
50 minutes ago, nallath said:

catalog = i18nCatalog("Cura")

 

That should probably be

catalog = i18nCatalog("cura")

 

Share this post


Link to post
Share on other sites
Posted (edited) · Python Script how to get the label in my native langage

Hello @nallath and @ahoeben ,

 

Thanks for your help , None of these solutions seems to work, If it's just a word I've got a translation but a complex label is not translated.

 

Tested :

from UM.i18n import i18nCatalog

catalog = i18nCatalog("cura")

untranslated_label = Application.getInstance().getGlobalContainerStack().getProperty("layer_height", "label")
translated_label = catalog.i18n(untranslated_label)

and 

 

from UM.i18n import i18nCatalog

catalog = i18nCatalog("cura")

untranslated_label = Application.getInstance().getGlobalContainerStack().getProperty("layer_height", "label")
translated_label = catalog.i18nc("@label", untranslated_label)

 

with the same result 😞

Edited by Cuq

Share this post


Link to post
Share on other sites
Posted (edited) · Python Script how to get the label in my native langage

I think I have tested every combination possible without success. but I never give up.

 

translated_label=catalog.i18n(key)
untranslated_label=stack.getProperty(key,"label")
translated_label=catalog.i18n(untranslated_label)
definition_key=key + " label" 
untranslated_label=stack.getProperty(key,"label")
translated_label=catalog.i18nc(definition_key, untranslated_label)

 

untranslated_label=stack.getProperty(key,"label")
translated_label=catalog.i18nc("@label", untranslated_label)

 

untranslated_label=stack.getProperty(key,"label").capitalize()
translated_label=catalog.i18nc("@label", untranslated_label)

 

untranslated_label=stack.getProperty(key,"label")
translated_label=catalog.i18nc("@label", key)

 

untranslated_label=stack.getProperty(key,"label")
translated_label=catalog.i18nc(key, untranslated_label)

 

 

Edited by Cuq

Share this post


Link to post
Share on other sites
Posted (edited) · Python Script how to get the label in my native langage

some help from Uranium translation Guide 

 

https://github.com/Ultimaker/Uranium/blob/master/docs/translations.md

 

 

https://github.com/Ultimaker/Uranium/blob/master/UM/i18n.py

 

    def i18nc(self, context: str, text: str, *args: Any) -> str:
        """Mark a string as translatable and provide a context for translators.
        :param context: The context of the string, i.e. something that explains
        the use of the text.
        :param text: The text to mark translatable.
        :param args: Formatting arguments. These will replace formatting elements
        in the translated string. See python ``str.format()``.
        :return: The translated text or the untranslated text if it was not found
        in this catalog.
        """

 

 

 

Edited by Cuq

Share this post


Link to post
Share on other sites
Posted (edited) · Python Script how to get the label in my native langage

YES!!!! I finaly got the solution .

 

The printing parameters are not define in the cura.po file but in fdmprinter.def.json.po  so the  catalog must be specified as

i18n_catalog = i18nCatalog("fdmprinter.def.json")

 

po_file.jpg

 

from UM.i18n import i18nCatalog

i18n_catalog = i18nCatalog("fdmprinter.def.json")

#: fdmprinter.def.json
# msgctxt "support_extruder_nr_layer_0 label"  -> Context 
# msgid "First Layer Support Extruder"  -> Key 
# msgstr "Extrudeuse de support de la première couche"  -> translated text

definition_key=key + " label"
untranslated_label=stack.getProperty(key,"label")
translated_label=i18n_catalog.i18nc(definition_key, untranslated_label)
Edited by Cuq
  • Like 2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...