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