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
Member

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:

    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:

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:

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 is what translated properties look like using the existing ("Area", "") and ("Area", "Amount") messages:
python_prop_contexts_test.png

The panel can be generated with this script:
python_prop_contexts_test.py

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 is what translated properties look like using the existing ("Area", "") and ("Area", "Amount") messages: ![python_prop_contexts_test.png](/attachments/b0d9737e-4b31-4c91-a08e-b347db31225f) The panel can be generated with this script: [python_prop_contexts_test.py](/attachments/ab613cdc-8eba-46bc-8f3c-ad0a97e7a6e5)
Damien Picard added the
Module
Python API
Interest
Translations
labels 2023-04-20 01:08:33 +02:00
Damien Picard requested review from Campbell Barton 2023-04-20 01:09:28 +02:00
Damien Picard requested review from Bastien Montagne 2023-04-20 01:10:44 +02:00
Damien Picard force-pushed dp_python_props_i18n_context from 63616087ec to 00fe3e60ae 2023-04-20 19:12:20 +02:00 Compare
Bastien Montagne requested changes 2023-04-28 18:37:43 +02:00
Bastien Montagne left a comment
Owner

Feature makes sense to me, and code looks good. Comments below are mainly for style/formatting issues.

Feature makes sense to me, and code looks good. Comments below are mainly for style/formatting issues.
@ -2739,6 +2745,7 @@ static int bpy_struct_id_used(StructRNA *srna, char *identifier)
PyDoc_STRVAR(BPy_BoolProperty_doc,
".. function:: BoolProperty(name=\"\", "
"description=\"\", "
"translation_context=\"\", "

Should be "translation_context=\"*\", " I think?

Same in other docstrings below of course.

Should be `"translation_context=\"*\", "` I think? Same in other docstrings below of course.
pioverfour marked this conversation as resolved
@ -2769,2 +2776,3 @@
};
const char *name = NULL, *description = "";
const char *name = NULL, *description = "",
*translation_context = BLT_I18NCONTEXT_DEFAULT_BPYRNA;

avoid wrapping multiple variables declarations over more than one line. Better to keep it on one line, and repeat the type for the next line.

Same for the other similar cases below of course.

avoid wrapping multiple variables declarations over more than one line. Better to keep it on one line, and repeat the type for the next line. Same for the other similar cases below of course.
pioverfour marked this conversation as resolved
@ -3305,3 +3316,1 @@
"get",
"set",
NULL,
"attr", "name", "description", "translation_context",

Please keep existing format ;)

Please keep existing format ;)
Author
Member

Please keep existing format ;)

I followed the advice here, and the formatting is from ClangFormat.

Should I still disable automatic formatting and revert the change? If so, should I reformat the entire file in a separate commit so others don’t have to do it?

> Please keep existing format ;) I followed the advice [here](https://wiki.blender.org/wiki/Tools/ClangFormat#IDE_Integration), and the formatting is from ClangFormat. Should I still disable automatic formatting and revert the change? If so, should I reformat the entire file in a separate commit so others don’t have to do it?
@ -3438,2 +3455,2 @@
"\n" BPY_PROPDEF_NAME_DOC BPY_PROPDEF_DESC_DOC BPY_PROPDEF_NUM_MIN_DOC
" :type min: float\n" BPY_PROPDEF_NUM_MAX_DOC
"\n" BPY_PROPDEF_NAME_DOC BPY_PROPDEF_DESC_DOC BPY_PROPDEF_CTXT_DOC
BPY_PROPDEF_NUM_MIN_DOC " :type min: float\n" BPY_PROPDEF_NUM_MAX_DOC

broken format, should be on two different lines.

broken format, should be on two different lines.
Author
Member

broken format, should be on two different lines.

This is coming from ClangFormat as well. Even if I temporarily disable formatting on save, it will still happen as soon as someone formats the codebase. Not sure how to avoid it here…

> broken format, should be on two different lines. This is coming from ClangFormat as well. Even if I temporarily disable formatting on save, it will still happen as soon as someone formats the codebase. Not sure how to avoid it here…

@blender-bot build

@blender-bot build
@blender-bot ping
Member

pong

pong

@blender-bot build

@blender-bot build
Damien Picard added 1 commit 2023-04-28 23:31:46 +02:00
e76409244b Address review for !107150
- Use "*" as default translation context in docstring.
- Do not wrap multiple var declarations on two lines.
Damien Picard requested review from Bastien Montagne 2023-05-07 12:19:45 +02:00
Campbell Barton approved these changes 2023-05-12 03:31:11 +02:00
Campbell Barton left a comment
Owner

Minor request, otherwise LGTM.

NOTE: it would be good to list known translation contexts as part of our API docs, the translation_context RNA argument can then point to this too, that seems outside the scope of this patch though.

Minor request, otherwise LGTM. NOTE: it would be good to list known translation contexts as part of our API docs, the `translation_context` RNA argument can then point to this too, that seems outside the scope of this patch though.
@ -4388,6 +4438,7 @@ PyObject *BPy_CollectionProperty(PyObject *self, PyObject *args, PyObject *kw)
.srna = srna,
};
const char *name = NULL, *description = "";
const char *translation_context = BLT_I18NCONTEXT_DEFAULT_BPYRNA;

Set to NULL, there is no need to call RNA_def_property_translation_context if it's not set as BLT_I18NCONTEXT_DEFAULT_BPYRNA is already the default. Same for all other cases.

Set to NULL, there is no need to call `RNA_def_property_translation_context` if it's not set as BLT_I18NCONTEXT_DEFAULT_BPYRNA is already the default. Same for all other cases.
pioverfour marked this conversation as resolved
Damien Picard added 2 commits 2023-05-12 12:16:03 +02:00
Campbell Barton reviewed 2023-05-18 07:14:39 +02:00
@ -18,6 +18,8 @@
#include "BLI_listbase.h"
#include "BLI_utildefines.h"
#include "BLT_translation.h"

This is no longer needed.

This is no longer needed.
Campbell Barton reviewed 2023-05-18 07:15:23 +02:00
@ -4482,6 +4536,7 @@ PyObject *BPy_CollectionProperty(PyObject *self, PyObject *args, PyObject *kw)
}
prop = RNA_def_collection_runtime(
srna, id_data.value, ptype, name ? name : id_data.value, description);
RNA_def_property_translation_context(prop, translation_context);

These functions don't need to run when the translation_context is NULL.

These functions don't need to run when the `translation_context` is NULL.
Damien Picard added 1 commit 2023-05-18 14:36:14 +02:00
buildbot/vexp-code-patch-coordinator Build done. Details
6012acf634
Address review for !107150
- Remove unused include.
- Do not run RNA_def_property_translation_context() if no context is
  specified.
Bastien Montagne approved these changes 2023-05-22 11:09:00 +02:00

@blender-bot build

@blender-bot build
Bastien Montagne merged commit 2decfe4f00 into main 2023-05-22 11:38:58 +02:00
Bastien Montagne deleted branch dp_python_props_i18n_context 2023-05-22 11:38:59 +02:00
Howard Trickey referenced this issue from a commit 2023-05-29 02:51:38 +02:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
4 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#107150
No description provided.