source: trunk/tools/linguist/shared/translator.h@ 239

Last change on this file since 239 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 8.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the Qt Linguist of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef METATRANSLATOR_H
43#define METATRANSLATOR_H
44
45#include "translatormessage.h"
46
47#include <QDir>
48#include <QList>
49#include <QLocale>
50#include <QString>
51
52
53QT_BEGIN_NAMESPACE
54
55class QIODevice;
56
57// A struct of "interesting" data passed to and from the load and save routines
58class ConversionData
59{
60public:
61 ConversionData() :
62 m_verbose(false),
63 m_ignoreUnfinished(false),
64 m_sortContexts(false),
65 m_noUiLines(false),
66 m_saveMode(SaveEverything)
67 {}
68
69 // tag manipulation
70 const QStringList &dropTags() const { return m_dropTags; }
71 QStringList &dropTags() { return m_dropTags; }
72 const QDir &targetDir() const { return m_targetDir; }
73 bool isVerbose() const { return m_verbose; }
74 bool ignoreUnfinished() const { return m_ignoreUnfinished; }
75 bool sortContexts() const { return m_sortContexts; }
76
77 void appendError(const QString &error) { m_errors.append(error); }
78 QString error() const { return m_errors.join(QLatin1String("\n")); }
79 QStringList errors() const { return m_errors; }
80 void clearErrors() { m_errors.clear(); }
81
82public:
83 QString m_defaultContext;
84 QByteArray m_codecForSource; // CPP specific
85 QString m_sourceFileName;
86 QString m_targetFileName;
87 QDir m_sourceDir;
88 QDir m_targetDir; // FIXME: TS spefic
89 QStringList m_dropTags; // tags to be dropped
90 QStringList m_errors;
91 bool m_verbose;
92 bool m_ignoreUnfinished;
93 bool m_sortContexts;
94 bool m_noUiLines;
95 TranslatorSaveMode m_saveMode;
96};
97
98class Translator
99{
100public:
101 Translator();
102
103 bool load(const QString &filename, ConversionData &err, const QString &format /*= "auto"*/);
104 bool save(const QString &filename, ConversionData &err, const QString &format /*= "auto"*/) const;
105 bool release(QFile *iod, ConversionData &cd) const;
106
107 bool contains(const QString &context, const QString &sourceText,
108 const QString &comment) const;
109 TranslatorMessage find(const QString &context,
110 const QString &sourceText, const QString &comment) const;
111
112 TranslatorMessage find(const QString &context,
113 const QString &comment, const TranslatorMessage::References &refs) const;
114
115 bool contains(const QString &context) const;
116 TranslatorMessage find(const QString &context) const;
117
118 void replace(const TranslatorMessage &msg);
119 void replaceSorted(const TranslatorMessage &msg);
120 void extend(const TranslatorMessage &msg); // Only for single-location messages
121 void append(const TranslatorMessage &msg);
122 void appendSorted(const TranslatorMessage &msg);
123
124 void stripObsoleteMessages();
125 void stripFinishedMessages();
126 void stripEmptyContexts();
127 void stripNonPluralForms();
128 void stripIdenticalSourceTranslations();
129 void dropTranslations();
130 void makeFileNamesAbsolute(const QDir &originalPath);
131 QSet<TranslatorMessagePtr> resolveDuplicates();
132 static void reportDuplicates(const QSet<TranslatorMessagePtr> &dupes,
133 const QString &fileName, bool verbose);
134
135 void setCodecName(const QByteArray &name);
136 QByteArray codecName() const { return m_codecName; }
137
138 QString languageCode() const { return m_language; }
139 QString sourceLanguageCode() const { return m_sourceLanguage; }
140
141 enum LocationsType { NoLocations, RelativeLocations, AbsoluteLocations };
142 void setLocationsType(LocationsType lt) { m_locationsType = lt; }
143 LocationsType locationsType() const { return m_locationsType; }
144
145 static QString makeLanguageCode(QLocale::Language language, QLocale::Country country);
146 static void languageAndCountry(const QString &languageCode,
147 QLocale::Language *lang, QLocale::Country *country);
148 void setLanguageCode(const QString &languageCode) { m_language = languageCode; }
149 void setSourceLanguageCode(const QString &languageCode) { m_sourceLanguage = languageCode; }
150 static QString guessLanguageCodeFromFileName(const QString &fileName);
151 QList<TranslatorMessage> messages() const;
152 QList<TranslatorMessage> translatedMessages() const;
153 static QStringList normalizedTranslations(const TranslatorMessage &m,
154 QLocale::Language lang, QLocale::Country country);
155 QStringList normalizedTranslations(const TranslatorMessage &m, ConversionData &cd, bool *ok) const;
156
157 int messageCount() const { return m_messages.size(); }
158 TranslatorMessage &message(int i) { return m_messages[i]; }
159 const TranslatorMessage &message(int i) const { return m_messages.at(i); }
160 void dump() const;
161
162 // additional file format specific data
163 // note: use '<fileformat>:' as prefix for file format specific members,
164 // e.g. "po-flags", "po-msgid_plural"
165 typedef TranslatorMessage::ExtraData ExtraData;
166 QString extra(const QString &ba) const;
167 void setExtra(const QString &ba, const QString &var);
168 bool hasExtra(const QString &ba) const;
169 const ExtraData &extras() const { return m_extra; }
170 void setExtras(const ExtraData &extras) { m_extra = extras; }
171
172 // registration of file formats
173 typedef bool (*SaveFunction)(const Translator &, QIODevice &out, ConversionData &data);
174 typedef bool (*LoadFunction)(Translator &, QIODevice &in, ConversionData &data);
175 struct FileFormat {
176 FileFormat() : loader(0), saver(0), priority(-1) {}
177 QString extension; // such as "ts", "xlf", ...
178 QString description; // human-readable description
179 LoadFunction loader;
180 SaveFunction saver;
181 enum FileType { SourceCode, TranslationSource, TranslationBinary } fileType;
182 int priority; // 0 = highest, -1 = invisible
183 };
184 static void registerFileFormat(const FileFormat &format);
185 static QList<FileFormat> &registeredFileFormats();
186
187 enum VariantSeparators {
188 DefaultVariantSeparator = 0x2762, // some weird character nobody ever heard of :-D
189 InternalVariantSeparator = 0x9c // unicode "STRING TERMINATOR"
190 };
191
192private:
193 typedef QList<TranslatorMessage> TMM; // int stores the sequence position.
194
195 TMM m_messages;
196 QByteArray m_codecName;
197 LocationsType m_locationsType;
198
199 // A string beginning with a 2 or 3 letter language code (ISO 639-1
200 // or ISO-639-2), followed by the optional country variant to distinguish
201 // between country-specific variations of the language. The language code
202 // and country code are always separated by '_'
203 // Note that the language part can also be a 3-letter ISO 639-2 code.
204 // Legal examples:
205 // 'pt' portuguese, assumes portuguese from portugal
206 // 'pt_BR' Brazilian portuguese (ISO 639-1 language code)
207 // 'por_BR' Brazilian portuguese (ISO 639-2 language code)
208 QString m_language;
209 QString m_sourceLanguage;
210 ExtraData m_extra;
211};
212
213bool getNumerusInfo(QLocale::Language language, QLocale::Country country,
214 QByteArray *rules, QStringList *forms);
215
216/*
217 This is a quick hack. The proper way to handle this would be
218 to extend Translator's interface.
219*/
220#define ContextComment "QT_LINGUIST_INTERNAL_CONTEXT_COMMENT"
221
222QT_END_NAMESPACE
223
224#endif
Note: See TracBrowser for help on using the repository browser.