File indexing completed on 2022-11-29 12:54:59

0001 /*
0002     SPDX-FileCopyrightText: 2003-2006 Cies Breijs <cies AT kde DOT nl>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 
0008 #ifndef _TRANSLATOR_H_
0009 #define _TRANSLATOR_H_
0010 
0011 #include <QChar>
0012 #include <QHash>
0013 #include <QString>
0014 #include <QStringList>
0015 
0016 
0017 static const char* DEFAULT_LANGUAGE_CODE = "en_US";
0018 
0019 /**
0020  * @short Uses an XML dictionary to translate unicode strings to Token types (if possible).
0021  *
0022  * This class can read XML dictionaries containing maps of international
0023  * strings and standard strings. Using the generated code in
0024  * @ref stringType2intType() the standard strings can be mapped to Token
0025  * types.
0026  *
0027  * When a dictionary is loaded by @ref loadDictionary() a private member map
0028  * called look2typeMap is created and filled where international strings
0029  * are mapped directly to the Token types.
0030  *
0031  * A some of the code of this class is generated code.
0032  *
0033  * @author Cies Breijs
0034  */
0035 class Translator
0036 {
0037     public:
0038         static Translator* instance();
0039 
0040         /** @short   Sets the dictionary language.
0041             By setting the dictionary KTurtle will use a different translation of TurtleScript.
0042             Examples are managed by the Translator aswell. Easy.
0043             TODO: is this redundant no KDE has a 'switch application langugae' in the Help(???) menu?
0044             @param   lang_code the ISO language code of the dictionary (eg: "en_US", "fr", "pt_BR", "nl")
0045             @returns TRUE is the loading was successful, otherwise FALSE */
0046         bool setLanguage(const QString &lang_code = QLatin1String(DEFAULT_LANGUAGE_CODE));
0047 
0048         /** @short Converts a unicode string to a token type.
0049             Uses the dictionary to do so.
0050             If the string could not translated to a Token type, Token::Unknown is returned.
0051             @param   look the unicode string a bit of KTurtle code
0052             @returns the token type, Token::Unknown if not recognised */
0053         int look2type(QString& look);
0054 
0055         /** @short Converts a unicode character to a token type.
0056             Overloaded for convenience, behaves like the method it overloads.
0057             @param   look one unicode character of KTurtle code
0058             @returns the token type, Token::Unknown if not recognised */
0059         int look2type(QChar& look);
0060 
0061         /** @short Converts a token type into a list of commands associated with it.
0062             This method is slow compared to the inverse, look2type(), methods
0063             because the internal representation of data is not optimized for this.
0064             This methods is used by the highlighter class.
0065             @param   type the token type as specified in the Token class
0066             @returns a QList of QString objects containing all the looks of the
0067                      command in the current translation. */
0068         QList<QString> type2look(int type);
0069 
0070         QHash<int, QList<QString> > token2stringsMap();
0071 
0072         QString default2localized(const QString& defaultLook) { return default2localizedMap[defaultLook]; }
0073 
0074         /// returns all default looks that have a localized look (for translating examples in main.cpp)
0075         QStringList allDefaultLooks() { return QStringList(default2localizedMap.keys()); }
0076         
0077         QStringList allLocalizedLooks() { return QStringList(default2localizedMap.values()); }
0078 
0079         /// used by the MainWindow's context help logic, and main.cpp
0080         QString defaultLook(const QString& localizedEntry) { return default2localizedMap.key(localizedEntry); }
0081 
0082         QStringList exampleNames() const { return QStringList(examples.keys()); }
0083 
0084         QString example(const QString& name) const { return examples[name]; }
0085 
0086         QString localizeScript(const QString& untranslatedScript);
0087 
0088 
0089     protected:
0090         /** @short Constructor. Does nothing special. */
0091         Translator();
0092 
0093         /** @short Destructor. Does nothing special. */
0094         virtual ~Translator();
0095 
0096         Translator(const Translator&);
0097         Translator& operator= (const Translator&);
0098 
0099 
0100     private:
0101         static Translator* m_instance;
0102 
0103         void setDictionary();
0104         void setExamples();
0105 
0106         QHash<QString, QString> examples;
0107 
0108         QHash<QString, int> look2typeMap;
0109         QHash<QString, QString> default2localizedMap;
0110 
0111         QStringList localizer;
0112 };
0113 
0114 
0115 #endif  // _TRANSLATOR_H_