hydrogen 1.2.6
Translations.h
Go to the documentation of this file.
1/*
2 * Hydrogen
3 * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net]
4 * Copyright(c) 2008-2025 The hydrogen development team [hydrogen-devel@lists.sourceforge.net]
5 *
6 * http://www.hydrogen-music.org
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY, without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see https://www.gnu.org/licenses
20 *
21 */
22
23#ifndef H2C_TRANSLATIONS_H
24#define H2C_TRANSLATIONS_H
25
26
27#include <core/Object.h>
29#include <QtCore/QString>
30#include <QTranslator>
31#include <QLocale>
32#include <QList>
33
34namespace H2Core
35{
36
40
41class Translations : public Object<Translations>
42{
43
44 // Need methods to:
45 // list available translations (return QLocales?)
46 // find appropriate translation given list of user prefs
47public:
48
49 static QStringList availableTranslations( QString sFileName,
50 QString sDirectory = H2Core::Filesystem::i18n_dir() ) {
51 QStringList translations;
52 QDir d( sDirectory );
53 QStringList filter = QStringList( sFileName + "_*.qm" );
54
55 for ( QString sEntry : d.entryList( filter ) ) {
56 // Extract language or language_REGION string
57 sEntry.remove( 0, sFileName.size() + 1 );
58 sEntry.remove( sEntry.size() - 3, 3);
59 sEntry.replace( '_', '-' );
60 translations << sEntry;
61 }
62 return translations;
63 }
64
79
80 static QString findTranslation( QStringList languages, QString sFileName, QString sDirectory = H2Core::Filesystem::i18n_dir() )
81 {
82 for ( QString sLanguage : languages ) {
83 sLanguage.replace( '-', '_' );
84
85 for (;;) {
86 QString sTransName = sFileName + "_" + sLanguage + ".qm";
87 QFileInfo fi( sDirectory, sTransName );
88 if ( fi.exists() && fi.isFile() ) {
89 sLanguage.replace( '_', '-' );
90 return sLanguage;
91 }
92 int i = sLanguage.lastIndexOf( '_' );
93 if ( i == -1 ) {
94 break;
95 }
96 sLanguage.truncate( i );
97 }
98 }
99 return QString();
100 }
101
102 static bool loadTranslation( QStringList languages, QTranslator &tor, QString sFileName, QString sDirectory = H2Core::Filesystem::i18n_dir() )
103 {
104 QString sLanguage = findTranslation( languages, sFileName, sDirectory );
105 if ( sLanguage.isNull() ) {
106 return false;
107 }
108 sLanguage.replace( '-', '_' );
109 QString sTransName = sFileName + "_" + sLanguage + ".qm";
110 return tor.load( sTransName, sDirectory );
111
112 }
113
114};
115
116
117}
118#endif
static QString i18n_dir()
returns internationalization path
Translations manager.
static QStringList availableTranslations(QString sFileName, QString sDirectory=H2Core::Filesystem::i18n_dir())
static QString findTranslation(QStringList languages, QString sFileName, QString sDirectory=H2Core::Filesystem::i18n_dir())
The standard QTranslation::load will prefer an exact match of a languae-REGION pair,...
static bool loadTranslation(QStringList languages, QTranslator &tor, QString sFileName, QString sDirectory=H2Core::Filesystem::i18n_dir())