Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 : #include "languagesconfiguration.h"
23 :
24 : #include <QStringList>
25 : #include <QUrl>
26 :
27 : #include "global/configreader.h"
28 : #include "settings.h"
29 : #include "languagestypes.h"
30 :
31 : #include "log.h"
32 :
33 : using namespace muse;
34 : using namespace muse::languages;
35 :
36 : static const Settings::Key LANGUAGE_KEY("languages", "language");
37 :
38 0 : void LanguagesConfiguration::init()
39 : {
40 0 : m_config = ConfigReader::read(":/configs/languages.cfg");
41 :
42 0 : settings()->setDefaultValue(LANGUAGE_KEY, Val(SYSTEM_LANGUAGE_CODE.toStdString()));
43 0 : settings()->valueChanged(LANGUAGE_KEY).onReceive(nullptr, [this](const Val& val) {
44 0 : m_currentLanguageCodeChanged.send(val.toQString());
45 0 : });
46 0 : }
47 :
48 0 : ValCh<QString> LanguagesConfiguration::currentLanguageCode() const
49 : {
50 0 : ValCh<QString> result;
51 0 : result.ch = m_currentLanguageCodeChanged;
52 0 : result.val = settings()->value(LANGUAGE_KEY).toQString();
53 :
54 0 : return result;
55 0 : }
56 :
57 0 : void LanguagesConfiguration::setCurrentLanguageCode(const QString& languageCode) const
58 : {
59 0 : Val value(languageCode.toStdString());
60 0 : settings()->setSharedValue(LANGUAGE_KEY, value);
61 0 : }
62 :
63 0 : QUrl LanguagesConfiguration::languagesUpdateUrl() const
64 : {
65 0 : return QUrl(m_config.value("server_url").toQString() + "details.json");
66 : }
67 :
68 0 : QUrl LanguagesConfiguration::languageFileServerUrl(const QString& languageCode) const
69 : {
70 0 : TRACEFUNC;
71 0 : return QUrl(m_config.value("server_url").toQString() + QString("locale_%1.zip").arg(languageCode));
72 0 : }
73 :
74 0 : io::path_t LanguagesConfiguration::languagesAppDataPath() const
75 : {
76 0 : return globalConfiguration()->appDataPath() + "/locale";
77 : }
78 :
79 0 : io::path_t LanguagesConfiguration::languagesUserAppDataPath() const
80 : {
81 0 : return globalConfiguration()->userAppDataPath() + "/locale";
82 : }
83 :
84 0 : io::path_t LanguagesConfiguration::appLanguagesJsonPath() const
85 : {
86 0 : return languagesAppDataPath() + "/languages.json";
87 : }
88 :
89 0 : io::path_t LanguagesConfiguration::builtinLanguagesJsonPath() const
90 : {
91 0 : return ":/languages/resources/languages.json";
92 : }
93 :
94 0 : io::path_t LanguagesConfiguration::builtinLanguageFilePath(const QString& resourceName, const QString& languageCode) const
95 : {
96 0 : return languagesAppDataPath() + QString("/%1_%2.qm").arg(resourceName, languageCode);
97 : }
98 :
99 0 : io::path_t LanguagesConfiguration::userLanguageFilePath(const QString& resourceName, const QString& languageCode) const
100 : {
101 0 : return languagesUserAppDataPath() + QString("/%1_%2.qm").arg(resourceName, languageCode);
102 : }
103 :
104 0 : QStringList LanguagesConfiguration::languageResourceNames() const
105 : {
106 0 : const Val& val = m_config.value("resource_names");
107 0 : QStringList result;
108 0 : for (const Val& item : val.toList()) {
109 0 : result.append(item.toQString());
110 0 : }
111 0 : return result;
112 0 : }
|