1
1
/*
2
- * Copyright (c) 2021-2023 , The PurpleI2P Project
2
+ * Copyright (c) 2021-2025 , The PurpleI2P Project
3
3
*
4
4
* This file is part of Purple i2pd project and licensed under BSD3
5
5
*
10
10
#define __I18N_H__
11
11
12
12
#include < string>
13
+ #include < string_view>
13
14
#include < map>
14
15
#include < utility>
15
16
#include < functional>
@@ -18,12 +19,13 @@ namespace i2p
18
19
{
19
20
namespace i18n
20
21
{
22
+ typedef std::map<std::string_view, std::string_view> LocaleStrings;
21
23
class Locale
22
24
{
23
25
public:
24
26
Locale (
25
27
const std::string& language,
26
- const std::map<std::string, std::string> & strings,
28
+ const LocaleStrings & strings,
27
29
const std::map<std::string, std::vector<std::string>>& plurals,
28
30
std::function<int (int )> formula
29
31
): m_Language (language), m_Strings (strings), m_Plurals (plurals), m_Formula (formula) { };
@@ -34,7 +36,7 @@ namespace i18n
34
36
return m_Language;
35
37
}
36
38
37
- std::string GetString (const std::string& arg) const
39
+ std::string_view GetString (std::string_view arg) const
38
40
{
39
41
const auto it = m_Strings.find (arg);
40
42
if (it == m_Strings.end ())
@@ -63,13 +65,13 @@ namespace i18n
63
65
64
66
private:
65
67
const std::string m_Language;
66
- const std::map<std::string, std::string> m_Strings;
68
+ const LocaleStrings m_Strings;
67
69
const std::map<std::string, std::vector<std::string>> m_Plurals;
68
70
std::function<int (int )> m_Formula;
69
71
};
70
72
71
73
void SetLanguage (const std::string &lang);
72
- std::string translate (const std::string& arg);
74
+ std::string_view translate (std::string_view arg);
73
75
std::string translate (const std::string& arg, const std::string& arg2, const int & n);
74
76
} // i18n
75
77
} // i2p
@@ -79,7 +81,7 @@ namespace i18n
79
81
* @param arg String with message
80
82
*/
81
83
template <typename TValue>
82
- std::string tr (TValue&& arg)
84
+ std::string_view tr (TValue&& arg)
83
85
{
84
86
return i2p::i18n::translate (std::forward<TValue>(arg));
85
87
}
@@ -92,7 +94,7 @@ std::string tr (TValue&& arg)
92
94
template <typename TValue, typename ... TArgs>
93
95
std::string tr (TValue&& arg, TArgs&&... args)
94
96
{
95
- std::string tr_str = i2p::i18n::translate (std::forward<TValue>(arg));
97
+ std::string tr_str = std::string ( i2p::i18n::translate (std::forward<TValue>(arg))); // TODO:
96
98
97
99
size_t size = std::snprintf (NULL , 0 , tr_str.c_str (), std::forward<TArgs>(args)...);
98
100
std::string str (size, 0 );
0 commit comments