Skip to content

Commit ede8244

Browse files
committed
store translation as string_view
1 parent c3d4d1b commit ede8244

20 files changed

+52
-49
lines changed

daemon/HTTPServer.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013-2024, The PurpleI2P Project
2+
* Copyright (c) 2013-2025, The PurpleI2P Project
33
*
44
* This file is part of Purple i2pd project and licensed under BSD3
55
*
@@ -132,7 +132,8 @@ namespace http {
132132

133133
static void ShowTunnelDetails (std::stringstream& s, enum i2p::tunnel::TunnelState eState, bool explr, int bytes)
134134
{
135-
std::string state, stateText;
135+
std::string state;
136+
std::string_view stateText;
136137
switch (eState)
137138
{
138139
case i2p::tunnel::eTunnelStateBuildReplyReceived :
@@ -146,7 +147,7 @@ namespace http {
146147
}
147148
if (stateText.empty ()) stateText = tr(state);
148149

149-
s << "<span class=\"tunnel " << state << "\"> " << stateText << ((explr) ? " (" + tr("exploratory") + ")" : "") << "</span>, ";
150+
s << "<span class=\"tunnel " << state << "\"> " << stateText << ((explr) ? " (" + std::string(tr("exploratory")) + ")" : "") << "</span>, "; // TODO:
150151
ShowTraffic(s, bytes);
151152
s << "\r\n";
152153
}
@@ -213,7 +214,7 @@ namespace http {
213214
"</html>\r\n";
214215
}
215216

216-
static void ShowError(std::stringstream& s, const std::string& string)
217+
static void ShowError(std::stringstream& s, std::string_view string)
217218
{
218219
s << "<b>" << tr("ERROR") << ":</b>&nbsp;" << string << "<br>\r\n";
219220
}
@@ -1262,7 +1263,7 @@ namespace http {
12621263
ShowLeasesSets(s);
12631264
else {
12641265
res.code = 400;
1265-
ShowError(s, tr("Unknown page") + ": " + page);
1266+
ShowError(s, std::string (tr("Unknown page")) + ": " + page); // TODO
12661267
return;
12671268
}
12681269
}
@@ -1462,7 +1463,7 @@ namespace http {
14621463
else
14631464
{
14641465
res.code = 400;
1465-
ShowError(s, tr("Unknown command") + ": " + cmd);
1466+
ShowError(s, std::string (tr("Unknown command")) + ": " + cmd); // TODO
14661467
return;
14671468
}
14681469

i18n/Afrikaans.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, The PurpleI2P Project
2+
* Copyright (c) 2021-2025, The PurpleI2P Project
33
*
44
* This file is part of Purple i2pd project and licensed under BSD3
55
*
@@ -29,7 +29,7 @@ namespace afrikaans // language namespace
2929
return n != 1 ? 1 : 0;
3030
}
3131

32-
static std::map<std::string, std::string> strings
32+
static const LocaleStrings strings
3333
{
3434
{"failed", "Het misluk"},
3535
{"unknown", "onbekend"},

i18n/Armenian.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021-2023, The PurpleI2P Project
2+
* Copyright (c) 2021-2025, The PurpleI2P Project
33
*
44
* This file is part of Purple i2pd project and licensed under BSD3
55
*
@@ -29,7 +29,7 @@ namespace armenian // language namespace
2929
return n != 1 ? 1 : 0;
3030
}
3131

32-
static std::map<std::string, std::string> strings
32+
static const LocaleStrings strings
3333
{
3434
{"%.2f KiB", "%.2f ԿիԲ"},
3535
{"%.2f MiB", "%.2f ՄիԲ"},

i18n/Chinese.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022-2024, The PurpleI2P Project
2+
* Copyright (c) 2022-2025, The PurpleI2P Project
33
*
44
* This file is part of Purple i2pd project and licensed under BSD3
55
*
@@ -29,7 +29,7 @@ namespace chinese // language namespace
2929
return 0;
3030
}
3131

32-
static std::map<std::string, std::string> strings
32+
static const LocaleStrings strings
3333
{
3434
{"%.2f KiB", "%.2f KiB"},
3535
{"%.2f MiB", "%.2f MiB"},

i18n/Czech.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022-2024, The PurpleI2P Project
2+
* Copyright (c) 2022-2025, The PurpleI2P Project
33
*
44
* This file is part of Purple i2pd project and licensed under BSD3
55
*
@@ -29,7 +29,7 @@ namespace czech // language namespace
2929
return (n == 1) ? 0 : (n >= 2 && n <= 4) ? 1 : 2;
3030
}
3131

32-
static std::map<std::string, std::string> strings
32+
static const LocaleStrings strings
3333
{
3434
{"%.2f KiB", "%.2f KiB"},
3535
{"%.2f MiB", "%.2f MiB"},

i18n/English.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, The PurpleI2P Project
2+
* Copyright (c) 2021-2025, The PurpleI2P Project
33
*
44
* This file is part of Purple i2pd project and licensed under BSD3
55
*
@@ -30,7 +30,7 @@ namespace english // language namespace
3030
return n != 1 ? 1 : 0;
3131
}
3232

33-
static std::map<std::string, std::string> strings
33+
static const LocaleStrings strings
3434
{
3535
{"", ""},
3636
};

i18n/French.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022-2024, The PurpleI2P Project
2+
* Copyright (c) 2022-2025, The PurpleI2P Project
33
*
44
* This file is part of Purple i2pd project and licensed under BSD3
55
*
@@ -29,7 +29,7 @@ namespace french // language namespace
2929
return n != 1 ? 1 : 0;
3030
}
3131

32-
static std::map<std::string, std::string> strings
32+
static const LocaleStrings strings
3333
{
3434
{"%.2f KiB", "%.2f Kio"},
3535
{"%.2f MiB", "%.2f Mio"},

i18n/German.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022-2023, The PurpleI2P Project
2+
* Copyright (c) 2022-2025, The PurpleI2P Project
33
*
44
* This file is part of Purple i2pd project and licensed under BSD3
55
*
@@ -29,7 +29,7 @@ namespace german // language namespace
2929
return n != 1 ? 1 : 0;
3030
}
3131

32-
static std::map<std::string, std::string> strings
32+
static const LocaleStrings strings
3333
{
3434
{"%.2f KiB", "%.2f KiB"},
3535
{"%.2f MiB", "%.2f MiB"},

i18n/I18N.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021-2023, The PurpleI2P Project
2+
* Copyright (c) 2021-2025, The PurpleI2P Project
33
*
44
* This file is part of Purple i2pd project and licensed under BSD3
55
*
@@ -30,7 +30,7 @@ namespace i18n
3030
}
3131
}
3232

