Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / desktop / source / app / langselect.cxx
blob5eb2f0636bcd347f65e1b5c069d0c3c62dfbe961
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <com/sun/star/configuration/theDefaultProvider.hpp>
23 #include <com/sun/star/container/XNameAccess.hpp>
24 #include <com/sun/star/lang/XLocalizable.hpp>
25 #include <com/sun/star/uno/Exception.hpp>
26 #include <com/sun/star/uno/Reference.hxx>
27 #include <com/sun/star/uno/Sequence.hxx>
28 #include <comphelper/configuration.hxx>
29 #include <comphelper/processfactory.hxx>
30 #include <i18nlangtag/lang.h>
31 #include <i18nlangtag/languagetag.hxx>
32 #include <i18nlangtag/mslangid.hxx>
33 #include <officecfg/Office/Linguistic.hxx>
34 #include <officecfg/Setup.hxx>
35 #include <officecfg/System.hxx>
36 #include <rtl/ustring.hxx>
37 #include <svl/languageoptions.hxx>
38 #include <svtools/langhelp.hxx>
39 #include <comphelper/diagnose_ex.hxx>
41 #include <app.hxx>
43 #include "cmdlineargs.hxx"
44 #include "langselect.hxx"
46 namespace desktop::langselect {
48 namespace {
50 void setMsLangIdFallback(OUString const & locale) {
51 // #i32939# setting of default document language
52 // See #i42730# for rules for determining source of settings
53 if (locale.isEmpty())
54 return;
56 LanguageType type = LanguageTag::convertToLanguageTypeWithFallback(locale);
57 switch (SvtLanguageOptions::GetScriptTypeOfLanguage(type)) {
58 case SvtScriptType::ASIAN:
59 MsLangId::setConfiguredAsianFallback(type);
60 break;
61 case SvtScriptType::COMPLEX:
62 MsLangId::setConfiguredComplexFallback(type);
63 break;
64 default:
65 MsLangId::setConfiguredWesternFallback(type);
66 break;
72 bool prepareLocale() {
73 // #i42730# Get the windows 16Bit locale, it should be preferred over the UI
74 // locale:
75 setMsLangIdFallback(officecfg::System::L10N::SystemLocale::get());
76 // #i32939# Use system locale to set document default locale:
77 setMsLangIdFallback(officecfg::System::L10N::Locale::get());
78 css::uno::Sequence<OUString> inst(
79 officecfg::Setup::Office::InstalledLocales::get()->getElementNames());
80 OUString locale(officecfg::Office::Linguistic::General::UILocale::get());
81 if (!locale.isEmpty()) {
82 locale = getInstalledLocaleForLanguage(inst, locale);
83 if (locale.isEmpty()) {
84 // Selected language is not/no longer installed:
85 try {
86 std::shared_ptr<comphelper::ConfigurationChanges> batch(
87 comphelper::ConfigurationChanges::create());
88 officecfg::Office::Linguistic::General::UILocale::set(
89 "", batch);
90 batch->commit();
91 } catch (const css::uno::Exception &) {
92 TOOLS_WARN_EXCEPTION("desktop.app", "ignoring");
96 if (locale.isEmpty()) {
97 locale = getInstalledLocaleForLanguage(
98 inst, Desktop::GetCommandLineArgs().GetLanguage());
100 if (locale.isEmpty()) {
101 locale = getInstalledLocaleForSystemUILanguage(inst, true);
103 if (locale.isEmpty()) {
104 return false;
106 LanguageTag tag(locale);
107 // Prepare default config provider by localizing it to the selected
108 // locale this will ensure localized configuration settings to be
109 // selected according to the UI language:
110 css::uno::Reference<css::lang::XLocalizable>(
111 css::configuration::theDefaultProvider::get(
112 comphelper::getProcessComponentContext()),
113 css::uno::UNO_QUERY_THROW)->setLocale(tag.getLocale(false));
114 try {
115 std::shared_ptr<comphelper::ConfigurationChanges> batch(
116 comphelper::ConfigurationChanges::create());
117 officecfg::Setup::L10N::ooLocale::set(locale, batch);
118 batch->commit();
119 } catch (const css::uno::Exception &) {
120 TOOLS_WARN_EXCEPTION("desktop.app", "ignoring");
122 MsLangId::setConfiguredSystemUILanguage(tag.getLanguageType(false));
124 // Note the system language/locale here may or may not be correct before we
125 // actually set it below. It is what could be figured from the system
126 // setting and may only partially match, like "en" for an unsupported
127 // English locale.
128 LanguageTag aSysLocTag( MsLangId::getSystemLanguage());
129 OUString setupSysLoc(officecfg::Setup::L10N::ooSetupSystemLocale::get());
130 if (!setupSysLoc.isEmpty())
131 aSysLocTag.reset( setupSysLoc);
132 // Ensure the system locale is set to a known supported locale.
133 aSysLocTag.makeFallback();
134 LanguageTag::setConfiguredSystemLanguage( aSysLocTag.getLanguageType(false));
136 // #i32939# setting of default document locale
137 // #i32939# this should not be based on the UI language
138 // So obtain the system locale now configured just above and pass it on,
139 // resolved of course.
140 LanguageTag docTag(LANGUAGE_SYSTEM);
141 setMsLangIdFallback(docTag.getBcp47());
143 return true;
148 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */