1 | /* |
2 | SPDX-FileCopyrightText: 2014 Kevin Ottens <ervin@kde.org> |
3 | |
4 | SPDX-License-Identifier: LGPL-2.1-or-later |
5 | */ |
6 | |
7 | // Undefine this because we don't want our i18n*() method names to be turned into i18nd*() |
8 | #undef TRANSLATION_DOMAIN |
9 | |
10 | #include "translationcontext.h" |
11 | |
12 | #include <QDebug> |
13 | |
14 | #include <KLocalizedString> |
15 | |
16 | TranslationContext::TranslationContext(QObject *parent) |
17 | : QObject(parent) |
18 | { |
19 | } |
20 | |
21 | TranslationContext::~TranslationContext() |
22 | { |
23 | } |
24 | |
25 | QString TranslationContext::domain() const |
26 | { |
27 | return m_domain; |
28 | } |
29 | |
30 | void TranslationContext::setDomain(const QString &domain) |
31 | { |
32 | if (m_domain == domain) { |
33 | return; |
34 | } |
35 | |
36 | m_domain = domain; |
37 | Q_EMIT domainChanged(domain); |
38 | } |
39 | |
40 | QString TranslationContext::i18n(const QString &message, |
41 | const QString ¶m1, |
42 | const QString ¶m2, |
43 | const QString ¶m3, |
44 | const QString ¶m4, |
45 | const QString ¶m5, |
46 | const QString ¶m6, |
47 | const QString ¶m7, |
48 | const QString ¶m8, |
49 | const QString ¶m9, |
50 | const QString ¶m10) const |
51 | { |
52 | if (message.isNull()) { |
53 | qWarning() << "i18n() needs at least one parameter" ; |
54 | return QString(); |
55 | } |
56 | |
57 | KLocalizedString trMessage = ki18nd(domain: m_domain.toUtf8().constData(), text: message.toUtf8().constData()); |
58 | |
59 | if (!param1.isNull()) { |
60 | trMessage = trMessage.subs(a: param1); |
61 | } |
62 | if (!param2.isNull()) { |
63 | trMessage = trMessage.subs(a: param2); |
64 | } |
65 | if (!param3.isNull()) { |
66 | trMessage = trMessage.subs(a: param3); |
67 | } |
68 | if (!param4.isNull()) { |
69 | trMessage = trMessage.subs(a: param4); |
70 | } |
71 | if (!param5.isNull()) { |
72 | trMessage = trMessage.subs(a: param5); |
73 | } |
74 | if (!param6.isNull()) { |
75 | trMessage = trMessage.subs(a: param6); |
76 | } |
77 | if (!param7.isNull()) { |
78 | trMessage = trMessage.subs(a: param7); |
79 | } |
80 | if (!param8.isNull()) { |
81 | trMessage = trMessage.subs(a: param8); |
82 | } |
83 | if (!param9.isNull()) { |
84 | trMessage = trMessage.subs(a: param9); |
85 | } |
86 | if (!param10.isNull()) { |
87 | trMessage = trMessage.subs(a: param10); |
88 | } |
89 | |
90 | return trMessage.toString(); |
91 | } |
92 | |
93 | QString TranslationContext::i18nc(const QString &context, |
94 | const QString &message, |
95 | const QString ¶m1, |
96 | const QString ¶m2, |
97 | const QString ¶m3, |
98 | const QString ¶m4, |
99 | const QString ¶m5, |
100 | const QString ¶m6, |
101 | const QString ¶m7, |
102 | const QString ¶m8, |
103 | const QString ¶m9, |
104 | const QString ¶m10) const |
105 | { |
106 | if (context.isNull() || message.isNull()) { |
107 | qWarning() << "i18nc() needs at least two arguments" ; |
108 | return QString(); |
109 | } |
110 | |
111 | KLocalizedString trMessage = ki18ndc(domain: m_domain.toUtf8().constData(), context: context.toUtf8().constData(), text: message.toUtf8().constData()); |
112 | |
113 | if (!param1.isNull()) { |
114 | trMessage = trMessage.subs(a: param1); |
115 | } |
116 | if (!param2.isNull()) { |
117 | trMessage = trMessage.subs(a: param2); |
118 | } |
119 | if (!param3.isNull()) { |
120 | trMessage = trMessage.subs(a: param3); |
121 | } |
122 | if (!param4.isNull()) { |
123 | trMessage = trMessage.subs(a: param4); |
124 | } |
125 | if (!param5.isNull()) { |
126 | trMessage = trMessage.subs(a: param5); |
127 | } |
128 | if (!param6.isNull()) { |
129 | trMessage = trMessage.subs(a: param6); |
130 | } |
131 | if (!param7.isNull()) { |
132 | trMessage = trMessage.subs(a: param7); |
133 | } |
134 | if (!param8.isNull()) { |
135 | trMessage = trMessage.subs(a: param8); |
136 | } |
137 | if (!param9.isNull()) { |
138 | trMessage = trMessage.subs(a: param9); |
139 | } |
140 | if (!param10.isNull()) { |
141 | trMessage = trMessage.subs(a: param10); |
142 | } |
143 | |
144 | return trMessage.toString(); |
145 | } |
146 | |
147 | QString TranslationContext::i18np(const QString &singular, |
148 | const QString &plural, |
149 | const QString ¶m1, |
150 | const QString ¶m2, |
151 | const QString ¶m3, |
152 | const QString ¶m4, |
153 | const QString ¶m5, |
154 | const QString ¶m6, |
155 | const QString ¶m7, |
156 | const QString ¶m8, |
157 | const QString ¶m9, |
158 | const QString ¶m10) const |
159 | { |
160 | if (singular.isNull() || plural.isNull()) { |
161 | qWarning() << "i18np() needs at least two arguments" ; |
162 | return QString(); |
163 | } |
164 | |
165 | KLocalizedString trMessage = ki18ndp(domain: m_domain.toUtf8().constData(), singular: singular.toUtf8().constData(), plural: plural.toUtf8().constData()); |
166 | |
167 | if (!param1.isNull()) { |
168 | bool ok; |
169 | int num = param1.toInt(ok: &ok); |
170 | if (ok) { |
171 | trMessage = trMessage.subs(a: num); |
172 | } else { |
173 | trMessage = trMessage.subs(a: param1); |
174 | } |
175 | } |
176 | if (!param2.isNull()) { |
177 | trMessage = trMessage.subs(a: param2); |
178 | } |
179 | if (!param3.isNull()) { |
180 | trMessage = trMessage.subs(a: param3); |
181 | } |
182 | if (!param4.isNull()) { |
183 | trMessage = trMessage.subs(a: param4); |
184 | } |
185 | if (!param5.isNull()) { |
186 | trMessage = trMessage.subs(a: param5); |
187 | } |
188 | if (!param6.isNull()) { |
189 | trMessage = trMessage.subs(a: param6); |
190 | } |
191 | if (!param7.isNull()) { |
192 | trMessage = trMessage.subs(a: param7); |
193 | } |
194 | if (!param8.isNull()) { |
195 | trMessage = trMessage.subs(a: param8); |
196 | } |
197 | if (!param9.isNull()) { |
198 | trMessage = trMessage.subs(a: param9); |
199 | } |
200 | if (!param10.isNull()) { |
201 | trMessage = trMessage.subs(a: param10); |
202 | } |
203 | |
204 | return trMessage.toString(); |
205 | } |
206 | |
207 | QString TranslationContext::i18ncp(const QString &context, |
208 | const QString &singular, |
209 | const QString &plural, |
210 | const QString ¶m1, |
211 | const QString ¶m2, |
212 | const QString ¶m3, |
213 | const QString ¶m4, |
214 | const QString ¶m5, |
215 | const QString ¶m6, |
216 | const QString ¶m7, |
217 | const QString ¶m8, |
218 | const QString ¶m9, |
219 | const QString ¶m10) const |
220 | { |
221 | if (context.isNull() || singular.isNull() || plural.isNull()) { |
222 | qWarning() << "i18ncp() needs at least three arguments" ; |
223 | return QString(); |
224 | } |
225 | |
226 | KLocalizedString trMessage = |
227 | ki18ndcp(domain: m_domain.toUtf8().constData(), context: context.toUtf8().constData(), singular: singular.toUtf8().constData(), plural: plural.toUtf8().constData()); |
228 | |
229 | if (!param1.isNull()) { |
230 | bool ok; |
231 | int num = param1.toInt(ok: &ok); |
232 | if (ok) { |
233 | trMessage = trMessage.subs(a: num); |
234 | } else { |
235 | trMessage = trMessage.subs(a: param1); |
236 | } |
237 | } |
238 | if (!param2.isNull()) { |
239 | trMessage = trMessage.subs(a: param2); |
240 | } |
241 | if (!param3.isNull()) { |
242 | trMessage = trMessage.subs(a: param3); |
243 | } |
244 | if (!param4.isNull()) { |
245 | trMessage = trMessage.subs(a: param4); |
246 | } |
247 | if (!param5.isNull()) { |
248 | trMessage = trMessage.subs(a: param5); |
249 | } |
250 | if (!param6.isNull()) { |
251 | trMessage = trMessage.subs(a: param6); |
252 | } |
253 | if (!param7.isNull()) { |
254 | trMessage = trMessage.subs(a: param7); |
255 | } |
256 | if (!param8.isNull()) { |
257 | trMessage = trMessage.subs(a: param8); |
258 | } |
259 | if (!param9.isNull()) { |
260 | trMessage = trMessage.subs(a: param9); |
261 | } |
262 | if (!param10.isNull()) { |
263 | trMessage = trMessage.subs(a: param10); |
264 | } |
265 | |
266 | return trMessage.toString(); |
267 | } |
268 | |
269 | #include "moc_translationcontext.cpp" |
270 | |