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