Line data Source code
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 <boost/shared_ptr.hpp>
23 : #include <com/sun/star/configuration/theDefaultProvider.hpp>
24 : #include <com/sun/star/container/XNameAccess.hpp>
25 : #include <com/sun/star/lang/XLocalizable.hpp>
26 : #include <com/sun/star/uno/Exception.hpp>
27 : #include <com/sun/star/uno/Reference.hxx>
28 : #include <com/sun/star/uno/Sequence.hxx>
29 : #include <comphelper/configuration.hxx>
30 : #include <comphelper/processfactory.hxx>
31 : #include <i18nlangtag/lang.h>
32 : #include <i18nlangtag/languagetag.hxx>
33 : #include <i18nlangtag/mslangid.hxx>
34 : #include "officecfg/Office/Linguistic.hxx"
35 : #include "officecfg/Setup.hxx"
36 : #include "officecfg/System.hxx"
37 : #include <rtl/ustring.hxx>
38 : #include <sal/log.hxx>
39 : #include <sal/types.h>
40 : #include <svl/languageoptions.hxx>
41 :
42 : #include "app.hxx"
43 :
44 : #include "cmdlineargs.hxx"
45 : #include "langselect.hxx"
46 :
47 : namespace desktop { namespace langselect {
48 :
49 : namespace {
50 :
51 160 : OUString foundLocale;
52 :
53 320 : OUString getInstalledLocale(
54 : css::uno::Sequence<OUString> const & installed, OUString const & locale)
55 : {
56 320 : if (locale.isEmpty())
57 160 : return OUString(); // do not attempt to resolve anything
58 :
59 160 : for (sal_Int32 i = 0; i != installed.getLength(); ++i) {
60 160 : if (installed[i] == locale) {
61 160 : return installed[i];
62 : }
63 : }
64 0 : ::std::vector<OUString> fallbacks( LanguageTag( locale).getFallbackStrings( false));
65 0 : for (size_t f=0; f < fallbacks.size(); ++f) {
66 0 : const OUString& rf = fallbacks[f];
67 0 : for (sal_Int32 i = 0; i != installed.getLength(); ++i) {
68 0 : if (installed[i] == rf) {
69 0 : return installed[i];
70 : }
71 : }
72 : }
73 0 : return OUString();
74 : }
75 :
76 480 : void setMsLangIdFallback(OUString const & locale) {
77 : // #i32939# setting of default document language
78 : // See #i42730# for rules for determining source of settings
79 480 : if (!locale.isEmpty()) {
80 480 : LanguageType type = LanguageTag::convertToLanguageTypeWithFallback(locale);
81 480 : switch (SvtLanguageOptions::GetScriptTypeOfLanguage(type)) {
82 : case SCRIPTTYPE_ASIAN:
83 0 : MsLangId::setConfiguredAsianFallback(type);
84 0 : break;
85 : case SCRIPTTYPE_COMPLEX:
86 0 : MsLangId::setConfiguredComplexFallback(type);
87 0 : break;
88 : default:
89 480 : MsLangId::setConfiguredWesternFallback(type);
90 480 : break;
91 : }
92 : }
93 480 : }
94 :
95 : }
96 :
97 0 : OUString getEmergencyLocale() {
98 0 : if (!foundLocale.isEmpty()) {
99 0 : return foundLocale;
100 : }
101 : try {
102 : css::uno::Sequence<OUString> inst(
103 0 : officecfg::Setup::Office::InstalledLocales::get()->
104 0 : getElementNames());
105 : OUString locale(
106 : getInstalledLocale(
107 : inst,
108 0 : officecfg::Office::Linguistic::General::UILocale::get()));
109 0 : if (!locale.isEmpty()) {
110 0 : return locale;
111 : }
112 0 : locale = getInstalledLocale(
113 0 : inst, officecfg::System::L10N::UILocale::get());
114 0 : if (!locale.isEmpty()) {
115 0 : return locale;
116 : }
117 0 : locale = getInstalledLocale(inst, "en-US");
118 0 : if (!locale.isEmpty()) {
119 0 : return locale;
120 : }
121 0 : if (inst.hasElements()) {
122 0 : return inst[0];
123 0 : }
124 0 : } catch (css::uno::Exception & e) {
125 : SAL_WARN("desktop.app", "ignoring Exception \"" << e.Message << "\"");
126 : }
127 0 : return OUString();
128 : }
129 :
130 160 : bool prepareLocale() {
131 : // #i42730# Get the windows 16Bit locale, it should be preferred over the UI
132 : // locale:
133 160 : setMsLangIdFallback(officecfg::System::L10N::SystemLocale::get());
134 : // #i32939# Use system locale to set document default locale:
135 160 : setMsLangIdFallback(officecfg::System::L10N::Locale::get());
136 : css::uno::Sequence<OUString> inst(
137 160 : officecfg::Setup::Office::InstalledLocales::get()->getElementNames());
138 320 : OUString locale(officecfg::Office::Linguistic::General::UILocale::get());
139 160 : if (!locale.isEmpty()) {
140 0 : locale = getInstalledLocale(inst, locale);
141 0 : if (locale.isEmpty()) {
142 : // Selected language is not/no longer installed:
143 : try {
144 : boost::shared_ptr<comphelper::ConfigurationChanges> batch(
145 0 : comphelper::ConfigurationChanges::create());
146 : officecfg::Office::Linguistic::General::UILocale::set(
147 0 : "", batch);
148 0 : batch->commit();
149 0 : } catch (css::uno::Exception & e) {
150 : SAL_WARN(
151 : "desktop.app",
152 : "ignoring Exception \"" << e.Message << "\"");
153 : }
154 : }
155 : }
156 160 : bool cmdLanguage = false;
157 160 : if (locale.isEmpty()) {
158 320 : locale = getInstalledLocale(
159 320 : inst, Desktop::GetCommandLineArgs().GetLanguage());
160 160 : if (!locale.isEmpty()) {
161 0 : cmdLanguage = true;
162 : }
163 : }
164 160 : if (locale.isEmpty()) {
165 320 : locale = getInstalledLocale(
166 160 : inst, officecfg::System::L10N::UILocale::get());
167 : }
168 160 : if (locale.isEmpty()) {
169 0 : locale = getInstalledLocale(inst, "en-US");
170 : }
171 160 : if (locale.isEmpty() && inst.hasElements()) {
172 0 : locale = inst[0];
173 : }
174 160 : if (locale.isEmpty()) {
175 0 : return false;
176 : }
177 320 : LanguageTag tag(locale);
178 : // Prepare default config provider by localizing it to the selected
179 : // locale this will ensure localized configuration settings to be
180 : // selected according to the UI language:
181 : css::uno::Reference<css::lang::XLocalizable>(
182 : com::sun::star::configuration::theDefaultProvider::get(
183 : comphelper::getProcessComponentContext()),
184 160 : css::uno::UNO_QUERY_THROW)->setLocale(tag.getLocale(false));
185 160 : if (!cmdLanguage) {
186 : try {
187 : boost::shared_ptr<comphelper::ConfigurationChanges> batch(
188 160 : comphelper::ConfigurationChanges::create());
189 160 : officecfg::Setup::L10N::ooLocale::set(locale, batch);
190 160 : batch->commit();
191 0 : } catch (css::uno::Exception & e) {
192 : SAL_WARN(
193 : "desktop.app", "ignoring Exception \"" << e.Message << "\"");
194 : }
195 : }
196 160 : MsLangId::setConfiguredSystemUILanguage(tag.getLanguageType(false));
197 320 : OUString setupSysLoc(officecfg::Setup::L10N::ooSetupSystemLocale::get());
198 : LanguageTag::setConfiguredSystemLanguage(
199 160 : setupSysLoc.isEmpty()
200 160 : ? MsLangId::getSystemLanguage()
201 320 : : LanguageTag(setupSysLoc).getLanguageType(false));
202 : // #i32939# setting of default document locale
203 : // #i32939# this should not be based on the UI language
204 160 : setMsLangIdFallback(locale);
205 160 : foundLocale = locale;
206 320 : return true;
207 : }
208 :
209 480 : } }
210 :
211 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|