summaryrefslogtreecommitdiff
path: root/app/i18n/CMakeLists.txt
blob: c3d356316c4eb1df02a1f6ed4e5de259f7202812 (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
PROJECT(i18n)

# List of the supported locales.
SET(XTRKCAD_LOCALE_IDS
	de_DE
	fi
	pt_BR
	)

# Misc variables
SET(XTC_LOCALE_TARGETS )
SET(POTFILE "${i18n_BINARY_DIR}/${XTRKCAD_PACKAGE}.pot")
SET(XGTKEYWORDS
	--keyword=_
	--keyword=p_
	--keyword=N_)

# Programs
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)

# Create the list of the original source files.
FILE(GLOB_RECURSE XGT_SOURCE_FILES
	RELATIVE ${i18n_SOURCE_DIR}
	${XTrkCAD_SOURCE_DIR}/*.c
	${XTrkCAD_SOURCE_DIR}/*.h)

# Dynamically generated files, such as messages.h and bllnhlp.c, must be added
# to the list manually to make sure that everything works properly.
# "FILE(RELATIVE_PATH ..." is used to get rid of the absolute paths like
# "/home/my_funny_username/very_long_path_to_my_projects/source_file.c"
# in the .pot and .po files.
FILE(RELATIVE_PATH TEMP_FILENAME ${i18n_SOURCE_DIR} "${help_BINARY_DIR}/messages.h")
LIST(APPEND XGT_SOURCE_FILES ${TEMP_FILENAME})
FILE(RELATIVE_PATH TEMP_FILENAME ${i18n_SOURCE_DIR} "${XTrkCAD_BINARY_DIR}/app/bin/bllnhlp.c")
LIST(APPEND XGT_SOURCE_FILES ${TEMP_FILENAME})
FILE(RELATIVE_PATH TEMP_FILENAME ${i18n_SOURCE_DIR} "${CMAKE_CURRENT_BINARY_DIR}/custmsg.h")
LIST(APPEND XGT_SOURCE_FILES ${TEMP_FILENAME})

# Extract strings and create xtrkcad.pot
ADD_CUSTOM_COMMAND(
	OUTPUT ${POTFILE}
	COMMAND ${XTC_XGETTEXT} ${XGTKEYWORDS} -d ${GETTEXT_PACKAGE} -F -o ${POTFILE} --from-code=ISO-8859-15 ${XGT_SOURCE_FILES}
	DEPENDS xtrkcad ${CMAKE_CURRENT_BINARY_DIR}/custmsg.h
	WORKING_DIRECTORY ${i18n_SOURCE_DIR}
	)

# Loop through the list of the supported locales
FOREACH(localeid ${XTRKCAD_LOCALE_IDS})
	SET(POFILE ${i18n_SOURCE_DIR}/${localeid}.po)
	SET(MOFILE_DIR ${i18n_BINARY_DIR}/${localeid}/LC_MESSAGES)
	SET(MOFILE ${MOFILE_DIR}/${XTRKCAD_PACKAGE}.mo)

	# Merge .pot file changes to .po file
	ADD_CUSTOM_TARGET(msgmerge-${localeid}
		COMMAND ${XTC_MSGMERGE} -U --backup=none ${POFILE} ${POTFILE}
		DEPENDS ${POTFILE} ${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 ${localeid} -o ${POFILE} -i ${POTFILE}
			DEPENDS ${POTFILE}
			)
	ENDIF(NOT EXISTS ${POFILE})

	FILE(MAKE_DIRECTORY ${MOFILE_DIR})

	# Compile .po file to binary format .mo file
	ADD_CUSTOM_COMMAND(
		OUTPUT ${MOFILE}
		COMMAND ${XTC_MSGFMT} -c -v -o ${MOFILE} ${POFILE}
		DEPENDS ${POFILE}
		)

	# If translator program was given, add target to edit .po files with it
	IF(XTC_TRANSLATOR)
		ADD_CUSTOM_TARGET(msgtranslate-${localeid}
			COMMAND ${XTC_TRANSLATOR} ${POFILE}
			)
	ENDIF(XTC_TRANSLATOR)

	# Install all .mo files
	INSTALL(
		FILES ${MOFILE}
		DESTINATION ${XTRKCAD_LOCALE_INSTALL_DIR}/${localeid}/LC_MESSAGES
		)

	SET(XTC_LOCALE_TARGETS
		${XTC_LOCALE_TARGETS}
		${i18n_BINARY_DIR}/${localeid}/LC_MESSAGES/xtrkcad.mo
		)

ENDFOREACH(localeid)

# Target to be executed with "make all". Xtrkcad 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 ALL
	DEPENDS xtrkcad ${POTFILE} ${XTC_LOCALE_TARGETS}
	)

# Custom message files list
FILE(GLOB_RECURSE XTC_CUSTOM_MSG_FILES
	${XTrkCAD_SOURCE_DIR}/*.xtq
	${XTrkCAD_SOURCE_DIR}/*.xtr
	${XTrkCAD_SOURCE_DIR}/*.tip)

# Custom string extractor utility
ADD_EXECUTABLE(stripmsg stripmsg.c)
GET_TARGET_PROPERTY(stripmsg_EXE stripmsg LOCATION)

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