This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.cmake
191 lines (158 loc) · 6.2 KB
/
translate.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# enable/disable cmake debug messages related to this pile
set (TRANSLATE_DEBUG_MSG OFF)
# make sure support code is present; no harm
# in including it twice; the user, however, should have used
# pileInclude() from pile_support.cmake module.
include(pile_support)
# initialize this module
macro (translateInit
ref_cnt_use_mode)
find_path(SQLITE3_INCLUDE_DIR
NAMES sqlite3.h
PREFIX sqlite
HINTS ${SQLITE3_INCLUDE_DIR})
# message(STATUS "SQLITE3_INCLUDE_DIR = ${SQLITE3_INCLUDE_DIR}")
if (SQLITE3_INCLUDE_DIR)
set (SQLITE3_FOUND ON)
else()
set (SQLITE3_FOUND OFF)
endif()
# default name
if (NOT TRANSLATE_INIT_NAME)
set(TRANSLATE_INIT_NAME "translate")
endif ()
# compose the list of headers and sources
set(TRANSLATE_HEADERS
"translang.h"
"translate.h")
set(TRANSLATE_SOURCES
"translate_sqlite.cc"
"translang.cc"
"translate.cc")
set(TRANSLATE_QT_MODS
"Core"
)
pileSetSources(
"${TRANSLATE_INIT_NAME}"
"${TRANSLATE_HEADERS}"
"${TRANSLATE_SOURCES}")
pileSetCommon(
"${TRANSLATE_INIT_NAME}"
"0;0;1;d"
"ON"
"${ref_cnt_use_mode}"
""
"category1"
"tag1;tag2")
endmacro ()
# Creates translation files. Should be run in internationalization directory.
#
# A custom target is created - translations_update - that scans source files
# for strings to translate. A .ts file is created for each language if it does
# not exist already and corresponding .qm file is compiled and installed.
#
# Each language is represented by a directory (name not important but should
# be meaningful) that consists of the .ts file, an icon.png file and
# a metadata.ini file.
#
macro (internationalizationDirectory)
find_package(Qt5 REQUIRED COMPONENTS LinguistTools)
# prevent source files from being deleted on make clean
set_directory_properties(PROPERTIES CLEAN_NO_CUSTOM 1)
# get the list of subdirectories
unset(lang_dirs)
pileSubDirList(lang_dirs "${CMAKE_CURRENT_SOURCE_DIR}")
# iterate and collect translations
unset(lang_ts_files)
foreach(lang_dir ${lang_dirs})
unset(contents)
unset(language_label)
unset(language_icon)
unset(language_locale)
unset(language_translate)
unset(language_additional)
unset(content_parts)
unset(content_part_1)
unset(content_part_2)
unset(lang_qm_file)
unset(lang_meta_file)
unset(lang_files_to_install)
set (lang_meta_file "${CMAKE_CURRENT_SOURCE_DIR}/${lang_dir}/metadata.ini")
file(READ "${lang_meta_file}" contents)
string(REGEX REPLACE ";" "\\\\;" contents "${contents}")
string(REGEX REPLACE "\n" ";" contents "${contents}")
# iterate in file's lines and extract information in variables
foreach(content_line_iter ${contents})
string(REGEX REPLACE ";" "\\\\;" content_line "${content_line_iter}")
unset(content_parts)
string(REGEX REPLACE "=" ";" content_parts "${content_line}")
unset(content_part_1)
unset(content_part_2)
unset(content_parts_count)
list(LENGTH content_parts content_parts_count)
list(GET content_parts 0 content_part_1)
if (content_parts_count GREATER "1")
list(GET content_parts 1 content_part_2)
endif()
if (content_part_1 MATCHES "^label$")
set(language_label ${content_part_2} )
elseif (content_part_1 MATCHES "^icon$")
set(language_icon ${content_part_2} )
elseif (content_part_1 MATCHES "^translate$")
set(language_translate ${content_part_2} )
elseif (content_part_1 MATCHES "^locale$")
set(language_locale ${content_part_2} )
elseif (content_part_1 MATCHES "^additional$")
# can't use directly ; as it represents the comment marker in .ini files
string(REGEX REPLACE ":" ";" language_additional "${content_part_2}")
endif()
endforeach()
unset(install_destin)
if(APPLE AND UNIX) ## OSX
set(install_destin "${PROJECT_NAME}.app/Contents/Resources/translations/${lang_dir}")
else()
set(install_destin "share/locale/${PROJECT_NAME_UNIX}/${lang_dir}")
endif()
# the translation for Qt for this locale
unset(qt_locale_file)
file(GLOB qt_locale_file "${PILE_QT_ROOT}/translations/qt_${language_locale}.qm")
if (qt_locale_file)
list(APPEND lang_files_to_install "${qt_locale_file}")
endif()
# metadata file
list(APPEND lang_files_to_install "${lang_meta_file}")
if (language_translate)
unset(local_ts_file)
set(local_ts_file "${CMAKE_CURRENT_SOURCE_DIR}/${lang_dir}/${language_translate}.ts")
list(APPEND lang_ts_files "${local_ts_file}")
unset(lang_qm_file)
set(lang_qm_file "${CMAKE_CURRENT_BINARY_DIR}/${language_translate}.qm")
list(APPEND lang_files_to_install "${lang_qm_file}")
endif()
if (language_icon)
list(APPEND lang_files_to_install "${CMAKE_CURRENT_SOURCE_DIR}/${lang_dir}/${language_icon}")
endif()
foreach(additional ${language_additional})
list(APPEND lang_files_to_install "${CMAKE_CURRENT_SOURCE_DIR}/${lang_dir}/${additional}")
endforeach()
# install all these files
install(
FILES
${lang_files_to_install}
DESTINATION ${install_destin}
COMPONENT languages)
endforeach()
unset(existing_lang_ts_files)
file(GLOB_RECURSE existing_lang_ts_files "*.ts")
list(APPEND lang_ts_files ${existing_lang_ts_files})
list(REMOVE_DUPLICATES lang_ts_files)
qt5_create_translation(
lang_qm_files
${PROJECT_SOURCE_DIR}
${lang_ts_files})
# qt5_add_translation(TRANSLATION_QM ${lang_qm_files})
# message(STATUS "---->> TRANSLATION_QM = ${TRANSLATION_QM}")
add_custom_target(
translate
DEPENDS ${lang_qm_files})
endmacro ()