33-
std::string translate (const std::string& arg)
33+
std::string_view translate (std::string_view arg)
3434
{
3535
return i2p::client::context.GetLanguage ()->GetString (arg);
3636
}

i18n/I18N.h

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021-2023, The PurpleI2P Project
2+
* Copyright (c) 2021-2025, The PurpleI2P Project
33
*
44
* This file is part of Purple i2pd project and licensed under BSD3
55
*
@@ -10,6 +10,7 @@
1010
#define __I18N_H__
1111

1212
#include <string>
13+
#include <string_view>
1314
#include <map>
1415
#include <utility>
1516
#include <functional>
@@ -18,12 +19,13 @@ namespace i2p
1819
{
1920
namespace i18n
2021
{
22+
typedef std::map<std::string_view, std::string_view> LocaleStrings;
2123
class Locale
2224
{
2325
public:
2426
Locale (
2527
const std::string& language,
26-
const std::map<std::string, std::string>& strings,
28+
const LocaleStrings& strings,
2729
const std::map<std::string, std::vector<std::string>>& plurals,
2830
std::function<int(int)> formula
2931
): m_Language (language), m_Strings (strings), m_Plurals (plurals), m_Formula (formula) { };
@@ -34,7 +36,7 @@ namespace i18n
3436
return m_Language;
3537
}
3638

37-
std::string GetString (const std::string& arg) const
39+
std::string_view GetString (std::string_view arg) const
3840
{
3941
const auto it = m_Strings.find(arg);
4042
if (it == m_Strings.end())
@@ -63,13 +65,13 @@ namespace i18n
6365

6466
private:
6567
const std::string m_Language;
66-
const std::map<std::string, std::string> m_Strings;
68+
const LocaleStrings m_Strings;
6769
const std::map<std::string, std::vector<std::string>> m_Plurals;
6870
std::function<int(int)> m_Formula;
6971
};
7072

7173
void SetLanguage(const std::string &lang);
72-
std::string translate (const std::string& arg);
74+
std::string_view translate (std::string_view arg);
7375
std::string translate (const std::string& arg, const std::string& arg2, const int& n);
7476
} // i18n
7577
} // i2p
@@ -79,7 +81,7 @@ namespace i18n
7981
* @param arg String with message
8082
*/
8183
template<typename TValue>
82-
std::string tr (TValue&& arg)
84+
std::string_view tr (TValue&& arg)
8385
{
8486
return i2p::i18n::translate(std::forward<TValue>(arg));
8587
}
@@ -92,7 +94,7 @@ std::string tr (TValue&& arg)
9294
template<typename TValue, typename... TArgs>
9395
std::string tr (TValue&& arg, TArgs&&... args)
9496
{
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:
9698

9799
size_t size = std::snprintf(NULL, 0, tr_str.c_str(), std::forward<TArgs>(args)...);
98100
std::string str(size, 0);

i18n/Italian.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022-2023, The PurpleI2P Project
2+
* Copyright (c) 2022-2025, The PurpleI2P Project
33
*
44
* This file is part of Purple i2pd project and licensed under BSD3
55
*
@@ -29,7 +29,7 @@ namespace italian // language namespace
2929
return n != 1 ? 1 : 0;
3030
}
3131

