1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <sal/log.hxx>
38 #include <sal/types.h>
39 #include <svl/languageoptions.hxx>
40 #include <svtools/langhelp.hxx>
41 #include <tools/diagnose_ex.h>
45 #include "cmdlineargs.hxx"
46 #include "langselect.hxx"
48 namespace desktop
{ namespace langselect
{
54 void setMsLangIdFallback(OUString
const & locale
) {
55 // #i32939# setting of default document language
56 // See #i42730# for rules for determining source of settings
57 if (!locale
.isEmpty()) {
58 LanguageType type
= LanguageTag::convertToLanguageTypeWithFallback(locale
);
59 switch (SvtLanguageOptions::GetScriptTypeOfLanguage(type
)) {
60 case SvtScriptType::ASIAN
:
61 MsLangId::setConfiguredAsianFallback(type
);
63 case SvtScriptType::COMPLEX
:
64 MsLangId::setConfiguredComplexFallback(type
);
67 MsLangId::setConfiguredWesternFallback(type
);
75 bool prepareLocale() {
76 // #i42730# Get the windows 16Bit locale, it should be preferred over the UI
78 setMsLangIdFallback(officecfg::System::L10N::SystemLocale::get());
79 // #i32939# Use system locale to set document default locale:
80 setMsLangIdFallback(officecfg::System::L10N::Locale::get());
81 css::uno::Sequence
<OUString
> inst(
82 officecfg::Setup::Office::InstalledLocales::get()->getElementNames());
83 OUString
locale(officecfg::Office::Linguistic::General::UILocale::get());
84 if (!locale
.isEmpty()) {
85 locale
= getInstalledLocaleForLanguage(inst
, locale
);
86 if (locale
.isEmpty()) {
87 // Selected language is not/no longer installed:
89 std::shared_ptr
<comphelper::ConfigurationChanges
> batch(
90 comphelper::ConfigurationChanges::create());
91 officecfg::Office::Linguistic::General::UILocale::set(
94 } catch (const css::uno::Exception
&) {
95 TOOLS_WARN_EXCEPTION("desktop.app", "ignoring");
99 bool cmdLanguage
= false;
100 if (locale
.isEmpty()) {
101 locale
= getInstalledLocaleForLanguage(
102 inst
, Desktop::GetCommandLineArgs().GetLanguage());
103 if (!locale
.isEmpty()) {
107 if (locale
.isEmpty()) {
108 locale
= getInstalledLocaleForSystemUILanguage(inst
, true);
110 if (locale
.isEmpty()) {
113 LanguageTag
tag(locale
);
114 // Prepare default config provider by localizing it to the selected
115 // locale this will ensure localized configuration settings to be
116 // selected according to the UI language:
117 css::uno::Reference
<css::lang::XLocalizable
>(
118 css::configuration::theDefaultProvider::get(
119 comphelper::getProcessComponentContext()),
120 css::uno::UNO_QUERY_THROW
)->setLocale(tag
.getLocale(false));
123 std::shared_ptr
<comphelper::ConfigurationChanges
> batch(
124 comphelper::ConfigurationChanges::create());
125 officecfg::Setup::L10N::ooLocale::set(locale
, batch
);
127 } catch (const css::uno::Exception
&) {
128 TOOLS_WARN_EXCEPTION("desktop.app", "ignoring");
131 MsLangId::setConfiguredSystemUILanguage(tag
.getLanguageType(false));
133 OUString
setupSysLoc(officecfg::Setup::L10N::ooSetupSystemLocale::get());
134 LanguageTag::setConfiguredSystemLanguage(
135 setupSysLoc
.isEmpty()
136 ? MsLangId::getSystemLanguage()
137 : LanguageTag(setupSysLoc
).getLanguageType(false));
138 // #i32939# setting of default document locale
139 // #i32939# this should not be based on the UI language
140 // So obtain the system locale now configured just above and pass it on,
141 // resolved of course.
142 LanguageTag
docTag(LANGUAGE_SYSTEM
);
143 setMsLangIdFallback(docTag
.getBcp47());
145 foundLocale
= locale
;
151 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */