Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve i18n support #1162

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Base/Python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set(Slicer_PYTHON_SCRIPTS
slicer/slicerqt
slicer/testing
slicer/util
slicer/i18n
freesurfer
mrml
vtkAddon
Expand Down
4 changes: 4 additions & 0 deletions Base/Python/slicer/i18n.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

def tr(context, text):
from slicer import app
return app.translate(context, text)
82 changes: 82 additions & 0 deletions CMake/RewriteTr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import ast
import errno
import getopt
import os
import sys

import astor


class RewriteTr(ast.NodeTransformer):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this function (and the whole file) do?

"""Replace tr to QT_TRANSLATE_NOOP
"""
def visit_Call(self, node):
self.generic_visit(node)
# Transform 'tr' into 'QT_TRANSLATE_NOOP' """
if (isinstance(node.func, ast.Name) and \
("tr" == node.func.id) and \
len(node.args) == 2):
call = ast.Call(func=ast.Name(id='QT_TRANSLATE_NOOP', ctx=ast.Load()),
args=node.args,
keywords=[])
ast.copy_location(call, node)
# Add lineno & col_offset to the nodes we created
ast.fix_missing_locations(call)
return call

# Return the original node if we don't want to change it.
return node


def mkdir_p(path):
"""Ensure directory ``path`` exists. If needed, parent directories
are created.
Adapted from http://stackoverflow.com/a/600612/1539918
"""
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else: # pragma: no cover
raise


def main(argv):

input_file = ''
output_file = ''
try:
opts, args = getopt.getopt(argv, "hi:o:", ["ifile=","ofile="])
except getopt.GetoptError:
print('RewriteTr.py -i <inputfile> -o <outputfile>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('RewriteTr.py -i <inputfile> -o <outputfile>')
sys.exit()
elif opt in ("-i", "--ifile"):
input_file = arg
elif opt in ("-o", "--ofile"):
output_file = arg

with open(input_file, "r") as source:
tree = ast.parse(source.read())

tree_new = RewriteTr().visit(tree)
all_lines = astor.to_source(tree_new)

# if needed, create output directory
output_dir = os.path.dirname(output_file)
#print("output_dir [%s]" % output_dir)
mkdir_p(output_dir)

# replace the single quotation marks to double quotation marks. It is necessary for lupdate
with open(output_file, "w") as destination:
for line in all_lines:
linenew = line.replace('\'', "\"")
destination.write(linenew)


if __name__ == "__main__":
main(sys.argv[1:])
9 changes: 8 additions & 1 deletion CMake/SlicerConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ set(Slicer_BUILD_EXTENSIONMANAGER_SUPPORT "@Slicer_BUILD_EXTENSIONMANAGER_SUPPOR
set(Slicer_BUILD_PARAMETERSERIALIZER_SUPPORT "@Slicer_BUILD_PARAMETERSERIALIZER_SUPPORT@")
set(Slicer_BUILD_TESTING "@BUILD_TESTING@")
set(Slicer_BUILD_WEBENGINE_SUPPORT "@Slicer_BUILD_WEBENGINE_SUPPORT@")
set(Slicer_BUILD_I18N_SUPPORT "@Slicer_BUILD_I18N_SUPPORT@")
set(Slicer_UPDATE_TRANSLATION "@Slicer_UPDATE_TRANSLATION@")

if(Slicer_BUILD_I18N_SUPPORT)
set(Slicer_LANGUAGES "@Slicer_LANGUAGES@")
endif()

set(Slicer_REQUIRED_QT_VERSION "@Slicer_REQUIRED_QT_VERSION@")
set(Slicer_REQUIRED_QT_MODULES "@Slicer_REQUIRED_QT_MODULES@")
Expand Down Expand Up @@ -348,14 +354,15 @@ if(Slicer_USE_PYTHONQT)
set(Slicer_INSTALL_QTSCRIPTEDMODULES_SHARE_DIR "@Slicer_INSTALL_QTSCRIPTEDMODULES_SHARE_DIR@")
endif()

set(Slicer_INSTALL_QM_DIR "@Slicer_INSTALL_QM_DIR@")

set(Slicer_INSTALL_THIRDPARTY_BIN_DIR "${Slicer_INSTALL_ROOT}${Slicer_BUNDLE_EXTENSIONS_LOCATION}${Slicer_THIRDPARTY_BIN_DIR}")
set(Slicer_INSTALL_THIRDPARTY_LIB_DIR "${Slicer_INSTALL_ROOT}${Slicer_BUNDLE_EXTENSIONS_LOCATION}${Slicer_THIRDPARTY_LIB_DIR}")
set(Slicer_INSTALL_THIRDPARTY_SHARE_DIR "${Slicer_INSTALL_ROOT}${Slicer_BUNDLE_EXTENSIONS_LOCATION}${Slicer_THIRDPARTY_SHARE_DIR}")

# The Slicer install prefix (*not* defined in the install tree)
set(Slicer_INSTALL_PREFIX "@CMAKE_INSTALL_PREFIX@")


# --------------------------------------------------------------------------
# Testing
# --------------------------------------------------------------------------
Expand Down
11 changes: 1 addition & 10 deletions CMake/SlicerMacroBuildApplication.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,7 @@ macro(slicerMacroBuildAppLibrary)
# Translation
# --------------------------------------------------------------------------
if(Slicer_BUILD_I18N_SUPPORT)
set(TS_DIR
"${CMAKE_CURRENT_SOURCE_DIR}/Resources/Translations/"
)
set(TS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Resources/Translations")
get_property(Slicer_LANGUAGES GLOBAL PROPERTY Slicer_LANGUAGES)

include(SlicerMacroTranslation)
Expand All @@ -187,13 +185,7 @@ macro(slicerMacroBuildAppLibrary)
TS_DIR ${TS_DIR}
TS_BASEFILENAME ${SLICERAPPLIB_NAME}
TS_LANGUAGES ${Slicer_LANGUAGES}
QM_OUTPUT_DIR_VAR QM_OUTPUT_DIR
QM_OUTPUT_FILES_VAR QM_OUTPUT_FILES
)

set_property(GLOBAL APPEND PROPERTY Slicer_QM_OUTPUT_DIRS ${QM_OUTPUT_DIR})
else()
set(QM_OUTPUT_FILES )
endif()

# --------------------------------------------------------------------------
Expand All @@ -204,7 +196,6 @@ macro(slicerMacroBuildAppLibrary)
${SLICERAPPLIB_MOC_OUTPUT}
${SLICERAPPLIB_UI_CXX}
${SLICERAPPLIB_QRC_SRCS}
${QM_OUTPUT_FILES}
)
set_target_properties(${lib_name} PROPERTIES LABELS ${lib_name})

Expand Down
11 changes: 1 addition & 10 deletions CMake/SlicerMacroBuildBaseQtLibrary.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,7 @@ macro(SlicerMacroBuildBaseQtLibrary)
# Translation
# --------------------------------------------------------------------------
if(Slicer_BUILD_I18N_SUPPORT)
set(TS_DIR
"${CMAKE_CURRENT_SOURCE_DIR}/Resources/Translations/"
)
set(TS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Resources/Translations")
get_property(Slicer_LANGUAGES GLOBAL PROPERTY Slicer_LANGUAGES)

include(SlicerMacroTranslation)
Expand All @@ -186,13 +184,7 @@ macro(SlicerMacroBuildBaseQtLibrary)
TS_DIR ${TS_DIR}
TS_BASEFILENAME ${SLICERQTBASELIB_NAME}
TS_LANGUAGES ${Slicer_LANGUAGES}
QM_OUTPUT_DIR_VAR QM_OUTPUT_DIR
QM_OUTPUT_FILES_VAR QM_OUTPUT_FILES
)

set_property(GLOBAL APPEND PROPERTY Slicer_QM_OUTPUT_DIRS ${QM_OUTPUT_DIR})
else()
set(QM_OUTPUT_FILES )
endif()

# --------------------------------------------------------------------------
Expand All @@ -203,7 +195,6 @@ macro(SlicerMacroBuildBaseQtLibrary)
${SLICERQTBASELIB_MOC_OUTPUT}
${SLICERQTBASELIB_UI_CXX}
${SLICERQTBASELIB_QRC_SRCS}
${QM_OUTPUT_FILES}
)
set_target_properties(${lib_name} PROPERTIES LABELS ${lib_name})

Expand Down
9 changes: 1 addition & 8 deletions CMake/SlicerMacroBuildLoadableModule.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ macro(slicerMacroBuildLoadableModule)
# Translation
# --------------------------------------------------------------------------
if(Slicer_BUILD_I18N_SUPPORT)
set(TS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Resources/Translations/")
set(TS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Resources/Translations")
get_property(Slicer_LANGUAGES GLOBAL PROPERTY Slicer_LANGUAGES)

include(SlicerMacroTranslation)
Expand All @@ -177,13 +177,7 @@ macro(slicerMacroBuildLoadableModule)
TS_DIR ${TS_DIR}
TS_BASEFILENAME ${LOADABLEMODULE_NAME}
TS_LANGUAGES ${Slicer_LANGUAGES}
QM_OUTPUT_DIR_VAR QM_OUTPUT_DIR
QM_OUTPUT_FILES_VAR QM_OUTPUT_FILES
)
set_property(GLOBAL APPEND PROPERTY Slicer_QM_OUTPUT_DIRS ${QM_OUTPUT_DIR})

else()
set(QM_OUTPUT_FILES )
endif()

# --------------------------------------------------------------------------
Expand All @@ -194,7 +188,6 @@ macro(slicerMacroBuildLoadableModule)
${LOADABLEMODULE_MOC_OUTPUT}
${LOADABLEMODULE_UI_CXX}
${LOADABLEMODULE_QRC_SRCS}
${QM_OUTPUT_FILES}
)

# Set loadable modules output path
Expand Down
10 changes: 2 additions & 8 deletions CMake/SlicerMacroBuildModuleWidgets.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ macro(SlicerMacroBuildModuleWidgets)
# Translation
#-----------------------------------------------------------------------------
if(Slicer_BUILD_I18N_SUPPORT)
set(TS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Resources/Translations/")
set(TS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Resources/Translations")
get_property(Slicer_LANGUAGES GLOBAL PROPERTY Slicer_LANGUAGES)

include(SlicerMacroTranslation)
Expand All @@ -93,13 +93,7 @@ macro(SlicerMacroBuildModuleWidgets)
TS_DIR ${TS_DIR}
TS_BASEFILENAME ${MODULEWIDGETS_NAME}
TS_LANGUAGES ${Slicer_LANGUAGES}
QM_OUTPUT_DIR_VAR QM_OUTPUT_DIR
QM_OUTPUT_FILES_VAR QM_OUTPUT_FILES
)
set_property(GLOBAL APPEND PROPERTY Slicer_QM_OUTPUT_DIRS ${QM_OUTPUT_DIR})

else()
set(QM_OUTPUT_FILES )
endif()

# --------------------------------------------------------------------------
Expand All @@ -110,7 +104,7 @@ macro(SlicerMacroBuildModuleWidgets)
EXPORT_DIRECTIVE ${MODULEWIDGETS_EXPORT_DIRECTIVE}
FOLDER ${MODULEWIDGETS_FOLDER}
INCLUDE_DIRECTORIES ${MODULEWIDGETS_INCLUDE_DIRECTORIES}
SRCS ${MODULEWIDGETS_SRCS} ${QM_OUTPUT_FILES}
SRCS ${MODULEWIDGETS_SRCS}
MOC_SRCS ${MODULEWIDGETS_MOC_SRCS}
UI_SRCS ${MODULEWIDGETS_UI_SRCS}
TARGET_LIBRARIES ${MODULEWIDGETS_TARGET_LIBRARIES}
Expand Down
34 changes: 33 additions & 1 deletion CMake/SlicerMacroBuildScriptedModule.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ macro(slicerMacroBuildScriptedModule)
set(_no_install_subdir_option "")
endif()

# --------------------------------------------------------------------------
# Copy and/or compile scripts and associated resources
# --------------------------------------------------------------------------
ctkMacroCompilePythonScript(
TARGET_NAME ${MY_SLICER_NAME}
SCRIPTS "${MY_SLICER_SCRIPTS}"
Expand All @@ -106,6 +109,36 @@ macro(slicerMacroBuildScriptedModule)
${_no_install_subdir_option}
)

# --------------------------------------------------------------------------
# Translations
# --------------------------------------------------------------------------

set(scripts )
foreach(file IN ITEMS ${MY_SLICER_SCRIPTS})
# Append "py" extension if needed
get_filename_component(file_ext ${file} EXT)
if(NOT "${file_ext}" MATCHES "py")
set(file "${file}.py")
endif()
list(APPEND scripts ${file})
endforeach()

set(TS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Resources/Translations")
include(SlicerMacroTranslation)
SlicerMacroTranslation(
SRCS ${scripts}
TS_DIR ${TS_DIR}
TS_BASEFILENAME ${MY_SLICER_NAME}
TS_LANGUAGES ${Slicer_LANGUAGES}
)

#if("${CTK_COMPILE_PYTHON_SCRIPTS_GLOBAL_TARGET_NAME}" STREQUAL "")
# SlicerFunctionAddPythonScriptTrFilesTargets(${MY_SLICER_NAME})
#endif()

# --------------------------------------------------------------------------
# Tests
# --------------------------------------------------------------------------
if(BUILD_TESTING AND MY_SLICER_WITH_GENERIC_TESTS)
set(_generic_unitest_scripts)
SlicerMacroConfigureGenericPythonModuleTests("${MY_SLICER_NAME}" _generic_unitest_scripts)
Expand All @@ -121,4 +154,3 @@ macro(slicerMacroBuildScriptedModule)
endif()

endmacro()

Loading