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