summaryrefslogtreecommitdiffstats
path: root/src/VBox/Main/src-all/VirtualBoxTranslator.cpp
blob: acd4a6dc7271ece624858062fe012c98106818ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/* $Id: VirtualBoxTranslator.cpp $ */
/** @file
 * VirtualBox Translator class.
 */

/*
 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_MAIN_VIRTUALBOXCLIENT /** @todo add separate logging group! */
#include "LoggingNew.h"

#include <iprt/asm.h>
#include <iprt/ctype.h>
#include <iprt/env.h>
#include <iprt/err.h>
#include <iprt/locale.h>
#include <iprt/once.h>
#include <iprt/path.h>
#include <iprt/string.h>
#include <iprt/thread.h>
#include <iprt/strcache.h>

#ifdef RT_OS_DARWIN
#include <CoreFoundation/CFLocale.h>
#include <CoreFoundation/CFString.h>
#endif

#include "Global.h"
#include "VirtualBoxBase.h"
#include "QMTranslator.h"
#include "VirtualBoxTranslator.h"


/*********************************************************************************************************************************
*   Defined Constants And Macros                                                                                                 *
*********************************************************************************************************************************/
#define TRANSLATOR_CACHE_SIZE 32


/*********************************************************************************************************************************
*   Global Variables                                                                                                             *
*********************************************************************************************************************************/
/** Init once for the critical section. */
static RTONCE               g_Once = RTONCE_INITIALIZER;
RTCRITSECTRW                VirtualBoxTranslator::s_instanceRwLock;
VirtualBoxTranslator       *VirtualBoxTranslator::s_pInstance = NULL;
/** TLS index that points to the translated text. */
static RTTLS                g_idxTlsTr = NIL_RTTLS;
/** TLS index that points to the original text. */
static RTTLS                g_idxTlsSrc = NIL_RTTLS;


/**
 * @callback_method_impl{FNRTONCE}
 */
static DECLCALLBACK(int32_t) initLock(void *pvUser)
{
    RT_NOREF(pvUser);
    return VirtualBoxTranslator::initCritSect();
}


/**
 * Obtains the user language code in ll_CC form depending on platform
 *
 * @returns VBox status code
 * @param pszName   The buffer for storing user language code
 * @param cbName    Size of the pszName buffer
 */
static int vboxGetDefaultUserLanguage(char *pszName, size_t cbName)
{
    AssertReturn(pszName, VERR_INVALID_PARAMETER);
    AssertReturn(cbName >= 6, VERR_INVALID_PARAMETER); /* 5 chars for language + null termination */

#ifdef RT_OS_WINDOWS
    if (   GetLocaleInfoA(GetUserDefaultLCID(), LOCALE_SISO639LANGNAME, pszName, (int)cbName) == 3
        && GetLocaleInfoA(GetUserDefaultLCID(), LOCALE_SISO3166CTRYNAME, &pszName[3], (int)cbName - 4) == 3)
    {
        pszName[2] = '_';
        Assert(RTLOCALE_IS_LANGUAGE2_UNDERSCORE_COUNTRY2(pszName));
        return VINF_SUCCESS;
    }
#elif RT_OS_DARWIN
    CFLocaleRef locale = CFLocaleCopyCurrent();
    CFTypeRef localeId = CFLocaleGetValue (locale, kCFLocaleIdentifier);
    char szLocale[256] = { 0 };
    if (CFGetTypeID(localeId) == CFStringGetTypeID())
        CFStringGetCString((CFStringRef)localeId, szLocale, sizeof(szLocale), kCFStringEncodingUTF8);
    /* Some cleanup */
    CFRelease(locale);
    if (szLocale[0] == '\0')
    {
        pszName[0] = 'C';
        pszName[1] = 0;
        return VINF_SUCCESS;
    }
    else
        return RTStrCopy(pszName, cbName, szLocale);

#elif defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD) || defined(RT_OS_NETBSD) || defined(RT_OS_OPENBSD) || defined(RT_OS_SOLARIS)
    const char *pszValue = RTEnvGet("LC_ALL");
    if (pszValue == 0)
        pszValue = RTEnvGet("LC_MESSAGES");
    if (pszValue == 0)
        pszValue = RTEnvGet("LANG");
    if (pszValue != 0)
    {
        /* ignore codepage part, i.e. ignore ".UTF-8" in "ru_RU.UTF-8" */
        const char *pszDot = strchr(pszValue, '.');
        size_t cbValue = strlen(pszValue);
        if (pszDot != NULL)
          cbValue = RT_MIN(cbValue, (size_t)(pszDot - pszValue));

        if (   (   cbValue == 2
                && RT_C_IS_LOWER(pszValue[0])
                && RT_C_IS_LOWER(pszValue[1]))
            || (   cbValue == 5
                && RTLOCALE_IS_LANGUAGE2_UNDERSCORE_COUNTRY2(pszValue)))
            return RTStrCopyEx(pszName, cbName, pszValue, cbValue);
    }
#endif
    return RTLocaleQueryNormalizedBaseLocaleName(pszName, cbName);
}

VirtualBoxTranslator::VirtualBoxTranslator()
    : util::RWLockHandle(util::LOCKCLASS_TRANSLATOR)
    , m_cInstanceRefs(0)
    , m_pDefaultComponent(NULL)
    , m_strLanguage("C")
    , m_hStrCache(NIL_RTSTRCACHE)
{
    g_idxTlsTr  = RTTlsAlloc();
    g_idxTlsSrc = RTTlsAlloc();
    int vrc = RTStrCacheCreate(&m_hStrCache, "API Translation");
    m_rcCache = vrc;
    if (RT_FAILURE(vrc))
        m_hStrCache = NIL_RTSTRCACHE; /* (loadLanguage will fail) */
    LogFlowFunc(("m_rcCache=%Rrc g_idxTlsTr=%#x g_idxTlsSrc=%#x\n", m_rcCache, g_idxTlsTr, g_idxTlsSrc));
}


VirtualBoxTranslator::~VirtualBoxTranslator()
{
    LogFlowFunc(("enter\n"));

    /* Write-lock the object as we could be racing language change
       notifications processing during XPCOM shutdown. (risky?) */
    AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);

    RTTlsFree(g_idxTlsTr);
    g_idxTlsTr = NIL_RTTLS;
    RTTlsFree(g_idxTlsSrc);
    g_idxTlsSrc = NIL_RTTLS;

    m_pDefaultComponent = NULL;

    for (TranslatorList::iterator it = m_lTranslators.begin();
         it != m_lTranslators.end();
         ++it)
    {
        if (it->pTranslator != NULL)
            delete it->pTranslator;
        it->pTranslator = NULL;
    }
    if (m_hStrCache != NIL_RTSTRCACHE)
    {
        RTStrCacheDestroy(m_hStrCache);
        m_hStrCache = NIL_RTSTRCACHE;
        m_rcCache = VERR_WRONG_ORDER;
    }
    LogFlowFunc(("returns\n"));
}


/**
 * Get or create a translator instance (singelton), referenced.
 *
 * The main reference is held by the main VBox singelton objects (VirtualBox,
 * VirtualBoxClient) tying it's lifetime to theirs.
 */
/* static */
VirtualBoxTranslator *VirtualBoxTranslator::instance()
{
    int vrc = RTOnce(&g_Once, initLock, NULL);
    if (RT_SUCCESS(vrc))
    {
        RTCritSectRwEnterShared(&s_instanceRwLock);
        VirtualBoxTranslator *pInstance = s_pInstance;
        if (RT_LIKELY(pInstance != NULL))
        {
            uint32_t cRefs = ASMAtomicIncU32(&pInstance->m_cInstanceRefs);
            Assert(cRefs > 1); Assert(cRefs < _8K); RT_NOREF(cRefs);
            RTCritSectRwLeaveShared(&s_instanceRwLock);
            return pInstance;
        }

        /* Maybe create the instance: */
        RTCritSectRwLeaveShared(&s_instanceRwLock);
        RTCritSectRwEnterExcl(&s_instanceRwLock);
        pInstance = s_pInstance;
        if (pInstance == NULL)
            s_pInstance = pInstance = new VirtualBoxTranslator();
        ASMAtomicIncU32(&pInstance->m_cInstanceRefs);
        RTCritSectRwLeaveExcl(&s_instanceRwLock);
        return pInstance;
    }
    return NULL;
}


/* static */
VirtualBoxTranslator *VirtualBoxTranslator::tryInstance() RT_NOEXCEPT
{
    int vrc = RTOnce(&g_Once, initLock, NULL);
    if (RT_SUCCESS(vrc))
    {
        RTCritSectRwEnterShared(&s_instanceRwLock);
        VirtualBoxTranslator *pInstance = s_pInstance;
        if (RT_LIKELY(pInstance != NULL))
        {
            uint32_t cRefs = ASMAtomicIncU32(&pInstance->m_cInstanceRefs);
            Assert(cRefs > 1); Assert(cRefs < _8K); RT_NOREF(cRefs);
        }
        RTCritSectRwLeaveShared(&s_instanceRwLock);
        return pInstance;
    }
    return NULL;
}


/**
 * Release translator reference previous obtained via instance() or
 * tryinstance().
 */
void VirtualBoxTranslator::release()
{
    RTCritSectRwEnterShared(&s_instanceRwLock);
    uint32_t cRefs = ASMAtomicDecU32(&m_cInstanceRefs);
    Assert(cRefs < _8K);
    if (RT_LIKELY(cRefs > 0))
        RTCritSectRwLeaveShared(&s_instanceRwLock);
    else
    {
        /* Looks like we've got the last reference. Must switch to exclusive
           mode for safe cleanup. */
        ASMAtomicIncU32(&m_cInstanceRefs);
        RTCritSectRwLeaveShared(&s_instanceRwLock);
        RTCritSectRwEnterExcl(&s_instanceRwLock);
        cRefs = ASMAtomicDecU32(&m_cInstanceRefs);
        Assert(cRefs < _8K);
        if (cRefs == 0)
        {
            s_pInstance = NULL;
            delete this;
        }
        RTCritSectRwLeaveExcl(&s_instanceRwLock);
    }
}


HRESULT VirtualBoxTranslator::loadLanguage(ComPtr<IVirtualBox> aVirtualBox)
{
    AssertReturn(aVirtualBox, E_INVALIDARG);

    ComPtr<ISystemProperties> pSystemProperties;
    HRESULT hrc = aVirtualBox->COMGETTER(SystemProperties)(pSystemProperties.asOutParam());
    if (SUCCEEDED(hrc))
    {
        com::Bstr bstrLocale;
        hrc = pSystemProperties->COMGETTER(LanguageId)(bstrLocale.asOutParam());
        if (SUCCEEDED(hrc))
        {
            int vrc = i_loadLanguage(com::Utf8Str(bstrLocale).c_str());
            if (RT_FAILURE(vrc))
                hrc = Global::vboxStatusCodeToCOM(vrc);
        }
    }
    return hrc;
}


com::Utf8Str VirtualBoxTranslator::language()
{
    AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);

    return m_strLanguage;
}


int VirtualBoxTranslator::i_loadLanguage(const char *pszLang)
{
    LogFlowFunc(("pszLang=%s\n", pszLang));
    int  vrc = VINF_SUCCESS;
    char szLocale[256];
    if (pszLang == NULL || *pszLang == '\0')
    {
        vrc = vboxGetDefaultUserLanguage(szLocale, sizeof(szLocale));
        if (RT_SUCCESS(vrc))
            pszLang = szLocale;
    }
    else
    {
        /* check the pszLang looks like language code, i.e. {ll} or {ll}_{CC} */
        size_t cbLang = strlen(pszLang);
        if (   !(cbLang == 1 && pszLang[0] == 'C')
            && !(cbLang == 2 && RT_C_IS_LOWER(pszLang[0]) && RT_C_IS_LOWER(pszLang[1]))
            && !(cbLang == 5 && RTLOCALE_IS_LANGUAGE2_UNDERSCORE_COUNTRY2(pszLang)))
            vrc = VERR_INVALID_PARAMETER;
    }
    if (RT_SUCCESS(vrc))
    {
        AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);

        m_strLanguage = pszLang;

        for (TranslatorList::iterator it = m_lTranslators.begin();
             it != m_lTranslators.end();
             ++it)
        {
            /* ignore errors from particular translator allowing the use of others */
            i_loadLanguageForComponent(&(*it), pszLang);
        }
    }
    return vrc;
}


int VirtualBoxTranslator::i_loadLanguageForComponent(TranslatorComponent *aComponent, const char *aLang)
{
    AssertReturn(aComponent, VERR_INVALID_PARAMETER);
    LogFlow(("aComponent=%s aLang=%s\n", aComponent->strPath.c_str(), aLang));

    int vrc;
    if (strcmp(aLang, "C") != 0)
    {
        /* Construct the base filename for the translations: */
        char szNlsPath[RTPATH_MAX];
        /* Try load language file on form 'VirtualBoxAPI_ll_CC.qm' if it exists
           where 'll_CC' could for example be 'en_US' or 'de_CH': */
        ssize_t cchOkay = RTStrPrintf2(szNlsPath, sizeof(szNlsPath), "%s_%s.qm",
                                       aComponent->strPath.c_str(), aLang);
        if (cchOkay > 0)
            vrc = i_setLanguageFile(aComponent, szNlsPath);
        else
            vrc = VERR_FILENAME_TOO_LONG;
        if (RT_FAILURE(vrc))
        {
            /* No luck, drop the country part, i.e. 'VirtualBoxAPI_de.qm' or 'VirtualBoxAPI_en.qm': */
            const char *pszDash = strchr(aLang, '_');
            if (pszDash && pszDash != aLang)
            {
                cchOkay = RTStrPrintf2(szNlsPath, sizeof(szNlsPath), "%s_%.*s.qm",
                                       aComponent->strPath.c_str(), pszDash - aLang, aLang);
                if (cchOkay > 0)
                    vrc = i_setLanguageFile(aComponent, szNlsPath);
            }
        }
    }
    else
    {
        /* No translator needed for 'C' */
        delete aComponent->pTranslator;
        aComponent->pTranslator = NULL;
        vrc = VINF_SUCCESS;
    }
    return vrc;
}


int VirtualBoxTranslator::i_setLanguageFile(TranslatorComponent *aComponent, const char *aFileName)
{
    AssertReturn(aComponent, VERR_INVALID_PARAMETER);

    int vrc = m_rcCache;
    if (m_hStrCache != NIL_RTSTRCACHE)
    {
        QMTranslator *pNewTranslator;
        try { pNewTranslator = new QMTranslator(); }
        catch (std::bad_alloc &) { pNewTranslator = NULL; }
        if (pNewTranslator)
        {
            vrc = pNewTranslator->load(aFileName, m_hStrCache);
            if (RT_SUCCESS(vrc))
            {
                if (aComponent->pTranslator)
                    delete aComponent->pTranslator;
                aComponent->pTranslator = pNewTranslator;
            }
            else
                delete pNewTranslator;
        }
        else
            vrc = VERR_NO_MEMORY;
    }
    else
        Assert(RT_FAILURE_NP(vrc));
    return vrc;
}


int VirtualBoxTranslator::registerTranslation(const char *aTranslationPath,
                                              bool aDefault,
                                              PTRCOMPONENT *aComponent)
{
    VirtualBoxTranslator *pCurrInstance = VirtualBoxTranslator::tryInstance();
    int vrc = VERR_GENERAL_FAILURE;
    if (pCurrInstance != NULL)
    {
        vrc = pCurrInstance->i_registerTranslation(aTranslationPath, aDefault, aComponent);
        pCurrInstance->release();
    }
    return vrc;
}


int VirtualBoxTranslator::i_registerTranslation(const char *aTranslationPath,
                                                bool aDefault,
                                                PTRCOMPONENT *aComponent)
{
    AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
    TranslatorComponent *pComponent;
    for (TranslatorList::iterator it = m_lTranslators.begin();
         it != m_lTranslators.end();
         ++it)
    {
        if (it->strPath == aTranslationPath)
        {
            pComponent = &(*it);
            if (aDefault)
                m_pDefaultComponent = pComponent;
            *aComponent = (PTRCOMPONENT)pComponent;
            return VINF_SUCCESS;
        }
    }

    try
    {
        m_lTranslators.push_back(TranslatorComponent());
        pComponent = &m_lTranslators.back();
    }
    catch(std::bad_alloc &)
    {
        return VERR_NO_MEMORY;
    }

    pComponent->strPath = aTranslationPath;
    if (aDefault)
        m_pDefaultComponent = pComponent;
    *aComponent = (PTRCOMPONENT)pComponent;
    /* ignore the error during loading because path
     * could contain no translation for current language */
    i_loadLanguageForComponent(pComponent, m_strLanguage.c_str());
    return VINF_SUCCESS;
}


