Proper handling of UI messages for translation (i18n) in CPP code. #92758

Closed
opened 2021-11-02 16:27:05 +01:00 by Bastien Montagne · 11 comments

'Generated' strings (e.g. using C-style printf strings) need to be known (as much as possible) from translators as a unique, complete single item, not as chunks of strings patched together when building the final UI message.

E.g (from AssetCatalogDropController::drop_tooltip_asset_catalog in asset_catalog_tree_view.cc):

return std::string(TIP_("Move Catalog")) + " '" + src_catalog->path.name() + "' " + TIP_("into") + " '" + catalog_item_.get_name() + "'";

This will generate two completely unrelated messages "Move Catalog" and "into".
Move Catalog could very likely also be an operator name, or a button label, that would require a different translation.
into might have ten different ways to be translated in other languages, how do they choose? What if many places use that single into message?

A C version could be e.g. (pseud-code obviously):

return BLI_sprintfN(TIP_("Move Catalog '%s' into '%s'"), src_catalog->path.name(), catalog_item_.get_name());

This will generate a full specific message "Move Catalog '%s' into '%s'".


Note that this is not only an issue of disambiguation, it is also a critical point for many non-latin languages (RTL languages, languages using alternative signs to quote, etc.).

'Generated' strings (e.g. using C-style `printf` strings) need to be known (as much as possible) from translators as a unique, complete single item, not as chunks of strings patched together when building the final UI message. E.g (from `AssetCatalogDropController::drop_tooltip_asset_catalog` in asset_catalog_tree_view.cc): ```CPP return std::string(TIP_("Move Catalog")) + " '" + src_catalog->path.name() + "' " + TIP_("into") + " '" + catalog_item_.get_name() + "'"; ``` This will generate two completely unrelated messages `"Move Catalog"` and `"into"`. `Move Catalog` could very likely also be an operator name, or a button label, that would require a different translation. `into` might have ten different ways to be translated in other languages, how do they choose? What if many places use that single `into` message? A C version could be e.g. (pseud-code obviously): ```C return BLI_sprintfN(TIP_("Move Catalog '%s' into '%s'"), src_catalog->path.name(), catalog_item_.get_name()); ``` This will generate a full specific message `"Move Catalog '%s' into '%s'"`. ------------------------- Note that this is not only an issue of disambiguation, it is also a critical point for many non-latin languages (RTL languages, languages using alternative signs to quote, etc.).
Author
Owner

Changed status from 'Needs Triage' to: 'Confirmed'

Changed status from 'Needs Triage' to: 'Confirmed'
Author
Owner

Added subscriber: @mont29

Added subscriber: @mont29
Author
Owner

For now, I would suggest that Cpp code uses standard C printf-like functions to build their custom/dynamic UI messages?

For now, I would suggest that Cpp code uses standard C `printf`-like functions to build their custom/dynamic UI messages?
Author
Owner

Added subscriber: @JulianEisel

Added subscriber: @JulianEisel
Member

Added subscriber: @HooglyBoogly

Added subscriber: @HooglyBoogly
Member

The issue with BLI_sprintfN is that it returns an allocated string output, meaning no small size optimization, and requires calling free later on.
Using std::string is so much nicer than that, because of RAII, etc.

Maybe a better solution would be a string format function with a std::string output.

The issue with `BLI_sprintfN` is that it returns an allocated string output, meaning no small size optimization, and requires calling free later on. Using `std::string` is so much nicer than that, because of RAII, etc. Maybe a better solution would be a string format function with a `std::string` output.

Added subscriber: @GeorgiaPacific

Added subscriber: @GeorgiaPacific
Member

Added subscriber: @pioverfour

Added subscriber: @pioverfour
Member

For the record, after trying to mitigate some string issues in D15996, a talk with @HooglyBoogly hinted at the possibility to replace BLI_sprintfN() with fmt::format(), which is already used in Blender and could be generalized to more C++ code for this purpose.

This library has the extra advantage that format arguments can be numbered, and reordered in translations if needed.

For the record, after trying to mitigate some string issues in [D15996](https://archive.blender.org/developer/D15996), a talk with @HooglyBoogly hinted at the possibility to replace `BLI_sprintfN()` with `fmt::format()`, which is already used in Blender and could be generalized to more C++ code for this purpose. This library has the extra advantage that format arguments can be numbered, and reordered in translations if needed.
Bastien Montagne added this to the User Interface project 2023-02-09 15:34:10 +01:00
Philipp Oeser removed the
Interest
User Interface
label 2023-02-10 09:23:05 +01:00
Member

fmt::format() has now been used multiple times without any issue AFAIK, I believe this issue can be closed.

`fmt::format()` has now been used multiple times without any issue AFAIK, I believe this issue can be closed.
Author
Owner

Indeed!

Indeed!
Blender Bot added
Status
Archived
and removed
Status
Confirmed
labels 2023-06-27 18:49:42 +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#92758
No description provided.