bl_i18n_utils - bugs saving translations to py files and finding po strings if context is not present in the file #116934

Merged
Bastien Montagne merged 6 commits from Andrej730/blender:bl_i18n_utils_bugs into main 2024-02-06 16:58:58 +01:00
Contributor

4 changes in this PR:

  1. Fixed bug with saving translations to py files - saving sources and comments it didn't added repr for the saved strings which was producing unexpected special characters:

image

Then those special characters sometimes made translations.py invalid (e.g., it was failing to load with some \u combinations) or was producing invalid characters in .po files created from the translations.py.

image

  1. find_best_messages_matches used to fail when current message context wasn't found in the .po file. E.g. there were no messages with context "View3D", "Operator", ... and you would try to edit translation for the element that uses that context. I've added a fallback to empty sets to make sure it won't fail and just return the valid result that there are no matches.

  2. added minor typing hint for easier code navigation.

  3. added print to make it clear what data is printed.

4 changes in this PR: 1) Fixed bug with saving translations to py files - saving sources and comments it didn't added `repr` for the saved strings which was producing unexpected special characters: ![image](/attachments/3c31cb27-0a0c-4360-9aeb-bd8418fbd95b) Then those special characters sometimes made `translations.py` invalid (e.g., it was failing to load with some `\u` combinations) or was producing invalid characters in .po files created from the `translations.py`. ![image](/attachments/ccbbaf88-12c3-4c9e-ad52-f5ae1a8077d1) 2) `find_best_messages_matches` used to fail when current message context wasn't found in the .po file. E.g. there were no messages with context "View3D", "Operator", ... and you would try to edit translation for the element that uses that context. I've added a fallback to empty sets to make sure it won't fail and just return the valid result that there are no matches. 3) added minor typing hint for easier code navigation. 4) added print to make it clear what data is printed.
Andrej added 4 commits 2024-01-09 12:20:08 +01:00
ffd2048daa find_best_messages_matches not to fail if context is not present in .po
It was failing when current message context wasn't found in the .po file. E.g. there were not messages with context "View3D", "Operator", ... and you would try to edit translation for the element that uses that context.

Traceback
```
Error: Python: Traceback (most recent call last):
File "\Blender\4.0\scripts\addons\ui_translate\edit_translation.py", line 287, in invoke
msgs.find_best_messages_matches(self, self.msgmap, self.rna_ctxt, self.rna_struct, self.rna_prop, self.rna_enum)
File "\Blender\4.0\scripts\modules\bl_i18n_utils\utils.py", line 798, in find_best_messages_matches
k = ctxt_to_msg[rna_ctxt].copy()
KeyError: 'Operator'
Andrej requested review from Bastien Montagne 2024-01-09 12:21:30 +01:00
Andrej requested review from Campbell Barton 2024-01-09 12:21:37 +01:00
Bastien Montagne requested changes 2024-01-10 17:43:40 +01:00
Bastien Montagne left a comment
Owner

Generally looks great, thanks for the patch (and sorry for the review delay).

Just noted a couple of details below.

Generally looks great, thanks for the patch (and sorry for the review delay). Just noted a couple of details below.
@ -782,3 +783,3 @@
if elbl:
# Enum items' labels have no i18n context...
k = ctxt_to_msg[self.settings.DEFAULT_CONTEXT].copy()
k = ctxt_to_msg.get(self.settings.DEFAULT_CONTEXT, set()).copy()

I would rather ensure that self.settings.DEFAULT_CONTEXT is always present in ctxt_to_msg (i.e. add it systematically with an empty set as value in invalidate_reverse_cache).

That way there is only needs for a default argument top get in one place.

I would rather ensure that `self.settings.DEFAULT_CONTEXT` is always present in `ctxt_to_msg` (i.e. add it systematically with an empty `set` as value in `invalidate_reverse_cache`). That way there is only needs for a default argument top `get` in one place.
Author
Contributor

Makes sense, it will be much more clean that way.
Resolved it in 4382d70808 👍

Makes sense, it will be much more clean that way. Resolved it in 4382d708087963b8677606b6caf458a81d94f241 👍
Andrej730 marked this conversation as resolved
@ -1250,3 +1251,3 @@
def __init__(self, kind=None, src=None, langs=set(), settings=settings):
self.settings = settings
self.trans = {}
self.trans: Dict[str, I18nMessages] = {}

I'd rather have a comment than this type hint, not a big fan of these (at all).

I'd rather have a comment than this type hint, not a big fan of these (at all).
Author
Contributor

Are you sure? The idea was that IDE would identify self.trans values as I18nMessages and it would be easier to navigate to the methods and attributes of those instances in for-loops over self.trans.

Are you sure? The idea was that IDE would identify `self.trans` values as `I18nMessages` and it would be easier to navigate to the methods and attributes of those instances in for-loops over `self.trans`. ![](https://i.imgur.com/0aic9WP.png)

I just don't like this syntax... Feels just as horrible as C++ templates. I guess since I do not use a real IDE for python, I don't get that help either.

If you really want to keep it, I can live with it though, I know that I'm on the dinosaurs side on this topic.

I just don't like this syntax... Feels just as horrible as C++ templates. I guess since I do not use a real IDE for python, I don't get that help either. If you really want to keep it, I can live with it though, I know that I'm on the dinosaurs side on this topic.
Andrej730 marked this conversation as resolved

(I assume this is a fix for #116579 ?)

(I assume this is a fix for #116579 ?)
Author
Contributor

(I assume this is a fix for #116579 ?)

No, it's a separate issue.

> (I assume this is a fix for `#116579` ?) No, it's a separate issue.
Andrej added 1 commit 2024-01-10 19:01:08 +01:00
Bastien Montagne approved these changes 2024-01-11 12:35:51 +01:00

