summaryrefslogtreecommitdiff
path: root/app/i18n/CMakeLists.txt
blob: 7e245c572e722ba636bd7ce88f8763284b134570 (plain)
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
192
193
194
195
#
# handle translation using gettext
#
project(i18n)

# List of the supported locales.
set(xtrkcad_locale_ids
	cy_GB
	de_DE
	fi
	fr_FR
	pt_BR
	ru
	)

# Misc variables
set(xtc_locale_targets)
set(potfile "${i18n_BINARY_DIR}/${XTRKCAD_PACKAGE}.pot")
set(xgtkeywords
	--keyword=_
	--keyword=p_
	--keyword=N_
)

#
# Find programs, registry is used on Windows to find the installation path

find_program(xtc_xgettext xgettext PATHS [HKEY_LOCAL_MACHINE\\SOFTWARE\\GnuWin32\\;InstallPath]/bin )
find_program(xtc_msginit msginit PATHS [HKEY_LOCAL_MACHINE\\SOFTWARE\\GnuWin32\\;InstallPath]/bin )
find_program(xtc_msgmerge msgmerge PATHS [HKEY_LOCAL_MACHINE\\SOFTWARE\\GnuWin32\\;InstallPath]/bin )
find_program(xtc_msgfmt msgfmt PATHS [HKEY_LOCAL_MACHINE\\SOFTWARE\\GnuWin32\\;InstallPath]/bin )
set(xtc_translator "" CACHE FILEPATH "Translator program for editing the .po files.")
mark_as_advanced(xtc_xgettext xtc_msginit xtc_msgmerge
                 xtc_msgfmt xtc_translator)

# 
# Extract the translatable strings from text files
#
# Custom string extractor utility
add_executable(stripmsg stripmsg.c)

# Create custom message files list
file(GLOB_RECURSE xtc_custom_msg_files
	${XTrkCAD_SOURCE_DIR}/*.xtq
	${XTrkCAD_SOURCE_DIR}/*.xtr
	${XTrkCAD_SOURCE_DIR}/*.tip)

# Extract translatable strings from custom message files to one temporary file
add_custom_target(
	custmsg
	COMMAND stripmsg ${xtc_custom_msg_files} > ${CMAKE_CURRENT_BINARY_DIR}/custmsg.h
	DEPENDS stripmsg ${xtc_custom_msg_files}
)

set_target_properties(
   stripmsg custmsg
   PROPERTIES FOLDER i18n
)

#
# Create the list of the original source files.

get_target_property(sourcefiles xtrkcad-lib SOURCES)
foreach( file ${sourcefiles})
	#
	# do not scan dependent objects / libraries for translatable strings
	string(FIND "${file}" "TARGET_OBJECTS:" result)
	if(result EQUAL -1)
	#
	# bllnhlp is generated in the build directory and has to be special cased
		string(COMPARE NOTEQUAL ${file} "bllnhlp.c" RESULT)
		if(RESULT)
			cmake_path(ABSOLUTE_PATH file BASE_DIRECTORY ${xtrkcad-lib_SOURCE_DIR} )
			cmake_path(RELATIVE_PATH file BASE_DIRECTORY ${i18n_SOURCE_DIR})
			list(APPEND xgt_source_files ${file})
		endif()
	endif()
endforeach()

#
# add file with translatable strings 
set(SOURCEFILES 
	"${xtrkcad-lib_SOURCE_DIR}/misc.c"
	"${wlib_SOURCE_DIR}/mswlib/mswmisc.c"
	"${messagefile_BINARY_DIR}/messages.h"
	"${CMAKE_CURRENT_BINARY_DIR}/custmsg.h"
	"${XTrkCAD_BINARY_DIR}/app/help/bllnhlp.c"
)

foreach( file ${SOURCEFILES})
	cmake_path(RELATIVE_PATH 
		file 
		BASE_DIRECTORY ${i18n_SOURCE_DIR}
	)
	list(APPEND xgt_source_files ${file})
endforeach()

# Extract strings and create xtrkcad.pot
add_custom_command(
	OUTPUT ${potfile}
	COMMAND ${xtc_xgettext} 
			${xgtkeywords} 
			--add-comments=i18n 
			-d ${GETTEXT_PACKAGE} 
			-F 
			-o ${potfile} 
			--from-code=ISO-8859-15 ${xgt_source_files}
	DEPENDS custmsg
	WORKING_DIRECTORY ${i18n_SOURCE_DIR}
)


#
# function to create and update the po-file for a locale

function(updatepofile locale)
	set(pofile ${i18n_SOURCE_DIR}/${locale}.po)
	set(mofile_dir ${i18n_BINARY_DIR}/${locale}/LC_MESSAGES)
	set(mofile ${mofile_dir}/${XTRKCAD_PACKAGE}.mo)

	# Merge .pot file changes to .po file
	add_custom_target(msgmerge-${locale} 
		COMMAND ${xtc_msgmerge} -U --backup=none ${pofile} ${potfile}
    	COMMAND ${xtc_msgfmt} -c -v -o ${mofile} ${pofile}
		DEPENDS messages ${pofile} 
	)

	# If new locale id was added, add .po file creation routine
	if(NOT EXISTS ${pofile})
		add_custom_command(
			OUTPUT ${pofile}
			COMMAND ${xtc_msginit} -l ${locale} -o ${pofile} -i ${potfile}
			DEPENDS ${potfile}
			)
	endif()

	file(MAKE_DIRECTORY ${mofile_dir})

	# If translator program was given, add target to edit .po files with it
	if(xtc_translator)
		add_custom_target(msgtranslate-${locale}
			COMMAND ${xtc_translator} ${pofile}
		)
	endif()
	
	# Compile .po file to binary format .mo file
	add_custom_command(
		OUTPUT ${mofile}
		COMMAND ${xtc_msgfmt} -c -v -o ${mofile} ${pofile}
		DEPENDS ${pofile}
	)

	# Install all .mo files
	install(
		FILES ${mofile}
		DESTINATION ${XTRKCAD_LOCALE_INSTALL_DIR}/${locale}/LC_MESSAGES
		)

    set_target_properties(
	    msgmerge-${locale} 
 	    PROPERTIES FOLDER i18n
    )

    set(xtc_locale_targets
		${xtc_locale_targets}
		${i18n_BINARY_DIR}/${localeid}/LC_MESSAGES/xtrkcad.mo
		PARENT_SCOPE
   	)	
endfunction()

#
# Loop through the list of the supported locales

foreach(localeid ${xtrkcad_locale_ids})
   updatepofile(${localeid})   
endforeach(localeid)


# xTrackCAD must be built before this to
# make sure that the dynamically generated files messages.h and bllnhlp.c are
# up to date.
add_custom_target(messages 
	DEPENDS xtrkcad ${potfile}
)

set_target_properties(
   messages
   PROPERTIES FOLDER i18n
)

# Utility target for building all translations 
add_custom_target(alltranslations ALL
	DEPENDS ${xtc_locale_targets}
)