- 1 year ago
CMake project using gettext on Windows
To create a CMake project using gettext
on Windows, you'll need to perform the following steps:
Install
gettext
:- Download the precompiled
gettext
binaries for Windows from the GNU gettext official website: https://www.gnu.org/software/gettext/ - Extract the downloaded package to a suitable location on your system.
- Add the
bin
directory ofgettext
to your system's PATH environment variable to make thegettext
utilities accessible from the command line.
- Download the precompiled
Set up your CMake project:
Create a new directory for your CMake project.
Inside the project directory, create a
CMakeLists.txt
file with the following contents:cmakecmake_minimum_required(VERSION 3.10)
project(MyProject)
# Find gettext package
find_package(Gettext REQUIRED)
# Add your project's source files
add_executable(MyProject main.cpp)
# Specify the target's include directories
target_include_directories(MyProject PRIVATE ${GETTEXT_INCLUDE_DIRS})
# Specify the target's link libraries
target_link_libraries(MyProject PRIVATE ${GETTEXT_LIBRARIES})
Create your source files (e.g.,
main.cpp
) in the project directory.
Generate translation files:
In your project directory, create a subdirectory named
locale
.Inside the
locale
directory, create a subdirectory for each language you want to support. For example, create afr
directory for French translations.Use the
xgettext
utility (provided bygettext
) to extract translatable strings from your source code and generate a.pot
template file. Run the following command in your project directory:cssxgettext --keyword=_ --output=locale/messages.pot *.cpp
Copy the generated
messages.pot
file into each language directory (e.g.,locale/fr
).Use a translation editor, such as
poedit
(https://poedit.net/), to open each.pot
file and create the corresponding.po
translation files. Save the translations in the respective language directories (e.g.,locale/fr/messages.po
).
Configure your CMake project to generate the translated
.mo
files during the build:Modify your
CMakeLists.txt
file to include the following lines:cmake# Specify the target's build output directory
set(GETTEXT_TRANSLATIONS_DIR ${CMAKE_BINARY_DIR}/translations)
# Generate the .mo files during the build
add_custom_target(translations)
add_custom_command(TARGET translations POST_BUILD
COMMAND ${GETTEXT_MSGFMT_EXECUTABLE} -o ${GETTEXT_TRANSLATIONS_DIR}/fr.mo ${CMAKE_SOURCE_DIR}/locale/fr/messages.po
COMMENT "Generating translated .mo files"
)
Replace
fr
with the language code of each translation you want to generate. Repeat theadd_custom_command()
block for each language.
Build your CMake project:
Create a build directory inside your project directory.
Navigate to the build directory and run the following commands:
csscmake ..
cmake --build .
Run your project:
After building the project, the translated
.mo
files will be generated in thetranslations
directory inside the build directory.Set the
LANG
environment variable to the desired language code before running your project to test the translations. For example:arduinoset LANG=fr
MyProject.exe
By following these steps, you can set up a C