LeviLamina
Loading...
Searching...
No Matches
I18n.h
1#pragma once
2
3#include "ll/api/Expected.h"
4#include "ll/api/base/Concepts.h"
5#include "ll/api/base/FixedString.h"
6
7#include <string>
8
9#include "fmt/chrono.h"
10#include "fmt/color.h"
11#include "fmt/compile.h"
12#include "fmt/format.h"
13#include "fmt/os.h"
14#include "fmt/ranges.h"
15#include "fmt/std.h"
16
17// #define LL_I18N_COLLECT_STRINGS
18
19namespace ll::i18n {
20
21LLNDAPI std::string_view getDefaultLocaleCode();
22
23class I18n {
24 struct Impl;
25 std::unique_ptr<Impl> impl;
26
27public:
28 LLNDAPI I18n();
29 LLAPI ~I18n();
30
31 LLNDAPI Expected<> load(std::filesystem::path const& path) noexcept;
32
33 LLAPI void clear();
34
35 LLAPI void set(std::string_view localeCode, std::string_view key, std::string_view value);
36
37 LLNDAPI std::string_view get(std::string_view key, std::string_view localeCode) const;
38};
39
40inline I18n& getInstance() {
41 static I18n ins{};
42 return ins;
43}
44
45} // namespace ll::i18n
46
47#ifdef LL_I18N_COLLECT_STRINGS
48#include "ll/api/reflection/TypeName.h"
49
50#ifndef LL_I18N_STRING_LITERAL_TYPE
51#define LL_I18N_STRING_LITERAL_TYPE ::ll::FixedStrWithLoc
52#endif
53
54namespace ll::i18n::detail {
55template <LL_I18N_STRING_LITERAL_TYPE str>
56struct TrStrOut;
57#ifndef LL_I18N_COLLECT_STRINGS_CUSTOM
58template <LL_I18N_STRING_LITERAL_TYPE str>
59struct TrStrOut {
60 static inline int _ = [] {
61 fmt::print("\"{0}\": \"{0}\", // at {1}\n", str.sv(), str.loc().toString());
62 return 0;
63 }();
64};
65#endif
66} // namespace ll::i18n::detail
67#else
68#ifndef LL_I18N_STRING_LITERAL_TYPE
69#define LL_I18N_STRING_LITERAL_TYPE ::ll::FixedString
70#endif
71#endif
72namespace ll::inline literals::inline i18n_literals {
73template <LL_I18N_STRING_LITERAL_TYPE Fmt>
74[[nodiscard]] constexpr auto operator""_tr() {
75#ifdef LL_I18N_COLLECT_STRINGS
76 static i18n::detail::TrStrOut<Fmt> e{};
77#endif
78 return [=]<class... Args>(Args&&... args) {
79 [[maybe_unused]] static constexpr auto checker = fmt::format_string<Args...>(Fmt.sv());
80 return fmt::vformat(i18n::getInstance().get(Fmt.sv(), {}), fmt::make_format_args(args...));
81 };
82}
83template <LL_I18N_STRING_LITERAL_TYPE Fmt>
84[[nodiscard]] constexpr auto operator""_trl() {
85#ifdef LL_I18N_COLLECT_STRINGS
86 static i18n::detail::TrStrOut<Fmt> e{};
87#endif
88 return [=]<class... Args>(std::string_view localeCode, Args&&... args) {
89 [[maybe_unused]] static constexpr auto checker = fmt::format_string<Args...>(Fmt.sv());
90 return fmt::vformat(i18n::getInstance().get(Fmt.sv(), localeCode), fmt::make_format_args(args...));
91 };
92}
93} // namespace ll::inline literals::inline i18n_literals
Definition I18n.h:23