1 | /* $Id: VirtualBoxTranslator.h 91369 2021-09-24 16:47:04Z 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 |
|
---|
28 | #include <VBox/com/AutoLock.h>
|
---|
29 | #include <VBox/com/defs.h>
|
---|
30 | #include <VBox/com/Guid.h>
|
---|
31 | #include <VBox/com/ptr.h>
|
---|
32 | #include <VBox/com/string.h>
|
---|
33 |
|
---|
34 | #ifdef VBOX_WITH_MAIN_NLS
|
---|
35 | #define DECLARE_TRANSLATION_CONTEXT(ctx) \
|
---|
36 | struct ctx \
|
---|
37 | {\
|
---|
38 | static const char *tr(const char *pszSource, const char *pszComment = NULL, const int iNum = -1) \
|
---|
39 | { \
|
---|
40 | return VirtualBoxTranslator::translate(NULL, #ctx, pszSource, pszComment, iNum); \
|
---|
41 | } \
|
---|
42 | };
|
---|
43 | #else
|
---|
44 | #define DECLARE_TRANSLATION_CONTEXT(ctx) \
|
---|
45 | struct ctx \
|
---|
46 | {\
|
---|
47 | static const char *tr(const char *pszSource, const char *pszComment = NULL, const int iNum = -1) \
|
---|
48 | { \
|
---|
49 | NOREF(pszComment); \
|
---|
50 | NOREF(iNum); \
|
---|
51 | return pszSource; \
|
---|
52 | } \
|
---|
53 | };
|
---|
54 | #endif
|
---|
55 |
|
---|
56 | typedef void *TRCOMPONENT;
|
---|
57 |
|
---|
58 | #ifdef VBOX_WITH_MAIN_NLS
|
---|
59 |
|
---|
60 | struct IVirtualBox;
|
---|
61 | class QMTranslator;
|
---|
62 |
|
---|
63 | class VirtualBoxTranslator
|
---|
64 | : public util::RWLockHandle
|
---|
65 | {
|
---|
66 | public:
|
---|
67 |
|
---|
68 | virtual ~VirtualBoxTranslator();
|
---|
69 |
|
---|
70 | static VirtualBoxTranslator *instance();
|
---|
71 | /* Returns instance if exists, returns NULL otherwise. */
|
---|
72 | static VirtualBoxTranslator *tryInstance();
|
---|
73 | void release();
|
---|
74 |
|
---|
75 | /* Load language based on settings in the VirtualBox config */
|
---|
76 | HRESULT loadLanguage(ComPtr<IVirtualBox> aVirtualBox);
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Adds path containing translation files into list of known paths.
|
---|
80 | * Path should include translation file prefix.
|
---|
81 | *
|
---|
82 | * @returns VBox status code
|
---|
83 | * @param aTranslationPath Path to translation files including file prefix
|
---|
84 | * @param aDefault Use as default translation component, i.e.
|
---|
85 | * Use this path for translation if component
|
---|
86 | * is NULL
|
---|
87 | * @param aComponent Where is the pointer to component returned
|
---|
88 | */
|
---|
89 | static int registerTranslation(const char *aTranslationPath,
|
---|
90 | bool aDefault,
|
---|
91 | TRCOMPONENT *aComponent);
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Removes the path from list of known paths.
|
---|
95 | * Does not remove already loaded translation from string cache.
|
---|
96 | */
|
---|
97 | static int unregisterTranslation(TRCOMPONENT aComponent);
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Translates @a aSourceText to user language.
|
---|
101 | * Uses component marked as default if @a aTranslationComponent is NULL
|
---|
102 | *
|
---|
103 | * @returns Translated string or @a aSourceText. The returned string is
|
---|
104 | * valid only during lifetime of the translator instance.
|
---|
105 | */
|
---|
106 | static const char *translate(TRCOMPONENT aComponent,
|
---|
107 | const char *aContext,
|
---|
108 | const char *aSourceText,
|
---|
109 | const char *aComment = NULL,
|
---|
110 | const int aNum = -1);
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Returns source text stored in the cache if exists.
|
---|
114 | * Otherwise, the pszTranslation itself returned.
|
---|
115 | */
|
---|
116 | static const char *trSource(const char *aTranslation);
|
---|
117 |
|
---|
118 | /* Convenience function used by VirtualBox::init */
|
---|
119 | int i_loadLanguage(const char *pszLang);
|
---|
120 |
|
---|
121 | static int32_t initCritSect();
|
---|
122 |
|
---|
123 | private:
|
---|
124 | static RTCRITSECTRW s_instanceRwLock;
|
---|
125 | static VirtualBoxTranslator *s_pInstance;
|
---|
126 |
|
---|
127 | uint32_t m_cInstanceRefs;
|
---|
128 |
|
---|
129 | struct TranslatorComponent
|
---|
130 | {
|
---|
131 | QMTranslator *pTranslator;
|
---|
132 | /* Path to translation files. It includes file prefix, i.e
|
---|
133 | * /path/to/folder/file_prefix */
|
---|
134 | com::Utf8Str strPath;
|
---|
135 |
|
---|
136 | TranslatorComponent() : pTranslator(NULL) {}
|
---|
137 | };
|
---|
138 | typedef std::list<TranslatorComponent> TranslatorList;
|
---|
139 | TranslatorList m_lTranslators;
|
---|
140 | TranslatorComponent *m_pDefaultComponent;
|
---|
141 |
|
---|
142 | /* keep the language code for registration */
|
---|
143 | com::Utf8Str m_strLanguage;
|
---|
144 |
|
---|
145 | /** String cache that all translation strings are stored in.
|
---|
146 | * This is a add-only cache, which allows translate() to return C-strings w/o
|
---|
147 | * needing to think about racing loadLanguage() wrt string lifetime. */
|
---|
148 | RTSTRCACHE m_hStrCache;
|
---|
149 | /** RTStrCacheCreate status code. */
|
---|
150 | int m_rcCache;
|
---|
151 |
|
---|
152 | VirtualBoxTranslator();
|
---|
153 |
|
---|
154 | int i_loadLanguageForComponent(TranslatorComponent *aComponent, const char *aLang);
|
---|
155 |
|
---|
156 | int i_setLanguageFile(TranslatorComponent *aComponent, const char *aFileName);
|
---|
157 |
|
---|
158 | int i_registerTranslation(const char *aTranslationPath,
|
---|
159 | bool aDefault,
|
---|
160 | TRCOMPONENT *aComponent);
|
---|
161 |
|
---|
162 | int i_unregisterTranslation(TRCOMPONENT aComponent);
|
---|
163 |
|
---|
164 | const char *i_translate(TRCOMPONENT aComponent,
|
---|
165 | const char *aContext,
|
---|
166 | const char *aSourceText,
|
---|
167 | const char *aComment = NULL,
|
---|
168 | const int aNum = -1);
|
---|
169 | };
|
---|
170 |
|
---|
171 | #endif /* !VBOX_WITH_MAIN_NLS */
|
---|
172 |
|
---|
173 | #endif /* !MAIN_INCLUDED_VirtualBoxTranslator_h */
|
---|
174 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|