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.
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()
Follow the steps below to install. Blender Wiki
The following will help you to change the language of Blender. blender.jp
For the basic part, see [\ Blender ] How to make a Blender plugin. This time, I will concentrate on the multilingual part.
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 islocale Same askey The character string that is displayed when is specified.In this partSpecify the character string after translationTo do. Specified locale Whenkey If 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"}
}
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'
To register a dictionary for translation, execute the following in the register function.
bpy.app.translations.register(__name__, translation_dict)
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)
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__)
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 **.
Recommended Posts