I18n: Add translation contexts to properties declared from Python #107150

Merged
Bastien Montagne merged 5 commits from pioverfour/blender:dp_python_props_i18n_context into main 2023-05-22 11:38:58 +02:00

5 Commits

Author SHA1 Message Date
Damien Picard 6012acf634 Address review for !107150
buildbot/vexp-code-patch-coordinator Build done. Details
- Remove unused include.
- Do not run RNA_def_property_translation_context() if no context is
  specified.
2023-05-18 14:34:08 +02:00
Damien Picard 8ed49fb0c8 Merge branch 'main' into dp_python_props_i18n_context 2023-05-12 11:57:49 +02:00
Damien Picard 82a49ca297 Address review for !107150 2023-05-12 11:57:37 +02:00
Damien Picard e76409244b Address review for !107150
- Use "*" as default translation context in docstring.
- Do not wrap multiple var declarations on two lines.
2023-04-28 23:29:40 +02:00
Damien Picard 00fe3e60ae I18n: Add translation contexts to properties declared from Python
buildbot/vexp-code-patch-coordinator Build done. Details
Some property labels need a context to disambiguate them from others
which have the same name.

The only way to show the proper text currently for such properties is
to override it in the UI code with a translation context, like:

```python
    layout.prop(obj, "area", text="Area",
                context=i18n_contexts.amount)
```

Python properties already store a translation context though, but this
context cannot be chosen from a Python script.

For instance, typing:

```python
bpy.types.Scene.test_area = bpy.props.BoolProperty(name="Area")
print(bpy.context.scene.bl_rna.properties['test_area'].translation_context)
```

will print `*`, the default context for Python props.

This commit allows specifying a context in this manner:

```python
from bpy.app.translations import contexts as i18n_contexts
bpy.types.Scene.test_number_area = bpy.props.BoolProperty(
    name="Area", translation_context=i18n_contexts.amount
)
print(bpy.context.scene.bl_rna.properties['test_number_area'].translation_context)
```

will now print `Amount` and can be translated differently from other
labels. In this instance, the word for a surface area measurement,
instead of a UI area.

This change is aimed at Python developers who want to translate their
add-ons, rather than end-users. For this reason, the context is not
exposed in the property editing UI.
2023-04-20 19:12:07 +02:00