@pioverfour FYI since you also work a lot on this tool these days, and in case you want to check. For me it's good to go.

@pioverfour FYI since you also work a lot on this tool these days, and in case you want to check. For me it's good to go.
Member

FYI since you also work a lot on this tool these days, and in case you want to check. For me it's good to go.

This change makes sense at a glance, I’ll test it tomorrow.

> FYI since you also work a lot on this tool these days, and in case you want to check. For me it's good to go. This change makes sense at a glance, I’ll test it tomorrow.
Member
  1. The fix works well, but I don’t really like seeing single quotes when double quotes are used everywhere else, and are the convention for Blender. Also this would make existing translations change quote format — though arguably very few people use this system anyway!
    Even without this quote issue, I think a better fix would be to normalize file paths to use POSIX separators ("/" instead of "\"). That way, if developers using Windows and Unix-like systems work on the same add-on, they don’t have to bother fixing the paths.

@Andrej730 This should work but I can’t test it on Windows, let me know what you think:

diff --git a/scripts/modules/bl_i18n_utils/bl_extract_messages.py b/scripts/modules/bl_i18n_utils/bl_extract_messages.py
index b49401c6d53..334ac907b5b 100644
--- a/scripts/modules/bl_i18n_utils/bl_extract_messages.py
+++ b/scripts/modules/bl_i18n_utils/bl_extract_messages.py
@@ -665,6 +665,9 @@ def dump_py_messages_from_files(msgs, reports, files, settings):
 
         fp_rel = make_rel(fp)
 
+        from pathlib import PurePath
+        fp_rel = PurePath(fp_rel).as_posix()
+
         for node in ast.walk(root_node):
             if type(node) == ast.Call:
                 # ~ print("found function at")
  1. I’m not really fond of the typing syntax and I don’t use a type-aware IDE, but I don’t mind it that much either so that’s fine with me.

The rest is nice :)

1. The fix works well, but I don’t really like seeing single quotes when double quotes are used everywhere else, and are the convention for Blender. Also this would make existing translations change quote format — though arguably very few people use this system anyway! Even without this quote issue, I think a better fix would be to normalize file paths to use POSIX separators ("/" instead of "\\"). That way, if developers using Windows and Unix-like systems work on the same add-on, they don’t have to bother fixing the paths. @Andrej730 This should work but I can’t test it on Windows, let me know what you think: ```diff diff --git a/scripts/modules/bl_i18n_utils/bl_extract_messages.py b/scripts/modules/bl_i18n_utils/bl_extract_messages.py index b49401c6d53..334ac907b5b 100644 --- a/scripts/modules/bl_i18n_utils/bl_extract_messages.py +++ b/scripts/modules/bl_i18n_utils/bl_extract_messages.py @@ -665,6 +665,9 @@ def dump_py_messages_from_files(msgs, reports, files, settings): fp_rel = make_rel(fp) + from pathlib import PurePath + fp_rel = PurePath(fp_rel).as_posix() + for node in ast.walk(root_node): if type(node) == ast.Call: # ~ print("found function at") ``` 3. I’m not really fond of the typing syntax and I don’t use a type-aware IDE, but I don’t mind it that much either so that’s fine with me. The rest is nice :)
Andrej added 1 commit 2024-01-14 10:20:26 +01:00
Author
Contributor

@pioverfour makes complete sense, that's much more direct fix, tested it! And it would also allow (in theory) different users to generate exactly the same translations.py, even if they use different systems which is pretty cool (and would be a problem I'd meet sooner or later 😅).

@pioverfour makes complete sense, that's much more direct fix, tested it! And it would also allow (in theory) different users to generate exactly the same `translations.py`, even if they use different systems which is pretty cool (and would be a problem I'd meet sooner or later 😅).
Author
Contributor

@mont29 Hi! Does this PR needs more approves or it's ready to merge?

@mont29 Hi! Does this PR needs more approves or it's ready to merge?
Bastien Montagne requested review from Damien Picard 2024-01-25 17:38:53 +01:00

@Andrej730 Sorry for the delay! Maybe @pioverfour wants to do another final check before this gets merged?

@Andrej730 Sorry for the delay! Maybe @pioverfour wants to do another final check before this gets merged?
Member

Looks good to me!

Looks good to me!
Damien Picard approved these changes 2024-02-03 14:57:46 +01:00
Bastien Montagne approved these changes 2024-02-06 16:57:01 +01:00
Bastien Montagne merged commit a8e14af167 into main 2024-02-06 16:58:58 +01:00
Bastien Montagne deleted branch bl_i18n_utils_bugs 2024-02-06 16:59:01 +01: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
3 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#116934
No description provided.