32-
static std::map<std::string, std::string> strings
32+
static const LocaleStrings strings
3333
{
3434
{"%.2f KiB", "%.2f KiB"},
3535
{"%.2f MiB", "%.2f MiB"},

i18n/Polish.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023-2024, The PurpleI2P Project
2+
* Copyright (c) 2023-2025, The PurpleI2P Project
33
*
44
* This file is part of Purple i2pd project and licensed under BSD3
55
*
@@ -29,7 +29,7 @@ namespace polish // language namespace
2929
return (n == 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2);
3030
}
3131

32-
static std::map<std::string, std::string> strings
32+
static const LocaleStrings strings
3333
{
3434
{"%.2f KiB", "%.2f KiB"},
3535
{"%.2f MiB", "%.2f MiB"},

i18n/Portuguese.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023-2024, The PurpleI2P Project
2+
* Copyright (c) 2023-2025, The PurpleI2P Project
33
*
44
* This file is part of Purple i2pd project and licensed under BSD3
55
*
@@ -29,7 +29,7 @@ namespace portuguese // language namespace
2929
return n != 1 ? 1 : 0;
3030
}
3131

32-
static std::map<std::string, std::string> strings
32+
static const LocaleStrings strings
3333
{
3434
{"%.2f KiB", "%.2f KiB"},
3535
{"%.2f MiB", "%.2f MiB"},

i18n/Russian.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021-2023, The PurpleI2P Project
2+
* Copyright (c) 2021-2025, The PurpleI2P Project
33
*
44
* This file is part of Purple i2pd project and licensed under BSD3
55
*
@@ -29,7 +29,7 @@ namespace russian // language namespace
2929
return n % 10 == 1 && n % 100 != 11 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2;
3030
}
3131

32-
static std::map<std::string, std::string> strings
32+
static const LocaleStrings strings
3333
{
3434
{"%.2f KiB", "%.2f КиБ"},
3535
{"%.2f MiB", "%.2f МиБ"},

i18n/Spanish.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022-2023, The PurpleI2P Project
2+
* Copyright (c) 2022-2025, The PurpleI2P Project
33
*
44
* This file is part of Purple i2pd project and licensed under BSD3
55
*
@@ -29,7 +29,7 @@ namespace spanish // language namespace
2929
return n != 1 ? 1 : 0;
3030
}
3131

32-
static std::map<std::string, std::string> strings
32+
static const LocaleStrings strings
3333
{
3434
{"%.2f KiB", "%.2f KiB"},
3535
{"%.2f MiB", "%.2f MiB"},

i18n/Swedish.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, The PurpleI2P Project
2+
* Copyright (c) 2023-2025, The PurpleI2P Project
33
*
44
* This file is part of Purple i2pd project and licensed under BSD3
55
*
@@ -29,7 +29,7 @@ namespace swedish // language namespace
2929
return n != 1 ? 1 : 0;
3030
}
3131

32-
static std::map<std::string, std::string> strings
32+
static const LocaleStrings strings
3333
{
3434
{"%.2f KiB", "%.2f KiB"},
3535
{"%.2f MiB", "%.2f MiB"},

i18n/Turkish.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, The PurpleI2P Project
2+
* Copyright (c) 2023-2025, The PurpleI2P Project
33
*
44
* This file is part of Purple i2pd project and licensed under BSD3
55
*
@@ -29,7 +29,7 @@ namespace turkish // language namespace
2929
return n != 1 ? 1 : 0;
3030
}
3131

32-
static std::map<std::string, std::string> strings
32+
static const LocaleStrings strings
3333
{
3434
{"%.2f KiB", "%.2f KiB"},
3535
{"%.2f MiB", "%.2f MiB"},

i18n/Turkmen.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021-2023, The PurpleI2P Project
2+
* Copyright (c) 2021-2025, The PurpleI2P Project
33
*
44
* This file is part of Purple i2pd project and licensed under BSD3
55
*
@@ -29,7 +29,7 @@ namespace turkmen // language namespace
2929
return n != 1 ? 1 : 0;
3030
}
3131

32-
static std::map<std::string, std::string> strings
32+
static const LocaleStrings strings
3333
{
3434
{"%.2f KiB", "%.2f KiB"},
3535
{"%.2f MiB", "%.2f MiB"},

i18n/Ukrainian.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021-2023, The PurpleI2P Project
2+
* Copyright (c) 2021-2025, The PurpleI2P Project
33
*
44
* This file is part of Purple i2pd project and licensed under BSD3
55
*
@@ -29,7 +29,7 @@ namespace ukrainian // language namespace
2929
return n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;
3030
}
3131

32-
static std::map<std::string, std::string> strings
32+
static const LocaleStrings strings
3333
{
3434
{"%.2f KiB", "%.2f КіБ"},
3535
{"%.2f MiB", "%.2f МіБ"},

0 commit comments

Comments
 (0)