1
$\begingroup$

I would like to switch between English and Chinese translation with a shortcut (Alt+E), and have it affect interface as well as tooltips and new objects (only translating tooltips such as recommended here would not be enough in my case: Can I display two language for ui in Blender at the same time?) I hope to achieve it with this script:

import bpy
class OBJECT_OT_CustomOp(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    def execute(self, context):



#bpy.context.preferences.view.language = 'zh_CN'
bpy.context.preferences.view.language = 'en_US'

 return {'FINISHED'}

So far, I have been manually running the line with the language I wanted to use. I couldn't figure out how to assign the shortcut, since it is in the user preferences. The upper part is copied from the operator simple template. As I am new to Python, all help is appreciated, Thank you!

$\endgroup$

1 Answer 1

1
$\begingroup$

Here's a script that will add an entry to a custom operator to your preferences. By pressing ALT + E you'll swap between the two languages defined in the keymap settings, by default Chinese and English.

Your mouse will need to be over a 3D viewport editor for it to work though. If you want it to work over other editors, you'll need to add other custom keymap entries.

import bpy


class INTERFACE_OT_swap_language(bpy.types.Operator):
    bl_idname = "interface.change_language"
    bl_label = "Swap Languages"
    language_one: bpy.props.StringProperty(default="en_US")
    language_two: bpy.props.StringProperty(default="zh_CN")

    def execute(self, context):
        view = context.preferences.view
        if view.language == self.language_one:
            view.language = self.language_two
        else:
            view.language = self.language_one
        return {'FINISHED'}


def register():
    bpy.utils.register_class(INTERFACE_OT_swap_language)

    wm = bpy.context.window_manager
    kc = wm.keyconfigs.user
    if kc:
        km = kc.keymaps.new(name='3D View', space_type='VIEW_3D')
        kmi = km.keymap_items.new(INTERFACE_OT_swap_language.bl_idname, type='E', value='PRESS', alt=True)


if __name__ == "__main__":
    register()

Note you can tweak these defaults by going into the preferences, looking for "Swap Languages" in the keymap search field and expanding the entry.

enter image description here

Here I've modified the second language property to swap to french for instance.

enter image description here

Sources :

https://blender.stackexchange.com/a/196518/86891

https://docs.blender.org/api/current/bpy.types.KeyConfig.html#bpy.types.KeyConfig

$\endgroup$
2
  • $\begingroup$ I have tried the script and it works, thank you! But what do I need to do to have it active by default? right now I need to execute it once in order for it to become active. If I create a keymap, export it and try to load it, I get this error message: ` Failed to load keymap 'C:\\Users\\elsch\\AppData\\Roaming\\Blender Foundation\\Blender\\3.3\\scripts\\presets\\keyconfig\\Chinese_English_Toggle.py' ` How do I save it correctly to have it start up automatically? Thanks! $\endgroup$ Commented Oct 11, 2022 at 12:49
  • $\begingroup$ @DavidSchulz you simply need to add a few lines to the script and then register the addon from the preferences > Addon > Install > yourfile.py see docs.blender.org/manual/en/latest/advanced/scripting/… for the specifics $\endgroup$
    – Gorgious
    Commented Oct 12, 2022 at 5:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .