1 | /* $Id: VirtualBoxTranslator.h 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Translator.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2005-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #ifndef MAIN_INCLUDED_VirtualBoxTranslator_h
|
---|
29 | #define MAIN_INCLUDED_VirtualBoxTranslator_h
|
---|
30 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
31 | # pragma once
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #include <list>
|
---|
35 |
|
---|
36 | #include <iprt/cpp/lock.h>
|
---|
37 | #include <VBox/com/AutoLock.h>
|
---|
38 |
|
---|
39 | COM_STRUCT_OR_CLASS(IVirtualBox);
|
---|
40 | class QMTranslator;
|
---|
41 |
|
---|
42 | class VirtualBoxTranslator
|
---|
43 | : public util::RWLockHandle
|
---|
44 | {
|
---|
45 | public:
|
---|
46 | virtual ~VirtualBoxTranslator();
|
---|
47 |
|
---|
48 | static VirtualBoxTranslator *instance();
|
---|
49 | /* Returns instance if exists, returns NULL otherwise. */
|
---|
50 | static VirtualBoxTranslator *tryInstance() RT_NOEXCEPT;
|
---|
51 | void release();
|
---|
52 |
|
---|
53 | /* Load language based on settings in the VirtualBox config */
|
---|
54 | HRESULT loadLanguage(ComPtr<IVirtualBox> aVirtualBox);
|
---|
55 |
|
---|
56 | private:
|
---|
57 | /** Translator component. */
|
---|
58 | struct TranslatorComponent
|
---|
59 | {
|
---|
60 | QMTranslator *pTranslator;
|
---|
61 | /** Path to translation files. It includes file prefix, i.e '/path/to/folder/file_prefix'. */
|
---|
62 | com::Utf8Str strPath;
|
---|
63 |
|
---|
64 | TranslatorComponent() : pTranslator(NULL) {}
|
---|
65 | };
|
---|
66 | public:
|
---|
67 | /** Pointer to a translator component. */
|
---|
68 | typedef TranslatorComponent *PTRCOMPONENT;
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Registers the translation for a component.
|
---|
72 | *
|
---|
73 | * @returns VBox status code
|
---|
74 | * @param aTranslationPath Path to the translation files, this includes the
|
---|
75 | * base filename prefix.
|
---|
76 | * @param aDefault Use this as the default translation component, i.e.
|
---|
77 | * when translate() is called with NULL for @a
|
---|
78 | * aComponent.
|
---|
79 | * @param aComponent Pointer to where is the component handle should be
|
---|
80 | * returned on success. The return value must be used
|
---|
81 | * for all subsequent calls to translate().
|
---|
82 | */
|
---|
83 | static int registerTranslation(const char *aTranslationPath,
|
---|
84 | bool aDefault,
|
---|
85 | PTRCOMPONENT *aComponent);
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Removes translations for a component.
|
---|
89 | *
|
---|
90 | * @returns VBox status code
|
---|
91 | * @param aComponent The component. NULL is quietly (VWRN_NOT_FOUND)
|
---|
92 | * ignored .
|
---|
93 | */
|
---|
94 | static int unregisterTranslation(PTRCOMPONENT aComponent);
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Translates @a aSourceText to user language.
|
---|
98 | * Uses component marked as default if @a aTranslationComponent is NULL
|
---|
99 | *
|
---|
100 | * @returns Translated string or @a aSourceText. The returned string is
|
---|
101 | * valid only during lifetime of the translator instance.
|
---|
102 | */
|
---|
103 | static const char *translate(PTRCOMPONENT aComponent,
|
---|
104 | const char *aContext,
|
---|
105 | const char *aSourceText,
|
---|
106 | const char *aComment = NULL,
|
---|
107 | const size_t aNum = ~(size_t)0) RT_NOEXCEPT;
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Returns source text stored in the cache if exists.
|
---|
111 | * Otherwise, the pszTranslation itself returned.
|
---|
112 | */
|
---|
113 | static const char *trSource(const char *aTranslation) RT_NOEXCEPT;
|
---|
114 |
|
---|
115 | /* Convenience function used by VirtualBox::init */
|
---|
116 | int i_loadLanguage(const char *pszLang);
|
---|
117 |
|
---|
118 | static int32_t initCritSect();
|
---|
119 |
|
---|
120 | com::Utf8Str language();
|
---|
121 |
|
---|
122 | private:
|
---|
123 | static RTCRITSECTRW s_instanceRwLock;
|
---|
124 | static VirtualBoxTranslator *s_pInstance;
|
---|
125 |
|
---|
126 | uint32_t m_cInstanceRefs;
|
---|
127 |
|
---|
128 | typedef std::list<TranslatorComponent> TranslatorList;
|
---|
129 | TranslatorList m_lTranslators;
|
---|
130 | TranslatorComponent *m_pDefaultComponent;
|
---|
131 |
|
---|
132 | /* keep the language code for registration */
|
---|
133 | com::Utf8Str m_strLanguage;
|
---|
134 |
|
---|
135 | /** String cache that all translation strings are stored in.
|
---|
136 | * This is a add-only cache, which allows translate() to return C-strings w/o
|
---|
137 | * needing to think about racing loadLanguage() wrt string lifetime. */
|
---|
138 | RTSTRCACHE m_hStrCache;
|
---|
139 | /** RTStrCacheCreate status code. */
|
---|
140 | int m_rcCache;
|
---|
141 |
|
---|
142 | VirtualBoxTranslator();
|
---|
143 |
|
---|
144 | int i_loadLanguageForComponent(TranslatorComponent *aComponent, const char *aLang);
|
---|
145 |
|
---|
146 | int i_setLanguageFile(TranslatorComponent *aComponent, const char *aFileName);
|
---|
147 |
|
---|
148 | int i_registerTranslation(const char *aTranslationPath,
|
---|
149 | bool aDefault,
|
---|
150 | PTRCOMPONENT *aComponent);
|
---|
151 |
|
---|
152 | int i_unregisterTranslation(PTRCOMPONENT aComponent);
|
---|
153 |
|
---|
154 | const char *i_translate(PTRCOMPONENT aComponent,
|
---|
155 | const char *aContext,
|
---|
156 | const char *aSourceText,
|
---|
157 | const char *aComment = NULL,
|
---|
158 | const size_t aNum = ~(size_t)0) RT_NOEXCEPT;
|
---|
159 | };
|
---|
160 |
|
---|
161 | /** Pointer to a translator component. */
|
---|
162 | typedef VirtualBoxTranslator::PTRCOMPONENT PTRCOMPONENT;
|
---|
163 |
|
---|
164 | #endif /* !MAIN_INCLUDED_VirtualBoxTranslator_h */
|
---|
165 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|