VirtualBox

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

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

Main/NLS: Must check that pszSourceTls and pszTranslationTls aren't NULL. Use the simple RTTls functions. bugref:1909

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

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