1 | /* $Id: VirtualBoxTranslator.cpp 93637 2022-02-07 11:22:06Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Translator class.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_MAIN_VIRTUALBOXCLIENT /** @todo add separate logging group! */
|
---|
23 | #include "LoggingNew.h"
|
---|
24 |
|
---|
25 | #include <iprt/asm.h>
|
---|
26 | #include <iprt/ctype.h>
|
---|
27 | #include <iprt/env.h>
|
---|
28 | #include <iprt/err.h>
|
---|
29 | #include <iprt/locale.h>
|
---|
30 | #include <iprt/once.h>
|
---|
31 | #include <iprt/path.h>
|
---|
32 | #include <iprt/string.h>
|
---|
33 | #include <iprt/thread.h>
|
---|
34 | #include <iprt/strcache.h>
|
---|
35 |
|
---|
36 | #ifdef RT_OS_DARWIN
|
---|
37 | #include <CoreFoundation/CFLocale.h>
|
---|
38 | #include <CoreFoundation/CFString.h>
|
---|
39 | #endif
|
---|
40 |
|
---|
41 | #include "Global.h"
|
---|
42 | #include "VirtualBoxBase.h"
|
---|
43 | #include "QMTranslator.h"
|
---|
44 | #include "VirtualBoxTranslator.h"
|
---|
45 |
|
---|
46 |
|
---|
47 | /*********************************************************************************************************************************
|
---|
48 | * Defined Constants And Macros *
|
---|
49 | *********************************************************************************************************************************/
|
---|
50 | #define TRANSLATOR_CACHE_SIZE 32
|
---|
51 |
|
---|
52 |
|
---|
53 | /*********************************************************************************************************************************
|
---|
54 | * Global Variables *
|
---|
55 | *********************************************************************************************************************************/
|
---|
56 | /** Init once for the critical section. */
|
---|
57 | static RTONCE g_Once = RTONCE_INITIALIZER;
|
---|
58 | RTCRITSECTRW VirtualBoxTranslator::s_instanceRwLock;
|
---|
59 | VirtualBoxTranslator *VirtualBoxTranslator::s_pInstance = NULL;
|
---|
60 | /** TLS index that points to the translated text. */
|
---|
61 | static RTTLS g_idxTlsTr = NIL_RTTLS;
|
---|
62 | /** TLS index that points to the original text. */
|
---|
63 | static RTTLS g_idxTlsSrc = NIL_RTTLS;
|
---|
64 |
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * @callback_method_impl{FNRTONCE}
|
---|
68 | */
|
---|
69 | static DECLCALLBACK(int32_t) initLock(void *pvUser)
|
---|
70 | {
|
---|
71 | RT_NOREF(pvUser);
|
---|
72 | return VirtualBoxTranslator::initCritSect();
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Obtains the user language code in ll_CC form depending on platform
|
---|
78 | *
|
---|
79 | * @returns VBox status code
|
---|
80 | * @param pszName The buffer for storing user language code
|
---|
81 | * @param cbName Size of the pszName buffer
|
---|
82 | */
|
---|
83 | static int vboxGetDefaultUserLanguage(char *pszName, size_t cbName)
|
---|
84 | {
|
---|
85 | AssertReturn(pszName, VERR_INVALID_PARAMETER);
|
---|
86 | AssertReturn(cbName >= 6, VERR_INVALID_PARAMETER); /* 5 chars for language + null termination */
|
---|
87 |
|
---|
88 | #ifdef RT_OS_WINDOWS
|
---|
89 | if ( GetLocaleInfoA(GetUserDefaultLCID(), LOCALE_SISO639LANGNAME, pszName, (int)cbName) == 3
|
---|
90 | && GetLocaleInfoA(GetUserDefaultLCID(), LOCALE_SISO3166CTRYNAME, &pszName[3], (int)cbName - 4) == 3)
|
---|
91 | {
|
---|
92 | pszName[2] = '_';
|
---|
93 | Assert(RTLOCALE_IS_LANGUAGE2_UNDERSCORE_COUNTRY2(pszName));
|
---|
94 | return VINF_SUCCESS;
|
---|
95 | }
|
---|
96 | #elif RT_OS_DARWIN
|
---|
97 | CFLocaleRef locale = CFLocaleCopyCurrent();
|
---|
98 | CFTypeRef localeId = CFLocaleGetValue (locale, kCFLocaleIdentifier);
|
---|
99 | char szLocale[256] = { 0 };
|
---|
100 | if (CFGetTypeID(localeId) == CFStringGetTypeID())
|
---|
101 | CFStringGetCString((CFStringRef)localeId, szLocale, sizeof(szLocale), kCFStringEncodingUTF8);
|
---|
102 | /* Some cleanup */
|
---|
103 | CFRelease(locale);
|
---|
104 | if (szLocale[0] == '\0')
|
---|
105 | {
|
---|
106 | pszName[0] = 'C';
|
---|
107 | pszName[1] = 0;
|
---|
108 | return VINF_SUCCESS;
|
---|
109 | }
|
---|
110 | else
|
---|
111 | return RTStrCopy(pszName, cbName, szLocale);
|
---|
112 |
|
---|
113 | #elif defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD) || defined(RT_OS_NETBSD) || defined(RT_OS_OPENBSD) || defined(RT_OS_SOLARIS)
|
---|
114 | const char *pszValue = RTEnvGet("LC_ALL");
|
---|
115 | if (pszValue == 0)
|
---|
116 | pszValue = RTEnvGet("LC_MESSAGES");
|
---|
117 | if (pszValue == 0)
|
---|
118 | pszValue = RTEnvGet("LANG");
|
---|
119 | if (pszValue != 0)
|
---|
120 | {
|
---|
121 | /* ignore codepage part, i.e. ignore ".UTF-8" in "ru_RU.UTF-8" */
|
---|
122 | const char *pszDot = strchr(pszValue, '.');
|
---|
123 | size_t cbValue = strlen(pszValue);
|
---|
124 | if (pszDot != NULL)
|
---|
125 | cbValue = RT_MIN(cbValue, (size_t)(pszDot - pszValue));
|
---|
126 |
|
---|
127 | if ( ( cbValue == 2
|
---|
128 | && RT_C_IS_LOWER(pszValue[0])
|
---|
129 | && RT_C_IS_LOWER(pszValue[1]))
|
---|
130 | || ( cbValue == 5
|
---|
131 | && RTLOCALE_IS_LANGUAGE2_UNDERSCORE_COUNTRY2(pszValue)))
|
---|
132 | return RTStrCopyEx(pszName, cbName, pszValue, cbValue);
|
---|
133 | }
|
---|
134 | #endif
|
---|
135 | return RTLocaleQueryNormalizedBaseLocaleName(pszName, cbName);
|
---|
136 | }
|
---|
137 |
|
---|
138 | VirtualBoxTranslator::VirtualBoxTranslator()
|
---|
139 | : util::RWLockHandle(util::LOCKCLASS_TRANSLATOR)
|
---|
140 | , m_cInstanceRefs(0)
|
---|
141 | , m_pDefaultComponent(NULL)
|
---|
142 | , m_strLanguage("C")
|
---|
143 | , m_hStrCache(NIL_RTSTRCACHE)
|
---|
144 | {
|
---|
145 | g_idxTlsTr = RTTlsAlloc();
|
---|
146 | g_idxTlsSrc = RTTlsAlloc();
|
---|
147 | int rc = RTStrCacheCreate(&m_hStrCache, "API Translation");
|
---|
148 | m_rcCache = rc;
|
---|
149 | if (RT_FAILURE(rc))
|
---|
150 | m_hStrCache = NIL_RTSTRCACHE; /* (loadLanguage will fail) */
|
---|
151 | LogFlowFunc(("m_rcCache=%Rrc g_idxTlsTr=%#x g_idxTlsSrc=%#x\n", m_rcCache, g_idxTlsTr, g_idxTlsSrc));
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | VirtualBoxTranslator::~VirtualBoxTranslator()
|
---|
156 | {
|
---|
157 | LogFlowFunc(("enter\n"));
|
---|
158 |
|
---|
159 | /* Write-lock the object as we could be racing language change
|
---|
160 | notifications processing during XPCOM shutdown. (risky?) */
|
---|
161 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
162 |
|
---|
163 | RTTlsFree(g_idxTlsTr);
|
---|
164 | g_idxTlsTr = NIL_RTTLS;
|
---|
165 | RTTlsFree(g_idxTlsSrc);
|
---|
166 | g_idxTlsSrc = NIL_RTTLS;
|
---|
167 |
|
---|
168 | m_pDefaultComponent = NULL;
|
---|
169 |
|
---|
170 | for (TranslatorList::iterator it = m_lTranslators.begin();
|
---|
171 | it != m_lTranslators.end();
|
---|
172 | ++it)
|
---|
173 | {
|
---|
174 | if (it->pTranslator != NULL)
|
---|
175 | delete it->pTranslator;
|
---|
176 | it->pTranslator = NULL;
|
---|
177 | }
|
---|
178 | if (m_hStrCache != NIL_RTSTRCACHE)
|
---|
179 | {
|
---|
180 | RTStrCacheDestroy(m_hStrCache);
|
---|
181 | m_hStrCache = NIL_RTSTRCACHE;
|
---|
182 | m_rcCache = VERR_WRONG_ORDER;
|
---|
183 | }
|
---|
184 | LogFlowFunc(("returns\n"));
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Get or create a translator instance (singelton), referenced.
|
---|
190 | *
|
---|
191 | * The main reference is held by the main VBox singelton objects (VirtualBox,
|
---|
192 | * VirtualBoxClient) tying it's lifetime to theirs.
|
---|
193 | */
|
---|
194 | /* static */
|
---|
195 | VirtualBoxTranslator *VirtualBoxTranslator::instance()
|
---|
196 | {
|
---|
197 | int rc = RTOnce(&g_Once, initLock, NULL);
|
---|
198 | if (RT_SUCCESS(rc))
|
---|
199 | {
|
---|
200 | RTCritSectRwEnterShared(&s_instanceRwLock);
|
---|
201 | VirtualBoxTranslator *pInstance = s_pInstance;
|
---|
202 | if (RT_LIKELY(pInstance != NULL))
|
---|
203 | {
|
---|
204 | uint32_t cRefs = ASMAtomicIncU32(&pInstance->m_cInstanceRefs);
|
---|
205 | Assert(cRefs > 1); Assert(cRefs < _8K); RT_NOREF(cRefs);
|
---|
206 | RTCritSectRwLeaveShared(&s_instanceRwLock);
|
---|
207 | return pInstance;
|
---|
208 | }
|
---|
209 |
|
---|
210 | /* Maybe create the instance: */
|
---|
211 | RTCritSectRwLeaveShared(&s_instanceRwLock);
|
---|
212 | RTCritSectRwEnterExcl(&s_instanceRwLock);
|
---|
213 | pInstance = s_pInstance;
|
---|
214 | if (pInstance == NULL)
|
---|
215 | s_pInstance = pInstance = new VirtualBoxTranslator();
|
---|
216 | ASMAtomicIncU32(&pInstance->m_cInstanceRefs);
|
---|
217 | RTCritSectRwLeaveExcl(&s_instanceRwLock);
|
---|
218 | return pInstance;
|
---|
219 | }
|
---|
220 | return NULL;
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 | /* static */
|
---|
225 | VirtualBoxTranslator *VirtualBoxTranslator::tryInstance() RT_NOEXCEPT
|
---|
226 | {
|
---|
227 | int rc = RTOnce(&g_Once, initLock, NULL);
|
---|
228 | if (RT_SUCCESS(rc))
|
---|
229 | {
|
---|
230 | RTCritSectRwEnterShared(&s_instanceRwLock);
|
---|
231 | VirtualBoxTranslator *pInstance = s_pInstance;
|
---|
232 | if (RT_LIKELY(pInstance != NULL))
|
---|
233 | {
|
---|
234 | uint32_t cRefs = ASMAtomicIncU32(&pInstance->m_cInstanceRefs);
|
---|
235 | Assert(cRefs > 1); Assert(cRefs < _8K); RT_NOREF(cRefs);
|
---|
236 | }
|
---|
237 | RTCritSectRwLeaveShared(&s_instanceRwLock);
|
---|
238 | return pInstance;
|
---|
239 | }
|
---|
240 | return NULL;
|
---|
241 | }
|
---|
242 |
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * Release translator reference previous obtained via instance() or
|
---|
246 | * tryinstance().
|
---|
247 | */
|
---|
248 | void VirtualBoxTranslator::release()
|
---|
249 | {
|
---|
250 | RTCritSectRwEnterShared(&s_instanceRwLock);
|
---|
251 | uint32_t cRefs = ASMAtomicDecU32(&m_cInstanceRefs);
|
---|
252 | Assert(cRefs < _8K);
|
---|
253 | if (RT_LIKELY(cRefs > 0))
|
---|
254 | RTCritSectRwLeaveShared(&s_instanceRwLock);
|
---|
255 | else
|
---|
256 | {
|
---|
257 | /* Looks like we've got the last reference. Must switch to exclusive
|
---|
258 | mode for safe cleanup. */
|
---|
259 | ASMAtomicIncU32(&m_cInstanceRefs);
|
---|
260 | RTCritSectRwLeaveShared(&s_instanceRwLock);
|
---|
261 | RTCritSectRwEnterExcl(&s_instanceRwLock);
|
---|
262 | cRefs = ASMAtomicDecU32(&m_cInstanceRefs);
|
---|
263 | Assert(cRefs < _8K);
|
---|
264 | if (cRefs == 0)
|
---|
265 | {
|
---|
266 | s_pInstance = NULL;
|
---|
267 | delete this;
|
---|
268 | }
|
---|
269 | RTCritSectRwLeaveExcl(&s_instanceRwLock);
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 |
|
---|
274 | HRESULT VirtualBoxTranslator::loadLanguage(ComPtr<IVirtualBox> aVirtualBox)
|
---|
275 | {
|
---|
276 | AssertReturn(aVirtualBox, E_INVALIDARG);
|
---|
277 |
|
---|
278 | ComPtr<ISystemProperties> pSystemProperties;
|
---|
279 | HRESULT hrc = aVirtualBox->COMGETTER(SystemProperties)(pSystemProperties.asOutParam());
|
---|
280 | if (SUCCEEDED(hrc))
|
---|
281 | {
|
---|
282 | com::Bstr bstrLocale;
|
---|
283 | hrc = pSystemProperties->COMGETTER(LanguageId)(bstrLocale.asOutParam());
|
---|
284 | if (SUCCEEDED(hrc))
|
---|
285 | {
|
---|
286 | int vrc = i_loadLanguage(com::Utf8Str(bstrLocale).c_str());
|
---|
287 | if (RT_FAILURE(vrc))
|
---|
288 | hrc = Global::vboxStatusCodeToCOM(vrc);
|
---|
289 | }
|
---|
290 | }
|
---|
291 | return hrc;
|
---|
292 | }
|
---|
293 |
|
---|
294 |
|
---|
295 | com::Utf8Str VirtualBoxTranslator::language()
|
---|
296 | {
|
---|
297 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
298 |
|
---|
299 | return m_strLanguage;
|
---|
300 | }
|
---|
301 |
|
---|
302 |
|
---|
303 | int VirtualBoxTranslator::i_loadLanguage(const char *pszLang)
|
---|
304 | {
|
---|
305 | LogFlowFunc(("pszLang=%s\n", pszLang));
|
---|
306 | int rc = VINF_SUCCESS;
|
---|
307 | char szLocale[256];
|
---|
308 | if (pszLang == NULL || *pszLang == '\0')
|
---|
309 | {
|
---|
310 | rc = vboxGetDefaultUserLanguage(szLocale, sizeof(szLocale));
|
---|
311 | if (RT_SUCCESS(rc))
|
---|
312 | pszLang = szLocale;
|
---|
313 | }
|
---|
314 | else
|
---|
315 | {
|
---|
316 | /* check the pszLang looks like language code, i.e. {ll} or {ll}_{CC} */
|
---|
317 | size_t cbLang = strlen(pszLang);
|
---|
318 | if ( !(cbLang == 1 && pszLang[0] == 'C')
|
---|
319 | && !(cbLang == 2 && RT_C_IS_LOWER(pszLang[0]) && RT_C_IS_LOWER(pszLang[1]))
|
---|
320 | && !(cbLang == 5 && RTLOCALE_IS_LANGUAGE2_UNDERSCORE_COUNTRY2(pszLang)))
|
---|
321 | rc = VERR_INVALID_PARAMETER;
|
---|
322 | }
|
---|
323 | if (RT_SUCCESS(rc))
|
---|
324 | {
|
---|
325 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
326 |
|
---|
327 | m_strLanguage = pszLang;
|
---|
328 |
|
---|
329 | for (TranslatorList::iterator it = m_lTranslators.begin();
|
---|
330 | it != m_lTranslators.end();
|
---|
331 | ++it)
|
---|
332 | {
|
---|
333 | /* ignore errors from particular translator allowing the use of others */
|
---|
334 | i_loadLanguageForComponent(&(*it), pszLang);
|
---|
335 | }
|
---|
336 | }
|
---|
337 | return rc;
|
---|
338 | }
|
---|
339 |
|
---|
340 |
|
---|
341 | int VirtualBoxTranslator::i_loadLanguageForComponent(TranslatorComponent *aComponent, const char *aLang)
|
---|
342 | {
|
---|
343 | AssertReturn(aComponent, VERR_INVALID_PARAMETER);
|
---|
344 | LogFlow(("aComponent=%s aLang=%s\n", aComponent->strPath.c_str(), aLang));
|
---|
345 |
|
---|
346 | int rc;
|
---|
347 | if (strcmp(aLang, "C") != 0)
|
---|
348 | {
|
---|
349 | /* Construct the base filename for the translations: */
|
---|
350 | char szNlsPath[RTPATH_MAX];
|
---|
351 | /* Try load language file on form 'VirtualBoxAPI_ll_CC.qm' if it exists
|
---|
352 | where 'll_CC' could for example be 'en_US' or 'de_CH': */
|
---|
353 | ssize_t cchOkay = RTStrPrintf2(szNlsPath, sizeof(szNlsPath), "%s_%s.qm",
|
---|
354 | aComponent->strPath.c_str(), aLang);
|
---|
355 | if (cchOkay > 0)
|
---|
356 | rc = i_setLanguageFile(aComponent, szNlsPath);
|
---|
357 | else
|
---|
358 | rc = VERR_FILENAME_TOO_LONG;
|
---|
359 | if (RT_FAILURE(rc))
|
---|
360 | {
|
---|
361 | /* No luck, drop the country part, i.e. 'VirtualBoxAPI_de.qm' or 'VirtualBoxAPI_en.qm': */
|
---|
362 | const char *pszDash = strchr(aLang, '_');
|
---|
363 | if (pszDash && pszDash != aLang)
|
---|
364 | {
|
---|
365 | cchOkay = RTStrPrintf2(szNlsPath, sizeof(szNlsPath), "%s_%.*s.qm",
|
---|
366 | aComponent->strPath.c_str(), pszDash - aLang, aLang);
|
---|
367 | if (cchOkay > 0)
|
---|
368 | rc = i_setLanguageFile(aComponent, szNlsPath);
|
---|
369 | }
|
---|
370 | }
|
---|
371 | }
|
---|
372 | else
|
---|
373 | {
|
---|
374 | /* No translator needed for 'C' */
|
---|
375 | delete aComponent->pTranslator;
|
---|
376 | aComponent->pTranslator = NULL;
|
---|
377 | rc = VINF_SUCCESS;
|
---|
378 | }
|
---|
379 | return rc;
|
---|
380 | }
|
---|
381 |
|
---|
382 |
|
---|
383 | int VirtualBoxTranslator::i_setLanguageFile(TranslatorComponent *aComponent, const char *aFileName)
|
---|
384 | {
|
---|
385 | AssertReturn(aComponent, VERR_INVALID_PARAMETER);
|
---|
386 |
|
---|
387 | int rc = m_rcCache;
|
---|
388 | if (m_hStrCache != NIL_RTSTRCACHE)
|
---|
389 | {
|
---|
390 | QMTranslator *pNewTranslator;
|
---|
391 | try { pNewTranslator = new QMTranslator(); }
|
---|
392 | catch (std::bad_alloc &) { pNewTranslator = NULL; }
|
---|
393 | if (pNewTranslator)
|
---|
394 | {
|
---|
395 | rc = pNewTranslator->load(aFileName, m_hStrCache);
|
---|
396 | if (RT_SUCCESS(rc))
|
---|
397 | {
|
---|
398 | if (aComponent->pTranslator)
|
---|
399 | delete aComponent->pTranslator;
|
---|
400 | aComponent->pTranslator = pNewTranslator;
|
---|
401 | }
|
---|
402 | else
|
---|
403 | delete pNewTranslator;
|
---|
404 | }
|
---|
405 | else
|
---|
406 | rc = VERR_NO_MEMORY;
|
---|
407 | }
|
---|
408 | else
|
---|
409 | Assert(RT_FAILURE_NP(rc));
|
---|
410 | return rc;
|
---|
411 | }
|
---|
412 |
|
---|
413 |
|
---|
414 | int VirtualBoxTranslator::registerTranslation(const char *aTranslationPath,
|
---|
415 | bool aDefault,
|
---|
416 | PTRCOMPONENT *aComponent)
|
---|
417 | {
|
---|
418 | VirtualBoxTranslator *pCurrInstance = VirtualBoxTranslator::tryInstance();
|
---|
419 | int rc = VERR_GENERAL_FAILURE;
|
---|
420 | if (pCurrInstance != NULL)
|
---|
421 | {
|
---|
422 | rc = pCurrInstance->i_registerTranslation(aTranslationPath, aDefault, aComponent);
|
---|
423 | pCurrInstance->release();
|
---|
424 | }
|
---|
425 | return rc;
|
---|
426 | }
|
---|
427 |
|
---|
428 |
|
---|
429 | int VirtualBoxTranslator::i_registerTranslation(const char *aTranslationPath,
|
---|
430 | bool aDefault,
|
---|
431 | PTRCOMPONENT *aComponent)
|
---|
432 | {
|
---|
433 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
434 | TranslatorComponent *pComponent;
|
---|
435 | for (TranslatorList::iterator it = m_lTranslators.begin();
|
---|
436 | it != m_lTranslators.end();
|
---|
437 | ++it)
|
---|
438 | {
|
---|
439 | if (it->strPath == aTranslationPath)
|
---|
440 | {
|
---|
441 | pComponent = &(*it);
|
---|
442 | if (aDefault)
|
---|
443 | m_pDefaultComponent = pComponent;
|
---|
444 | *aComponent = (PTRCOMPONENT)pComponent;
|
---|
445 | return VINF_SUCCESS;
|
---|
446 | }
|
---|
447 | }
|
---|
448 |
|
---|
449 | try
|
---|
450 | {
|
---|
451 | m_lTranslators.push_back(TranslatorComponent());
|
---|
452 | pComponent = &m_lTranslators.back();
|
---|
453 | }
|
---|
454 | catch(std::bad_alloc &)
|
---|
455 | {
|
---|
456 | return VERR_NO_MEMORY;
|
---|
457 | }
|
---|
458 |
|
---|
459 | pComponent->strPath = aTranslationPath;
|
---|
460 | if (aDefault)
|
---|
461 | m_pDefaultComponent = pComponent;
|
---|
462 | *aComponent = (PTRCOMPONENT)pComponent;
|
---|
463 | /* ignore the error during loading because path
|
---|
464 | * could contain no translation for current language */
|
---|
465 | i_loadLanguageForComponent(pComponent, m_strLanguage.c_str());
|
---|
466 | return VINF_SUCCESS;
|
---|
467 | }
|
---|
468 |
|
---|
469 |
|
---|
470 | int VirtualBoxTranslator::unregisterTranslation(PTRCOMPONENT aComponent)
|
---|
471 | {
|
---|
472 | int rc;
|
---|
473 | if (aComponent != NULL)
|
---|
474 | {
|
---|
475 | VirtualBoxTranslator *pCurrInstance = VirtualBoxTranslator::tryInstance();
|
---|
476 | if (pCurrInstance != NULL)
|
---|
477 | {
|
---|
478 | rc = pCurrInstance->i_unregisterTranslation(aComponent);
|
---|
479 | pCurrInstance->release();
|
---|
480 | }
|
---|
481 | else
|
---|
482 | rc = VERR_GENERAL_FAILURE;
|
---|
483 | }
|
---|
484 | else
|
---|
485 | rc = VWRN_NOT_FOUND;
|
---|
486 | return rc;
|
---|
487 | }
|
---|
488 |
|
---|
489 |
|
---|
490 | int VirtualBoxTranslator::i_unregisterTranslation(PTRCOMPONENT aComponent)
|
---|
491 | {
|
---|
492 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
493 |
|
---|
494 | if (aComponent == m_pDefaultComponent)
|
---|
495 | m_pDefaultComponent = NULL;
|
---|
496 |
|
---|
497 | for (TranslatorList::iterator it = m_lTranslators.begin();
|
---|
498 | it != m_lTranslators.end();
|
---|
499 | ++it)
|
---|
500 | {
|
---|
501 | if (&(*it) == aComponent)
|
---|
502 | {
|
---|
503 | delete aComponent->pTranslator;
|
---|
504 | m_lTranslators.erase(it);
|
---|
505 | return VINF_SUCCESS;
|
---|
506 | }
|
---|
507 | }
|
---|
508 |
|
---|
509 | return VERR_NOT_FOUND;
|
---|
510 | }
|
---|
511 |
|
---|
512 |
|
---|
513 | const char *VirtualBoxTranslator::translate(PTRCOMPONENT aComponent,
|
---|
514 | const char *aContext,
|
---|
515 | const char *aSourceText,
|
---|
516 | const char *aComment,
|
---|
517 | const size_t aNum) RT_NOEXCEPT
|
---|
518 | {
|
---|
519 | VirtualBoxTranslator *pCurrInstance = VirtualBoxTranslator::tryInstance();
|
---|
520 | const char *pszTranslation = aSourceText;
|
---|
521 | if (pCurrInstance != NULL)
|
---|
522 | {
|
---|
523 | pszTranslation = pCurrInstance->i_translate(aComponent, aContext, aSourceText, aComment, aNum);
|
---|
524 | pCurrInstance->release();
|
---|
525 | }
|
---|
526 | return pszTranslation;
|
---|
527 | }
|
---|
528 |
|
---|
529 |
|
---|
530 | const char *VirtualBoxTranslator::i_translate(PTRCOMPONENT aComponent,
|
---|
531 | const char *aContext,
|
---|
532 | const char *aSourceText,
|
---|
533 | const char *aComment,
|
---|
534 | const size_t aNum) RT_NOEXCEPT
|
---|
535 | {
|
---|
536 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
537 |
|
---|
538 | if (aComponent == NULL)
|
---|
539 | aComponent = m_pDefaultComponent;
|
---|
540 |
|
---|
541 | if ( aComponent == NULL
|
---|
542 | || aComponent->pTranslator == NULL)
|
---|
543 | return aSourceText;
|
---|
544 |
|
---|
545 | const char *pszSafeSource = NULL;
|
---|
546 | const char *pszTranslation = aComponent->pTranslator->translate(aContext, aSourceText, &pszSafeSource, aComment, aNum);
|
---|
547 | if (pszSafeSource && g_idxTlsSrc != NIL_RTTLS && g_idxTlsTr != NIL_RTTLS)
|
---|
548 | {
|
---|
549 | RTTlsSet(g_idxTlsTr, (void *)pszTranslation);
|
---|
550 | RTTlsSet(g_idxTlsSrc, (void *)pszSafeSource);
|
---|
551 | }
|
---|
552 |
|
---|
553 | return pszTranslation;
|
---|
554 | }
|
---|
555 |
|
---|
556 |
|
---|
557 | const char *VirtualBoxTranslator::trSource(const char *aTranslation) RT_NOEXCEPT
|
---|
558 | {
|
---|
559 | const char *pszSource = aTranslation;
|
---|
560 | VirtualBoxTranslator *pCurInstance = VirtualBoxTranslator::tryInstance(); /* paranoia */
|
---|
561 | if (pCurInstance != NULL)
|
---|
562 | {
|
---|
563 | if (g_idxTlsSrc != NIL_RTTLS && g_idxTlsTr != NIL_RTTLS)
|
---|
564 | {
|
---|
565 | const char * const pszTranslationTls = (const char *)RTTlsGet(g_idxTlsTr);
|
---|
566 | const char * const pszSourceTls = (const char *)RTTlsGet(g_idxTlsSrc);
|
---|
567 | if ( pszSourceTls != NULL
|
---|
568 | && pszTranslationTls != NULL
|
---|
569 | && ( pszTranslationTls == aTranslation
|
---|
570 | || strcmp(pszTranslationTls, aTranslation) == 0))
|
---|
571 | pszSource = pszSourceTls;
|
---|
572 | }
|
---|
573 | pCurInstance->release();
|
---|
574 | }
|
---|
575 | return pszSource;
|
---|
576 | }
|
---|
577 |
|
---|
578 |
|
---|
579 | int32_t VirtualBoxTranslator::initCritSect()
|
---|
580 | {
|
---|
581 | return RTCritSectRwInit(&s_instanceRwLock);
|
---|
582 | }
|
---|
583 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|