int VirtualBoxTranslator::unregisterTranslation(PTRCOMPONENT aComponent)
{
    int vrc;
    if (aComponent != NULL)
    {
        VirtualBoxTranslator *pCurrInstance = VirtualBoxTranslator::tryInstance();
        if (pCurrInstance != NULL)
        {
            vrc = pCurrInstance->i_unregisterTranslation(aComponent);
            pCurrInstance->release();
        }
        else
            vrc = VERR_GENERAL_FAILURE;
    }
    else
        vrc = VWRN_NOT_FOUND;
    return vrc;
}


int VirtualBoxTranslator::i_unregisterTranslation(PTRCOMPONENT aComponent)
{
    AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);

    if (aComponent == m_pDefaultComponent)
        m_pDefaultComponent = NULL;

    for (TranslatorList::iterator it = m_lTranslators.begin();
         it != m_lTranslators.end();
         ++it)
    {
        if (&(*it) == aComponent)
        {
            delete aComponent->pTranslator;
            m_lTranslators.erase(it);
            return VINF_SUCCESS;
        }
    }

    return VERR_NOT_FOUND;
}


const char *VirtualBoxTranslator::translate(PTRCOMPONENT aComponent,
                                            const char  *aContext,
                                            const char  *aSourceText,
                                            const char  *aComment,
                                            const size_t aNum) RT_NOEXCEPT
{
    VirtualBoxTranslator *pCurrInstance = VirtualBoxTranslator::tryInstance();
    const char *pszTranslation = aSourceText;
    if (pCurrInstance != NULL)
    {
        pszTranslation = pCurrInstance->i_translate(aComponent, aContext, aSourceText, aComment, aNum);
        pCurrInstance->release();
    }
    return pszTranslation;
}


const char *VirtualBoxTranslator::i_translate(PTRCOMPONENT aComponent,
                                              const char  *aContext,
                                              const char  *aSourceText,
                                              const char  *aComment,
                                              const size_t aNum) RT_NOEXCEPT
{
    AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);

    if (aComponent == NULL)
        aComponent = m_pDefaultComponent;

    if (   aComponent == NULL
        || aComponent->pTranslator == NULL)
        return aSourceText;

    const char *pszSafeSource  = NULL;
    const char *pszTranslation = aComponent->pTranslator->translate(aContext, aSourceText, &pszSafeSource, aComment, aNum);
    if (pszSafeSource && g_idxTlsSrc != NIL_RTTLS && g_idxTlsTr != NIL_RTTLS)
    {
        RTTlsSet(g_idxTlsTr, (void *)pszTranslation);
        RTTlsSet(g_idxTlsSrc, (void *)pszSafeSource);
    }

    return pszTranslation;
}


const char *VirtualBoxTranslator::trSource(const char *aTranslation) RT_NOEXCEPT
{
    const char *pszSource = aTranslation;
    VirtualBoxTranslator *pCurInstance = VirtualBoxTranslator::tryInstance(); /* paranoia */
    if (pCurInstance != NULL)
    {
        if (g_idxTlsSrc != NIL_RTTLS && g_idxTlsTr != NIL_RTTLS)
        {
            const char * const pszTranslationTls = (const char *)RTTlsGet(g_idxTlsTr);
            const char * const pszSourceTls      = (const char *)RTTlsGet(g_idxTlsSrc);
            if (   pszSourceTls      != NULL
                && pszTranslationTls != NULL
                && (   pszTranslationTls == aTranslation
                    || strcmp(pszTranslationTls, aTranslation) == 0))
                pszSource = pszSourceTls;
        }
        pCurInstance->release();
    }
    return pszSource;
}


int32_t VirtualBoxTranslator::initCritSect()
{
    return RTCritSectRwInit(&s_instanceRwLock);
}
/* vi: set tabstop=4 shiftwidth=4 expandtab: */