[Blender] How to make Blender scripts multilingual

There was a bit of talk on Twitter about how Blender scripts can be multilingual, as the Blender documentation is confusing. As a result of various investigations, I managed to support multiple languages, so I will introduce it with a sample.

sample

Here are some samples that support multiple languages. This time, I simply tried to support the following two languages.

translation.py


import bpy

bl_info = {
    "name" : "Translation Test",
    "author" : "Nutti",
    "version" : (0, 1),
    "blender" : (2, 7, 0),
    "location" : "UV > Translation Test",
    "description" : "Translation Test",
    "warning" : "",
    "wiki_url" : "",
    "tracker_url" : "",
    "category" : "UV"
}

#Translation dictionary
translation_dict = {
    "en_US" :
        {("*", "Test: ") : "Test: %d"},
    "ja_JP" :
        {("*", "Test: ") : "Tesuto: %d"}
}


class TranslationTest(bpy.types.Operator):
    bl_idname = "uv.translation_test"
    bl_label = "Translation Test"
    bl_description = "Translation Test"
    bl_options = {'REGISTER', 'UNDO'}
    
    def execute(self, context):
        num = 50
        #Display translation results in notification bar
        self.report({'INFO'}, bpy.app.translations.pgettext("Test: ") % (num))
        return {'FINISHED'}


def menu_func(self, context):
    self.layout.separator()
    self.layout.operator(TranslationTest.bl_idname)


def register():
    bpy.utils.register_module(__name__)
    bpy.types.VIEW3D_MT_uv_map.append(menu_func)
    bpy.app.translations.register(__name__, translation_dict)   #Dictionary registration


def unregister():
    bpy.app.translations.unregister(__name__)   #Delete dictionary
    bpy.utils.unregister_module(__name__)
    bpy.types.VIEW3D_MT_uv_map.remove(menu_func)


if __name__ == "__main__":
    register()

How to use

Follow the steps below to install. Blender Wiki

  1. Japaneseize Blender
  2. Execute "Translation Test" from the menu displayed by pressing the'U'key in Edit mode.
  3. Confirm that the notification bar shows "Test: 50"
  4. Translate Blender into English
  5. Execute "Translation Test" from the menu displayed by pressing the'U'key in Edit mode.
  6. Confirm that "Test: 50" is displayed in the notification bar.

How to change Blender language

The following will help you to change the language of Blender. blender.jp

Sample explanation

For the basic part, see [\ Blender ] How to make a Blender plugin. This time, I will concentrate on the multilingual part.

Definition of translation dictionary

Define a dictionary for translation. The format of the dictionary is as follows.

{locale: {(context, key): translated_str, ...}, ...}

Each parameter is shown below.

Parameters meaning
locale Locale
In Japanesejp_JP, In englishen_ENIt will be.
Localeに指定する文字列は下に記載しています。
context Context to be executed(?)
For the time being**"*"**If you leave it, there will be no problem.
key Key string specified when translating
The string you want to display by defaultIt is a good idea to specify.
personallyEnglish without garbled charactersIs recommended.
translated_str The current locale islocaleSame askeyThe character string that is displayed when is specified.
In this partSpecify the character string after translationTo do.
SpecifiedlocaleWhenkeyIf does not existtranslated_The character string specified for key is displayedWill be done.

In this sample, the dictionary is defined as follows.

language display
English Test: 50
Japanese Tesuto: 50
Other Test: 50
translation_dict = {
    "en_US" :
        {("*", "Test: ") : "Test: %d"},
    "ja_JP" :
        {("*", "Test: ") : "Tesuto: %d"}
}

Tips: How to get the locale

Some people may not know what to specify, even though it is a locale. In such a case, try the following from the Python Console provided in Blender. You can find out the current locale of Blender.

>>> bpy.app.translations.locale
'en_US'

Registration of translation dictionary

To register a dictionary for translation, execute the following in the register function.

bpy.app.translations.register(__name__, translation_dict)

Perform translation

To get the translated string from the specified key, use bpy.app.translations.pgettext as follows.

bpy.app.translations.pgettext("Test: ") 

If you want to use ** string format **, use bpy.app.translations.pgettext_iface.

bpy.app.translations.pgettext("Test: ") % (num)

Delete translation dictionary

The registered translation dictionary must be released when the add-on is stopped. To unregister a dictionary, execute the following in the unregister function.

bpy.app.translations.unregister(__name__)

in conclusion

I showed you how to make Blender scripts multilingual. By making the script compatible with multiple languages, there is a high possibility that the script will be used for the supported languages. Blender is still widely used in English-speaking countries, so if you are writing scripts for Japanese, why not take this opportunity to support English as well? ** By supporting English, you may be able to get feedback and get acquainted with people from overseas **.

Reference information

Recommended Posts

[Blender] How to make Blender scripts multilingual
[Blender] How to make a Blender plugin
[Blender x Python] How to make an animation
[Blender x Python] How to make vertex animation
How to make a Japanese-English translation
How to make a slack bot
How to make a crawler --Advanced
How to make a recursive function
How to make a deadman's switch
[Blender] How to handle mouse and keyboard events in Blender scripts
How to make a crawler --Basic
How to make Word Cloud characters monochromatic
[Blender] How to set shape_key with script
How to make Selenium as light as possible
How to package and distribute Python scripts
[Cocos2d-x] How to make Script Binding (Part 2)
How to make multi-boot USB (Windows 10 compatible)
How to make a Backtrader custom indicator
How to make a Pelican site map
[Cocos2d-x] How to make Script Binding (Part 1)
[Blender x Python] How to use modifiers
How to make an embedded Linux device driver (11)
How to make WTForms TextArea correspond to file drop
How to make Spigot plugin (for Java beginners)
How to make an embedded Linux device driver (1)
How to make an embedded Linux device driver (4)
How to make multiple kernels selectable on Jupyter
How to make an embedded Linux device driver (7)
How to make scrapy JSON output into Japanese
How to make an embedded Linux device driver (3)
Bring your life to life procedurally with Blender scripts
How to make a QGIS plugin (package generation)
I read "How to make a hacking lab"
How to make an embedded Linux device driver (6)
How to make Substance Painter Python plugin (Introduction)
How to make an embedded Linux device driver (5)
How to make an embedded Linux device driver (10)
How to make Python faster for beginners [numpy]
How to make Python Interpreter changes in Pycharm
How to make Linux compatible with Japanese keyboard
How to make an embedded Linux device driver (9)
Inspired by "How to make pure functional JavaScript"
How to use xml.etree.ElementTree
How to make AWS rekognition recognize local image files
How to use virtualenv
Scraping 2 How to scrape
How to use Seaboan
[Continued] Inspired by "How to make pure functional JavaScript"
How to use image-match
How to use shogun
How to install Python
How to use Pandas 2
How to make Yubico Yubikey recognized in Manjaro Linux
How to read PyPI
How to install pip
How to use Virtualenv
How to know the current directory in Python in Blender
Explain in detail how to make sounds with python
How to use numpy.vectorize
How to update easy_install
[Blender x Python] How to create an original object