7#include "po_translations.hpp"
8#include "po_parser.hpp"
9#include "../i18n/i18n.hpp"
10#include "../utility/utility.hpp"
11#include "../settings/settings.hpp"
12#include "../unicode/unicode.hpp"
13#include "../telemetry/telemetry.hpp"
14#include "../macros.hpp"
18#include <unordered_map>
21hi_export_module(hikogui.l10n.translation);
23hi_export
namespace hi {
32 return hash_mix(msgid, language);
48hi_export
namespace hi {
54inline void add_translation(std::string_view msgid, language_tag language,
std::vector<std::string> const &plural_forms)
noexcept
56 auto key = translation_key{
std::string{msgid}, language};
57 translations[key] = plural_forms;
60inline void add_translations(po_translations
const &po_translations)
noexcept
62 for (
auto const &translation : po_translations.translations) {
63 auto msgid = translation.msgctxt ? *translation.msgctxt +
'|' + translation.msgid : translation.msgid;
64 add_translation(
std::move(msgid), po_translations.language, translation.msgstr);
68inline void load_translations(std::filesystem::path path)
70 hi_log_info(
"Loading translation file {}.", path.string());
71 return add_translations(parse_po(path));
74inline void load_translations()
76 if (not translations_loaded.
exchange(
true)) {
79 for (
auto &path :
glob(resource_paths,
"**/*.po")) {
81 load_translations(path);
83 hi_log_error(
"Could not load translation file. {}", e.
what());
95 auto key = translation_key{
std::string{msgid}, language_tag{}};
97 for (
auto const language : languages) {
98 key.language = language;
100 auto const i = translations.find(key);
101 if (i != translations.cend()) {
103 auto const& translation = i->second[plurality];
104 if (translation.size() != 0) {
105 return {translation, language};
109 hi_log_debug(
"No translation found for '{}'", msgid);
110 return {msgid, language_tag{
"en-Latn-US"}};
hi_export generator< std::filesystem::path > glob(glob_pattern pattern) noexcept
Find paths on the filesystem that match the glob pattern.
Definition glob.hpp:880
generator< std::filesystem::path > resource_dirs() noexcept
The directories to search for resource files.
Definition path_location_win32_impl.hpp:63
The HikoGUI namespace.
Definition array_generic.hpp:20
constexpr plurality cardinal_plural(language_tag language, std::integral auto n) noexcept
Get plural information of a number in a given language.
Definition unicode_plural.hpp:929
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
The IETF BCP 47 language tag.
Definition language_tag_intf.hpp:30
Definition translation.hpp:26