LibreOffice Module desktop (master)  1
langselect.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <sal/config.h>
21 
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>
30 #include <i18nlangtag/lang.h>
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 <tools/diagnose_ex.h>
40 
41 #include <app.hxx>
42 
43 #include "cmdlineargs.hxx"
44 #include "langselect.hxx"
45 
47 
48 namespace {
49 
50 OUString foundLocale;
51 
52 void setMsLangIdFallback(OUString const & locale) {
53  // #i32939# setting of default document language
54  // See #i42730# for rules for determining source of settings
55  if (!locale.isEmpty()) {
58  case SvtScriptType::ASIAN:
60  break;
61  case SvtScriptType::COMPLEX:
63  break;
64  default:
66  break;
67  }
68  }
69 }
70 
71 }
72 
73 bool prepareLocale() {
74  // #i42730# Get the windows 16Bit locale, it should be preferred over the UI
75  // locale:
76  setMsLangIdFallback(officecfg::System::L10N::SystemLocale::get());
77  // #i32939# Use system locale to set document default locale:
78  setMsLangIdFallback(officecfg::System::L10N::Locale::get());
79  css::uno::Sequence<OUString> inst(
80  officecfg::Setup::Office::InstalledLocales::get()->getElementNames());
81  OUString locale(officecfg::Office::Linguistic::General::UILocale::get());
82  if (!locale.isEmpty()) {
83  locale = getInstalledLocaleForLanguage(inst, locale);
84  if (locale.isEmpty()) {
85  // Selected language is not/no longer installed:
86  try {
87  std::shared_ptr<comphelper::ConfigurationChanges> batch(
89  officecfg::Office::Linguistic::General::UILocale::set(
90  "", batch);
91  batch->commit();
92  } catch (const css::uno::Exception &) {
93  TOOLS_WARN_EXCEPTION("desktop.app", "ignoring");
94  }
95  }
96  }
97  bool cmdLanguage = false;
98  if (locale.isEmpty()) {
101  if (!locale.isEmpty()) {
102  cmdLanguage = true;
103  }
104  }
105  if (locale.isEmpty()) {
106  locale = getInstalledLocaleForSystemUILanguage(inst, true);
107  }
108  if (locale.isEmpty()) {
109  return false;
110  }
111  LanguageTag tag(locale);
112  // Prepare default config provider by localizing it to the selected
113  // locale this will ensure localized configuration settings to be
114  // selected according to the UI language:
115  css::uno::Reference<css::lang::XLocalizable>(
116  css::configuration::theDefaultProvider::get(
118  css::uno::UNO_QUERY_THROW)->setLocale(tag.getLocale(false));
119  if (!cmdLanguage) {
120  try {
121  std::shared_ptr<comphelper::ConfigurationChanges> batch(
123  officecfg::Setup::L10N::ooLocale::set(locale, batch);
124  batch->commit();
125  } catch (const css::uno::Exception &) {
126  TOOLS_WARN_EXCEPTION("desktop.app", "ignoring");
127  }
128  }
130 
131  OUString setupSysLoc(officecfg::Setup::L10N::ooSetupSystemLocale::get());
133  setupSysLoc.isEmpty()
135  : LanguageTag(setupSysLoc).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.
141  setMsLangIdFallback(docTag.getBcp47());
142 
143  foundLocale = locale;
144  return true;
145 }
146 
147 }
148 
149 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static void setConfiguredComplexFallback(LanguageType nLang)
static void setConfiguredAsianFallback(LanguageType nLang)
static void setConfiguredSystemUILanguage(LanguageType nLang)
static void setConfiguredSystemLanguage(LanguageType nLang)
LanguageType getLanguageType(bool bResolveSystem=true) const
const css::lang::Locale & getLocale(bool bResolveSystem=true) const
OUString getInstalledLocaleForLanguage(css::uno::Sequence< OUString > const &installed, OUString const &locale)
static LanguageType convertToLanguageTypeWithFallback(const OUString &rBcp47)
static void setConfiguredWesternFallback(LanguageType nLang)
static std::shared_ptr< ConfigurationChanges > create(css::uno::Reference< css::uno::XComponentContext > const &context=comphelper::getProcessComponentContext())
static SvtScriptType GetScriptTypeOfLanguage(LanguageType nLang)
sal_uInt32 tag
static LanguageType getSystemLanguage()
#define TOOLS_WARN_EXCEPTION(area, stream)
#define LANGUAGE_SYSTEM
LanguageType GetLanguage(SwWrtShell &rSh, sal_uInt16 nLangWhichId)
static CommandLineArgs & GetCommandLineArgs()
Definition: app.cxx:387
OUString getInstalledLocaleForSystemUILanguage(const css::uno::Sequence< OUString > &rLocaleElementNames, bool bRequestInstallIfMissing, const OUString &rPreferredLocale)
Reference< XComponentContext > getProcessComponentContext()
ResultType type