This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
translang.cc
166 lines (146 loc) · 4.32 KB
/
translang.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* @file translate.cc
* @brief Definitions for translate class.
* @author Nicu Tofan <nicu.tofan@gmail.com>
* @copyright Copyright 2014 piles contributors. All rights reserved.
* This file is released under the
* [MIT License](http://opensource.org/licenses/mit-license.html)
*/
#include "translang.h"
#include "translate.h"
#include "translate-private.h"
#include <QDir>
/**
* @class TransLang
*
*/
/* ------------------------------------------------------------------------- */
/**
* This constructor is only useful for creating the invalid instance used by
* the manager.
*/
TransLang::TransLang() :
path_("/\\invalid instance;:"),
langfile_("/\\invalid instance;:"),
name_("/\\invalid instance;:"),
icon_("/\\invalid instance;:"),
locale_(),
metadata_(),
transl_(NULL),
qttransl_(NULL)
{
TRANSLATE_TRACE_ENTRY;
TRANSLATE_TRACE_EXIT;
}
/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/**
* ...
*/
TransLang::TransLang (
const QString & dir, const QString & langfile,
const QString & name, const QString & icon, const QString & locale) :
path_(dir),
langfile_(langfile),
name_(name),
icon_(icon),
locale_(locale),
metadata_(),
transl_(NULL),
qttransl_(NULL)
{
TRANSLATE_TRACE_ENTRY;
TRANSLATE_TRACE_EXIT;
}
/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/**
* ...
*/
TransLang::~TransLang()
{
TRANSLATE_TRACE_ENTRY;
TRANSLATE_TRACE_EXIT;
}
/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/**
* ...
*/
void TransLang::addMetadata (
const QString & key, const QString & value)
{
metadata_.insert (key, value);
}
/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/**
* ...
*/
bool TransLang::isValid () const
{
return Translate::validInstance (*this);
}
/* ========================================================================= */
/* ------------------------------------------------------------------------- */
QString TransLang::qtLangFile() const
{
if (locale_.isEmpty())
return QString();
QDir dlang (path_);
QString s_qt_name = QString("qt_%1.qm").arg(locale_);
if (dlang.exists (s_qt_name)) {
return dlang.absoluteFilePath (s_qt_name);
} else {
dlang.setPath (QString::fromUtf8 (getenv ("TRANSLATE_QT_DIR")));
if (dlang.exists (s_qt_name)) {
return dlang.absoluteFilePath (s_qt_name);
} else {
return QString();
}
}
}
/* ========================================================================= */
/* ------------------------------------------------------------------------- */
QTranslator * TransLang::translator()
{
if (transl_ != NULL)
return transl_;
transl_ = new QTranslator ();
if (!transl_->load (langfile_)) {
TRANSLATE_DEBUGM("Failed to load translator from file %s\n",
TMP_A(langfile_));
delete transl_;
transl_ = NULL;
}
return transl_;
}
/* ========================================================================= */
/* ------------------------------------------------------------------------- */
QTranslator * TransLang::qtTranslator()
{
QString s_file = qtLangFile ();
qttransl_ = new QTranslator ();
if (!qttransl_->load (s_file)) {
TRANSLATE_DEBUGM("Failed to load translator from file %s\n",
TMP_A(s_file));
delete qttransl_;
qttransl_ = NULL;
}
return qttransl_;
}
/* ========================================================================= */
/* ------------------------------------------------------------------------- */
void TransLang::translatorDone ()
{
if (transl_ != NULL) {
delete transl_;
transl_ = NULL;
}
if (qttransl_ != NULL) {
delete qttransl_;
qttransl_ = NULL;
}
}
/* ========================================================================= */
void TransLang::anchorVtable() const {}