|
| 1 | +# System-Tools |
| 2 | +# Copyright (C) 2013-2024 by Thomas Dreibholz |
| 3 | +# |
| 4 | +# This program is free software: you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU General Public License as published by |
| 6 | +# the Free Software Foundation, either version 3 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +# |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +############################################################################# |
| 21 | +#### TRANSLATIONS #### |
| 22 | +############################################################################# |
| 23 | + |
| 24 | +SET(copyrightHolder "Copyright (C) 2013-2024 by Thomas Dreibholz") |
| 25 | +SET(bugTrackerAddress "https://github.com/dreibh/system-tools/issues") |
| 26 | + |
| 27 | +FILE(GLOB sources "*.sources") |
| 28 | + |
| 29 | +# ====== Find all text domains ============================================== |
| 30 | +FOREACH(source IN LISTS sources) |
| 31 | + # The .sources file lists the source files for each text domain: |
| 32 | + GET_FILENAME_COMPONENT(potFile "${source}" NAME_WE) |
| 33 | + SET(potFile "${CMAKE_CURRENT_BINARY_DIR}/${potFile}.pot") |
| 34 | + GET_FILENAME_COMPONENT(textdomain "${source}" NAME_WE) |
| 35 | + |
| 36 | + |
| 37 | + # ====== Create POT file (translation template) ========================== |
| 38 | + FILE(READ "${source}" potInputFiles) |
| 39 | + STRING(REGEX REPLACE "[ \n]" ";" potInputFiles "${potInputFiles}") |
| 40 | + SET(LIST potInputFilesForDependency "") |
| 41 | + SET(potInputFilesForGetText "") |
| 42 | + FOREACH(file ${potInputFiles}) |
| 43 | + GET_FILENAME_COMPONENT(fileName ${file} NAME) |
| 44 | + IF (${fileName} MATCHES "^[-][-]") |
| 45 | + # Option -> Just add it for gettext |
| 46 | + SET(potInputFilesForGetText "${potInputFilesForGetText} F=${fileName}") |
| 47 | + ELSE() |
| 48 | + # File -> Add absolute file name |
| 49 | + SET(potInputFilesForGetText "${potInputFilesForGetText} ${PROJECT_SOURCE_DIR}/${file}") |
| 50 | + LIST(APPEND potInputFilesForDependency ${potInputFilesAbsolute} "${PROJECT_SOURCE_DIR}/${file}") |
| 51 | + ENDIF() |
| 52 | + ENDFOREACH() |
| 53 | + |
| 54 | + MESSAGE("Translation template: ${textdomain} (${potInputFiles} -> ${potFile})") |
| 55 | + ADD_CUSTOM_COMMAND(OUTPUT "${potFile}" |
| 56 | + COMMAND "${XGETTEXT}" |
| 57 | + --from-code=utf-8 |
| 58 | + --default-domain="${textdomain}" |
| 59 | + --package-name="${PROJECT_NAME}" |
| 60 | + --package-version="${BUILD_VERSION}" |
| 61 | + --copyright-holder="${copyrightHolder}" |
| 62 | + --msgid-bugs-address="${bugTrackerAddress}" |
| 63 | + -p "${CMAKE_CURRENT_SOURCE_DIR}" |
| 64 | + -o "${potFile}" |
| 65 | + ${potInputFiles} |
| 66 | + COMMAND sed -e "s/charset=CHARSET/charset=UTF-8/g" -i~ "${potFile}" |
| 67 | + WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" |
| 68 | + DEPENDS ${potInputFilesForDependency} "${source}" |
| 69 | + VERBATIM) |
| 70 | + SET(generate_PotFile "generate_pot_${textdomain}") |
| 71 | + ADD_CUSTOM_TARGET(${generate_PotFile} ALL DEPENDS "${potFile}") |
| 72 | + |
| 73 | + |
| 74 | + # ====== Process PO files (translations) ================================= |
| 75 | + FILE(GLOB_RECURSE poFiles "*/${textdomain}.po") |
| 76 | + FOREACH(poFile IN LISTS poFiles) |
| 77 | + |
| 78 | + # ====== Update PO file (translation) ================================= |
| 79 | + GET_FILENAME_COMPONENT(languageDirectory "${poFile}" DIRECTORY) |
| 80 | + GET_FILENAME_COMPONENT(language "${languageDirectory}" NAME "${CMAKE_CURRENT_SOURCE_DIR}") |
| 81 | + SET(poStampFile "${CMAKE_CURRENT_BINARY_DIR}/${language}/.${textdomain}.po.stamp") |
| 82 | + |
| 83 | + MESSAGE("Translation update: ${textdomain}, ${language} (${potFile} -> ${poFile})") |
| 84 | + # The .po file cannot be OUTPUT, since "make clean" would then delete it |
| 85 | + # => Using a stamp file as placeholder to ensure the dependency. |
| 86 | + ADD_CUSTOM_COMMAND(OUTPUT "${poStampFile}" |
| 87 | + COMMAND "${MSGMERGE}" |
| 88 | + --update "${poFile}" "${potFile}" |
| 89 | + COMMAND touch "${poStampFile}" |
| 90 | + WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" |
| 91 | + DEPENDS ${generate_PotFile} "${potFile}") |
| 92 | + SET(generate_PoFile "generate_po_${textdomain}_${language}") |
| 93 | + ADD_CUSTOM_TARGET(${generate_PoFile} DEPENDS "${poStampFile}") |
| 94 | + |
| 95 | + |
| 96 | + # ====== Compile PO file to MO file =================================== |
| 97 | + FILE(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${language}/LC_MESSAGES") |
| 98 | + SET(moFile "${CMAKE_CURRENT_BINARY_DIR}/${language}/LC_MESSAGES/${textdomain}.mo") |
| 99 | + |
| 100 | + MESSAGE("Compiling translation: ${textdomain}, ${language} (${poFile} -> ${moFile})") |
| 101 | + ADD_CUSTOM_COMMAND(OUTPUT "${moFile}" |
| 102 | + COMMAND "${MSGFMT}" |
| 103 | + --statistics |
| 104 | + --check |
| 105 | + --output-file="${moFile}" |
| 106 | + ${poFile} |
| 107 | + WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" |
| 108 | + DEPENDS ${generate_PoFile} "${poFile}") |
| 109 | + SET(generate_MoFile "generate_mo_${textdomain}_${language}") |
| 110 | + ADD_CUSTOM_TARGET(${generate_MoFile} ALL DEPENDS "${moFile}") |
| 111 | + |
| 112 | + |
| 113 | + # ====== Install MO file ============================================== |
| 114 | + INSTALL(FILES "${moFile}" |
| 115 | + DESTINATION "${CMAKE_INSTALL_DATADIR}/locale/${language}/LC_MESSAGES") |
| 116 | + |
| 117 | + ENDFOREACH() |
| 118 | +ENDFOREACH() |
0 commit comments