VirtualBox

source: vbox/trunk/src/VBox/Main/include/VirtualBoxTranslator.h@ 92154

Last change on this file since 92154 was 92068, checked in by vboxsync, 3 years ago

Main: Removed unnecessary parentheses. bugref:1909

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.3 KB
Line 
1/* $Id: VirtualBoxTranslator.h 92068 2021-10-26 08:35:27Z 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
29COM_STRUCT_OR_CLASS(IVirtualBox);
30class QMTranslator;
31
32class VirtualBoxTranslator
33 : public util::RWLockHandle
34{
35public:
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
46private:
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 };
56public:
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
110private:
111 static RTCRITSECTRW s_instanceRwLock;
112 static VirtualBoxTranslator *s_pInstance;
113
114 uint32_t m_cInstanceRefs;
115
116 typedef std::list<TranslatorComponent> TranslatorList;
117 TranslatorList m_lTranslators;
118 TranslatorComponent *m_pDefaultComponent;
119
120 /* keep the language code for registration */
121 com::Utf8Str m_strLanguage;
122
123 /** String cache that all translation strings are stored in.
124 * This is a add-only cache, which allows translate() to return C-strings w/o
125 * needing to think about racing loadLanguage() wrt string lifetime. */
126 RTSTRCACHE m_hStrCache;
127 /** RTStrCacheCreate status code. */
128 int m_rcCache;
129
130 VirtualBoxTranslator();
131
132 int i_loadLanguageForComponent(TranslatorComponent *aComponent, const char *aLang);
133
134 int i_setLanguageFile(TranslatorComponent *aComponent, const char *aFileName);
135
136 int i_registerTranslation(const char *aTranslationPath,
137 bool aDefault,
138 PTRCOMPONENT *aComponent);
139
140 int i_unregisterTranslation(PTRCOMPONENT aComponent);
141
142 const char *i_translate(PTRCOMPONENT aComponent,
143 const char *aContext,
144 const char *aSourceText,
145 const char *aComment = NULL,
146 const size_t aNum = ~(size_t)0) RT_NOEXCEPT;
147};
148
149/** Pointer to a translator component. */
150typedef VirtualBoxTranslator::PTRCOMPONENT PTRCOMPONENT;
151
152#endif /* !MAIN_INCLUDED_VirtualBoxTranslator_h */
153/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use