Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svtools / source / misc / langhelp.cxx
blob793e3f7d0e10b06f6c844be1c513d2f42d660fbf
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <sal/config.h>
12 #include <string_view>
14 #include <comphelper/sequence.hxx>
15 #include <i18nlangtag/languagetag.hxx>
16 #include <i18nlangtag/mslangid.hxx>
17 #include <officecfg/Office/Common.hxx>
18 #include <officecfg/System.hxx>
19 #include <org/freedesktop/PackageKit/SyncDbusSessionHelper.hpp>
20 #include <rtl/ustring.hxx>
21 #include <svtools/langhelp.hxx>
22 #include <comphelper/diagnose_ex.hxx>
23 #include <vcl/idle.hxx>
24 #include <vcl/svapp.hxx>
25 #include <vcl/settings.hxx>
26 #include <config_langs.h>
27 #include <config_vendor.h>
29 void localizeWebserviceURI( OUString& rURI )
31 OUString aLang = Application::GetSettings().GetUILanguageTag().getLanguage();
32 if ( aLang.equalsIgnoreAsciiCase("pt")
33 && Application::GetSettings().GetUILanguageTag().getCountry().equalsIgnoreAsciiCase("br") )
35 aLang = "pt-br";
37 if ( aLang.equalsIgnoreAsciiCase("zh") )
39 if ( Application::GetSettings().GetUILanguageTag().getCountry().equalsIgnoreAsciiCase("cn") )
40 aLang = "zh-cn";
41 if ( Application::GetSettings().GetUILanguageTag().getCountry().equalsIgnoreAsciiCase("tw") )
42 aLang = "zh-tw";
45 rURI += aLang;
48 OUString getInstalledLocaleForLanguage(css::uno::Sequence<OUString> const & installed, OUString const & locale)
50 if (locale.isEmpty())
51 return OUString(); // do not attempt to resolve anything
53 if (comphelper::findValue(installed, locale) != -1)
54 return locale;
56 std::vector<OUString> fallbacks(LanguageTag(locale).getFallbackStrings(false));
57 auto pf = std::find_if(fallbacks.begin(), fallbacks.end(),
58 [&installed](const OUString& rf) { return comphelper::findValue(installed, rf) != -1; });
59 if (pf != fallbacks.end())
60 return *pf;
61 return OUString();
64 static std::unique_ptr<Idle> xLangpackInstaller;
66 namespace {
68 class InstallLangpack : public Idle
70 std::vector<OUString> m_aPackages;
71 public:
72 explicit InstallLangpack(std::vector<OUString>&& rPackages)
73 : Idle("install langpack")
74 , m_aPackages(std::move(rPackages))
76 SetPriority(TaskPriority::LOWEST);
79 virtual void Invoke() override
81 vcl::Window* pTopWindow = Application::GetActiveTopWindow();
82 if (!pTopWindow)
83 pTopWindow = Application::GetFirstTopLevelWindow();
84 if (!pTopWindow)
86 Start();
87 return;
89 try
91 using namespace org::freedesktop::PackageKit;
92 css::uno::Reference<XSyncDbusSessionHelper> xSyncDbusSessionHelper(SyncDbusSessionHelper::create(comphelper::getProcessComponentContext()));
93 xSyncDbusSessionHelper->InstallPackageNames(comphelper::containerToSequence(m_aPackages), OUString());
95 catch (const css::uno::Exception&)
97 TOOLS_INFO_EXCEPTION("svl", "trying to install a LibreOffice langpack");
99 xLangpackInstaller.reset();
105 OUString getInstalledLocaleForSystemUILanguage(const css::uno::Sequence<OUString>& rLocaleElementNames, bool bRequestInstallIfMissing, const OUString& rPreferredLocale)
107 OUString wantedLocale(rPreferredLocale);
108 if (wantedLocale.isEmpty())
109 wantedLocale = officecfg::System::L10N::UILocale::get();
111 OUString locale = getInstalledLocaleForLanguage(rLocaleElementNames, wantedLocale);
112 if (bRequestInstallIfMissing && locale.isEmpty() && !wantedLocale.isEmpty() && !Application::IsHeadlessModeEnabled() &&
113 officecfg::Office::Common::PackageKit::EnableLangpackInstallation::get())
115 LanguageTag aWantedTag(wantedLocale);
116 if (aWantedTag.getLanguage() != "en")
118 // Get the list of langpacks that this build was configured to include
119 std::vector<OUString> aPackages;
120 OUString const sAvailableLocales(WITH_LANG);
121 std::vector<OUString> aAvailable;
122 sal_Int32 nIndex = 0;
125 aAvailable.emplace_back(sAvailableLocales.getToken(0, ' ', nIndex));
127 while (nIndex >= 0);
128 // See which one matches the desired ui locale
129 OUString install = getInstalledLocaleForLanguage(comphelper::containerToSequence(aAvailable), wantedLocale);
130 if (!install.isEmpty() && install != "en-US")
132 std::string_view sVendor(OOO_VENDOR);
133 if (sVendor == "Red Hat, Inc." || sVendor == "The Fedora Project")
135 // langpack is the typical Fedora/RHEL naming convention
136 LanguageType eType = aWantedTag.getLanguageType();
137 if (MsLangId::isSimplifiedChinese(eType))
138 aPackages.emplace_back("libreoffice-langpack-zh-Hans");
139 else if (MsLangId::isTraditionalChinese(eType))
140 aPackages.emplace_back("libreoffice-langpack-zh-Hant");
141 else if (install == "pt")
142 aPackages.emplace_back("libreoffice-langpack-pt-PT");
143 else
144 aPackages.emplace_back("libreoffice-langpack-" + install);
146 else if (sVendor == "The Document Foundation/Debian" || sVendor == "The Document Foundation, Debian and Ubuntu")
148 // l10n is the typical Debian/Ubuntu naming convention
149 aPackages.emplace_back("libreoffice-l10n-" + install);
152 if (!aPackages.empty())
154 xLangpackInstaller.reset(new InstallLangpack(std::move(aPackages)));
155 xLangpackInstaller->Start();
159 if (locale.isEmpty())
160 locale = getInstalledLocaleForLanguage(rLocaleElementNames, "en-US");
161 if (locale.isEmpty() && rLocaleElementNames.hasElements())
162 locale = rLocaleElementNames[0];
163 return locale;
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */