1 /************************************************************************
2 **
3 **  Copyright (C) 2019  Kevin B. Hendricks, Stratford, Ontario Canada
4 **  Copyright (C) 2011  John Schember <john@nachtimwald.com>
5 **
6 **  This file is part of PageEdit.
7 **
8 **  PageEdit 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 3 of the License, or
11 **  (at your option) any later version.
12 **
13 **  PageEdit 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 PageEdit.  If not, see <http://www.gnu.org/licenses/>.
20 **
21 *************************************************************************/
22 
23 #include <QtCore/QDir>
24 #include <QtCore/QCoreApplication>
25 #include <QtCore/QLibraryInfo>
26 #include <QtCore/QString>
27 #include "UILanguage.h"
28 #include "pageedit_constants.h"
29 
30 #if !defined(Q_OS_WIN32) && !defined(Q_OS_MAC)
31 #include <stdlib.h>
32 #endif
33 
34 static QString TRANSLATION_FILE_PREFIX = "pageedit_";
35 static QString TRANSLATION_FILE_SUFFIX = ".qm";
36 
GetPossibleTranslationPaths()37 QStringList UILanguage::GetPossibleTranslationPaths()
38 {
39     // There are a few different places translations can be stored depending
40     // on the platform and where they were installed.
41     QStringList possible_qm_locations;
42 #if !defined(Q_OS_WIN32) && !defined(Q_OS_MAC)
43     possible_qm_locations.append(pageedit_share_root + "/translations/");
44 #endif
45 
46     possible_qm_locations.append(QLibraryInfo::location(QLibraryInfo::TranslationsPath));
47 
48 #ifdef Q_OS_MAC
49     possible_qm_locations.append(QCoreApplication::applicationDirPath() + "/../translations");
50 #else
51     possible_qm_locations.append(QCoreApplication::applicationDirPath() + "/translations");
52 #endif
53 
54     return possible_qm_locations;
55 }
56 
GetUILanguages()57 QStringList UILanguage::GetUILanguages()
58 {
59     QStringList ui_languages;
60     QStringList checked_dirs;
61     foreach(QString path, GetPossibleTranslationPaths()) {
62         // Find all translation files and add them to the avaliable list.
63         QDir translationDir(path);
64 
65         if (translationDir.exists() && !checked_dirs.contains(translationDir.absolutePath())) {
66             QStringList filters;
67             // Look for all pageedit_*.qm files.
68             filters << TRANSLATION_FILE_PREFIX + "*" + TRANSLATION_FILE_SUFFIX;
69             translationDir.setNameFilters(filters);
70             QStringList translation_files = translationDir.entryList();
71             foreach(QString file, translation_files) {
72                 QFileInfo fileInfo(file);
73                 QString basename = fileInfo.baseName();
74                 QString language = basename.right(basename.length() - TRANSLATION_FILE_PREFIX.length());
75                 ui_languages.append(language);
76             }
77             checked_dirs.append(translationDir.absolutePath());
78         }
79     }
80     return ui_languages;
81 }
82