VirtualBox

source: vbox/trunk/src/VBox/Main/src-all/VirtualBoxTranslator.cpp@ 93412

Last change on this file since 93412 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.4 KB
Line 
1/* $Id: VirtualBoxTranslator.cpp 93115 2022-01-01 11:31:46Z 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/err.h>
28#include <iprt/locale.h>
29#include <iprt/once.h>
30#include <iprt/path.h>
31#include <iprt/string.h>
32#include <iprt/thread.h>
33#include <iprt/strcache.h>
34
35#include "Global.h"
36#include "VirtualBoxBase.h"
37#include "QMTranslator.h"
38#include "VirtualBoxTranslator.h"
39
40
41/*********************************************************************************************************************************
42* Defined Constants And Macros *
43*********************************************************************************************************************************/
44#define TRANSLATOR_CACHE_SIZE 32
45
46
47/*********************************************************************************************************************************
48* Global Variables *
49*********************************************************************************************************************************/
50/** Init once for the critical section. */
51static RTONCE g_Once = RTONCE_INITIALIZER;
52RTCRITSECTRW VirtualBoxTranslator::s_instanceRwLock;
53VirtualBoxTranslator *VirtualBoxTranslator::s_pInstance = NULL;
54/** TLS index that points to the translated text. */
55static RTTLS g_idxTlsTr = NIL_RTTLS;
56/** TLS index that points to the original text. */
57static RTTLS g_idxTlsSrc = NIL_RTTLS;
58
59
60
61/**
62 * @callback_method_impl{FNRTONCE}
63 */
64static DECLCALLBACK(int32_t) initLock(void *pvUser)
65{
66 RT_NOREF(pvUser);
67 return VirtualBoxTranslator::initCritSect();
68}
69
70
71VirtualBoxTranslator::VirtualBoxTranslator()
72 : util::RWLockHandle(util::LOCKCLASS_TRANSLATOR)
73 , m_cInstanceRefs(0)
74 , m_pDefaultComponent(NULL)
75 , m_strLanguage("C")
76 , m_hStrCache(NIL_RTSTRCACHE)
77{
78 g_idxTlsTr = RTTlsAlloc();
79 g_idxTlsSrc = RTTlsAlloc();
80 int rc = RTStrCacheCreate(&m_hStrCache, "API Translation");
81 m_rcCache = rc;
82 if (RT_FAILURE(rc))
83 m_hStrCache = NIL_RTSTRCACHE; /* (loadLanguage will fail) */
84 LogFlowFunc(("m_rcCache=%Rrc g_idxTlsTr=%#x g_idxTlsSrc=%#x\n", m_rcCache, g_idxTlsTr, g_idxTlsSrc));
85}
86
87
88VirtualBoxTranslator::~VirtualBoxTranslator()
89{
90 LogFlowFunc(("enter\n"));
91
92 /* Write-lock the object as we could be racing language change
93 notifications processing during XPCOM shutdown. (risky?) */
94 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
95
96 RTTlsFree(g_idxTlsTr);
97 g_idxTlsTr = NIL_RTTLS;
98 RTTlsFree(g_idxTlsSrc);
99 g_idxTlsSrc = NIL_RTTLS;
100
101 m_pDefaultComponent = NULL;
102
103 for (TranslatorList::iterator it = m_lTranslators.begin();
104 it != m_lTranslators.end();
105 ++it)
106 {
107 if (it->pTranslator != NULL)
108 delete it->pTranslator;
109 it->pTranslator = NULL;
110 }
111 if (m_hStrCache != NIL_RTSTRCACHE)
112 {
113 RTStrCacheDestroy(m_hStrCache);
114 m_hStrCache = NIL_RTSTRCACHE;
115 m_rcCache = VERR_WRONG_ORDER;
116 }
117 LogFlowFunc(("returns\n"));
118}
119
120
121/**
122 * Get or create a translator instance (singelton), referenced.
123 *
124 * The main reference is held by the main VBox singelton objects (VirtualBox,
125 * VirtualBoxClient) tying it's lifetime to theirs.
126 */
127/* static */
128VirtualBoxTranslator *VirtualBoxTranslator::instance()
129{
130 int rc = RTOnce(&g_Once, initLock, NULL);
131 if (RT_SUCCESS(rc))
132 {
133 RTCritSectRwEnterShared(&s_instanceRwLock);
134 VirtualBoxTranslator *pInstance = s_pInstance;
135 if (RT_LIKELY(pInstance != NULL))
136 {
137 uint32_t cRefs = ASMAtomicIncU32(&pInstance->m_cInstanceRefs);
138 Assert(cRefs > 1); Assert(cRefs < _8K); RT_NOREF(cRefs);
139 RTCritSectRwLeaveShared(&s_instanceRwLock);
140 return pInstance;
141 }
142
143 /* Maybe create the instance: */
144 RTCritSectRwLeaveShared(&s_instanceRwLock);
145 RTCritSectRwEnterExcl(&s_instanceRwLock);
146 pInstance = s_pInstance;
147 if (pInstance == NULL)
148 s_pInstance = pInstance = new VirtualBoxTranslator();
149 ASMAtomicIncU32(&pInstance->m_cInstanceRefs);
150 RTCritSectRwLeaveExcl(&s_instanceRwLock);
151 return pInstance;
152 }
153 return NULL;
154}
155
156
157/* static */
158VirtualBoxTranslator *VirtualBoxTranslator::tryInstance() RT_NOEXCEPT
159{
160 int rc = RTOnce(&g_Once, initLock, NULL);
161 if (RT_SUCCESS(rc))
162 {
163 RTCritSectRwEnterShared(&s_instanceRwLock);
164 VirtualBoxTranslator *pInstance = s_pInstance;
165 if (RT_LIKELY(pInstance != NULL))
166 {
167 uint32_t cRefs = ASMAtomicIncU32(&pInstance->m_cInstanceRefs);
168 Assert(cRefs > 1); Assert(cRefs < _8K); RT_NOREF(cRefs);
169 }
170 RTCritSectRwLeaveShared(&s_instanceRwLock);
171 return pInstance;
172 }
173 return NULL;
174}
175
176
177/**
178 * Release translator reference previous obtained via instance() or
179 * tryinstance().
180 */
181void VirtualBoxTranslator::release()
182{
183 RTCritSectRwEnterShared(&s_instanceRwLock);
184 uint32_t cRefs = ASMAtomicDecU32(&m_cInstanceRefs);
185 Assert(cRefs < _8K);
186 if (RT_LIKELY(cRefs > 0))
187 RTCritSectRwLeaveShared(&s_instanceRwLock);
188 else
189 {
190 /* Looks like we've got the last reference. Must switch to exclusive
191 mode for safe cleanup. */
192 ASMAtomicIncU32(&m_cInstanceRefs);
193 RTCritSectRwLeaveShared(&s_instanceRwLock);
194 RTCritSectRwEnterExcl(&s_instanceRwLock);
195 cRefs = ASMAtomicDecU32(&m_cInstanceRefs);
196 Assert(cRefs < _8K);
197 if (cRefs == 0)
198 {
199 s_pInstance = NULL;
200 delete this;
201 }
202 RTCritSectRwLeaveExcl(&s_instanceRwLock);
203 }
204}
205
206
207HRESULT VirtualBoxTranslator::loadLanguage(ComPtr<IVirtualBox> aVirtualBox)
208{
209 AssertReturn(aVirtualBox, E_INVALIDARG);
210
211 ComPtr<ISystemProperties> pSystemProperties;
212 HRESULT hrc = aVirtualBox->COMGETTER(SystemProperties)(pSystemProperties.asOutParam());
213 if (SUCCEEDED(hrc))
214 {
215 com::Bstr bstrLocale;
216 hrc = pSystemProperties->COMGETTER(LanguageId)(bstrLocale.asOutParam());
217 if (SUCCEEDED(hrc))
218 {
219 int vrc = i_loadLanguage(com::Utf8Str(bstrLocale).c_str());
220 if (RT_FAILURE(vrc))
221 hrc = Global::vboxStatusCodeToCOM(vrc);
222 }
223 }
224 return hrc;
225}
226
227
228com::Utf8Str VirtualBoxTranslator::language()
229{
230 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
231
232 return m_strLanguage;
233}
234
235
236int VirtualBoxTranslator::i_loadLanguage(const char *pszLang)
237{
238 LogFlowFunc(("pszLang=%s\n", pszLang));
239 int rc = VINF_SUCCESS;
240 char szLocale[256];
241 if (pszLang == NULL || *pszLang == '\0')
242 {
243 rc = RTLocaleQueryNormalizedBaseLocaleName(szLocale, sizeof(szLocale));
244 if (RT_SUCCESS(rc))
245 pszLang = szLocale;
246 }
247 else
248 {
249 /* check the pszLang looks like language code, i.e. {ll} or {ll}_{CC} */
250 size_t cbLang = strlen(pszLang);
251 if ( !(cbLang == 1 && pszLang[0] == 'C')
252 && !(cbLang == 2 && RT_C_IS_LOWER(pszLang[0]) && RT_C_IS_LOWER(pszLang[1]))
253 && !(cbLang == 5 && RTLOCALE_IS_LANGUAGE2_UNDERSCORE_COUNTRY2(pszLang)))
254 rc = VERR_INVALID_PARAMETER;
255 }
256 if (RT_SUCCESS(rc))
257 {
258 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
259
260 m_strLanguage = pszLang;
261
262 for (TranslatorList::iterator it = m_lTranslators.begin();
263 it != m_lTranslators.end();
264 ++it)
265 {
266 /* ignore errors from particular translator allowing the use of others */
267 i_loadLanguageForComponent(&(*it), pszLang);
268 }
269 }
270 return rc;
271}
272
273
274int VirtualBoxTranslator::i_loadLanguageForComponent(TranslatorComponent *aComponent, const char *aLang)
275{
276 AssertReturn(aComponent, VERR_INVALID_PARAMETER);
277 LogFlow(("aComponent=%s aLang=%s\n", aComponent->strPath.c_str(), aLang));
278
279 int rc;
280 if (strcmp(aLang, "C") != 0)
281 {
282 /* Construct the base filename for the translations: */
283 char szNlsPath[RTPATH_MAX];
284 /* Try load language file on form 'VirtualBoxAPI_ll_CC.qm' if it exists
285 where 'll_CC' could for example be 'en_US' or 'de_CH': */
286 ssize_t cchOkay = RTStrPrintf2(szNlsPath, sizeof(szNlsPath), "%s_%s.qm",
287 aComponent->strPath.c_str(), aLang);
288 if (cchOkay > 0)
289 rc = i_setLanguageFile(aComponent, szNlsPath);
290 else
291 rc = VERR_FILENAME_TOO_LONG;
292 if (RT_FAILURE(rc))
293 {
294 /* No luck, drop the country part, i.e. 'VirtualBoxAPI_de.qm' or 'VirtualBoxAPI_en.qm': */
295 const char *pszDash = strchr(aLang, '_');
296 if (pszDash && pszDash != aLang)
297 {
298 cchOkay = RTStrPrintf2(szNlsPath, sizeof(szNlsPath), "%s_%.*s.qm",
299 aComponent->strPath.c_str(), pszDash - aLang, aLang);
300 if (cchOkay > 0)
301 rc = i_setLanguageFile(aComponent, szNlsPath);
302 }
303 }
304 }
305 else
306 {
307 /* No translator needed for 'C' */
308 delete aComponent->pTranslator;
309 aComponent->pTranslator = NULL;
310 rc = VINF_SUCCESS;
311 }
312 return rc;
313}
314
315
316int VirtualBoxTranslator::i_setLanguageFile(TranslatorComponent *aComponent, const char *aFileName)
317{
318 AssertReturn(aComponent, VERR_INVALID_PARAMETER);
319
320 int rc = m_rcCache;
321 if (m_hStrCache != NIL_RTSTRCACHE)
322 {
323 QMTranslator *pNewTranslator;
324 try { pNewTranslator = new QMTranslator(); }
325 catch (std::bad_alloc &) { pNewTranslator = NULL; }
326 if (pNewTranslator)
327 {
328 rc = pNewTranslator->load(aFileName, m_hStrCache);
329 if (RT_SUCCESS(rc))
330 {
331 if (aComponent->pTranslator)
332 delete aComponent->pTranslator;
333 aComponent->pTranslator = pNewTranslator;
334 }
335 else
336 delete pNewTranslator;
337 }
338 else
339 rc = VERR_NO_MEMORY;
340 }
341 else
342 Assert(RT_FAILURE_NP(rc));
343 return rc;
344}
345
346
347int VirtualBoxTranslator::registerTranslation(const char *aTranslationPath,
348 bool aDefault,
349 PTRCOMPONENT *aComponent)
350{
351 VirtualBoxTranslator *pCurrInstance = VirtualBoxTranslator::tryInstance();
352 int rc = VERR_GENERAL_FAILURE;
353 if (pCurrInstance != NULL)
354 {
355 rc = pCurrInstance->i_registerTranslation(aTranslationPath, aDefault, aComponent);
356 pCurrInstance->release();
357 }
358 return rc;
359}
360
361
362int VirtualBoxTranslator::i_registerTranslation(const char *aTranslationPath,
363 bool aDefault,
364 PTRCOMPONENT *aComponent)
365{
366 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
367 TranslatorComponent *pComponent;
368 for (TranslatorList::iterator it = m_lTranslators.begin();
369 it != m_lTranslators.end();
370 ++it)
371 {
372 if (it->strPath == aTranslationPath)
373 {
374 pComponent = &(*it);
375 if (aDefault)
376 m_pDefaultComponent = pComponent;
377 *aComponent = (PTRCOMPONENT)pComponent;
378 return VINF_SUCCESS;
379 }
380 }
381
382 try
383 {
384 m_lTranslators.push_back(TranslatorComponent());
385 pComponent = &m_lTranslators.back();
386 }
387 catch(std::bad_alloc &)
388 {
389 return VERR_NO_MEMORY;
390 }
391
392 pComponent->strPath = aTranslationPath;
393 if (aDefault)
394 m_pDefaultComponent = pComponent;
395 *aComponent = (PTRCOMPONENT)pComponent;
396 /* ignore the error during loading because path
397 * could contain no translation for current language */
398 i_loadLanguageForComponent(pComponent, m_strLanguage.c_str());
399 return VINF_SUCCESS;
400}
401
402
403int VirtualBoxTranslator::unregisterTranslation(PTRCOMPONENT aComponent)
404{
405 int rc;
406 if (aComponent != NULL)
407 {
408 VirtualBoxTranslator *pCurrInstance = VirtualBoxTranslator::tryInstance();
409 if (pCurrInstance != NULL)
410 {
411 rc = pCurrInstance->i_unregisterTranslation(aComponent);
412 pCurrInstance->release();
413 }
414 else
415 rc = VERR_GENERAL_FAILURE;
416 }
417 else
418 rc = VWRN_NOT_FOUND;
419 return rc;
420}
421
422
423int VirtualBoxTranslator::i_unregisterTranslation(PTRCOMPONENT aComponent)
424{
425 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
426
427 if (aComponent == m_pDefaultComponent)
428 m_pDefaultComponent = NULL;
429
430 for (TranslatorList::iterator it = m_lTranslators.begin();
431 it != m_lTranslators.end();
432 ++it)
433 {
434 if (&(*it) == aComponent)
435 {
436 delete aComponent->pTranslator;
437 m_lTranslators.erase(it);
438 return VINF_SUCCESS;
439 }
440 }
441
442 return VERR_NOT_FOUND;
443}
444
445
446const char *VirtualBoxTranslator::translate(PTRCOMPONENT aComponent,
447 const char *aContext,
448 const char *aSourceText,
449 const char *aComment,
450 const size_t aNum) RT_NOEXCEPT
451{
452 VirtualBoxTranslator *pCurrInstance = VirtualBoxTranslator::tryInstance();
453 const char *pszTranslation = aSourceText;
454 if (pCurrInstance != NULL)
455 {
456 pszTranslation = pCurrInstance->i_translate(aComponent, aContext, aSourceText, aComment, aNum);
457 pCurrInstance->release();
458 }
459 return pszTranslation;
460}
461
462
463const char *VirtualBoxTranslator::i_translate(PTRCOMPONENT aComponent,
464 const char *aContext,
465 const char *aSourceText,
466 const char *aComment,
467 const size_t aNum) RT_NOEXCEPT
468{
469 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
470
471 if (aComponent == NULL)
472 aComponent = m_pDefaultComponent;
473
474 if ( aComponent == NULL
475 || aComponent->pTranslator == NULL)
476 return aSourceText;
477
478 const char *pszSafeSource = NULL;
479 const char *pszTranslation = aComponent->pTranslator->translate(aContext, aSourceText, &pszSafeSource, aComment, aNum);
480 if (pszSafeSource && g_idxTlsSrc != NIL_RTTLS && g_idxTlsTr != NIL_RTTLS)
481 {
482 RTTlsSet(g_idxTlsTr, (void *)pszTranslation);
483 RTTlsSet(g_idxTlsSrc, (void *)pszSafeSource);
484 }
485
486 return pszTranslation;
487}
488
489
490const char *VirtualBoxTranslator::trSource(const char *aTranslation) RT_NOEXCEPT
491{
492 const char *pszSource = aTranslation;
493 VirtualBoxTranslator *pCurInstance = VirtualBoxTranslator::tryInstance(); /* paranoia */
494 if (pCurInstance != NULL)
495 {
496 if (g_idxTlsSrc != NIL_RTTLS && g_idxTlsTr != NIL_RTTLS)
497 {
498 const char * const pszTranslationTls = (const char *)RTTlsGet(g_idxTlsTr);
499 const char * const pszSourceTls = (const char *)RTTlsGet(g_idxTlsSrc);
500 if ( pszSourceTls != NULL
501 && pszTranslationTls != NULL
502 && ( pszTranslationTls == aTranslation
503 || strcmp(pszTranslationTls, aTranslation) == 0))
504 pszSource = pszSourceTls;
505 }
506 pCurInstance->release();
507 }
508 return pszSource;
509}
510
511
512int32_t VirtualBoxTranslator::initCritSect()
513{
514 return RTCritSectRwInit(&s_instanceRwLock);
515}
516/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette