Slicer / SlicerLanguagePacks

3D Slicer extension for creating, editing, and storing translations for Slicer core and extensions
MIT License
7 stars 11 forks source link

String context changes at runtime deny some translations to work #52

Closed mhdiop closed 6 months ago

mhdiop commented 8 months ago

Concerned strings (in DICOM module)

The "Segment this" option

image

The "Calculate statistics" option

This text will show up after the "Segment this..." option has been chosen.

image

Cause of the problem

First, those strings are already marked and translated. After investigation, I realized that the problem comes from a change in the string context (context at extraction time differs from the one at runtime). In fact, the __init.py__ file added in the runtime folder (see the following pictures) changes the string context from SegmentEditorSubjectHierarchyPlugin to SubjectHierarchyPlugins.SegmentEditorSubjectHierarchyPlugin. What deny Slicer from getting the translated strings.

Folder structure at extraction time

image

Folder structure at runtime

image

Possible solutions

I unfortunately don't have sufficient information about rules behind folder structure changes at runtime. However, the following solutions might work:

  1. Add a dummy __init.py__ file in the original source folder
  2. For this specific file, use translate instead of the _ function so that to directly specify the context
  3. Update the translation function

To verify my hypothesis, I implemented a quick fix that resolves the current problem, using the 3rd option. However, I don't know if it takes all possible cases since, as said earlier, I don't have sufficient information about rules behind folder structure changes at runtime.

# source file : Base/Python/slicer/i18n.py

def tr(text):
    filename = inspect.stack()[1][1]
    contextName = getContext(filename)
    translated = translate(contextName, text)
    if text == translated:
        index = contextName.find('.')
        if index != -1:
            return translate(contextName[index+1:], text)
    return translated

Obtained result

image

Thank you in advance, @lassoan and @pieper, for your suggestions.

pieper commented 8 months ago

Good research @mhdiop - I don't have a better solution off the top of my head. Perhaps @lassoan will have an idea or if not we can discuss when we meet on Wednesday.

mhdiop commented 8 months ago

Thank you @pieper for your feedback. Ok, I'll wait for Andras' answer, or for the next meeting as suggested.

Best regards.

lassoan commented 8 months ago

I've run into the same issue with the Segment Editor effects. I ended up changing the source directory structure to match the installed directory structure. Consistency of the directory structure between source and install tree is generally desirable anyway.

mhdiop commented 8 months ago

Thank you @lassoan for your feedback. I'll explore that to see if it better matches this case.

Regards.