summaryrefslogtreecommitdiff
path: root/desktop/source/app/langselect.cxx
blob: 291e7082222268b9c658a082280f9858508f0cdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include "sal/config.h"

#include "boost/shared_ptr.hpp"
#include "com/sun/star/configuration/theDefaultProvider.hpp"
#include "com/sun/star/container/XNameAccess.hpp"
#include "com/sun/star/lang/XLocalizable.hpp"
#include "com/sun/star/uno/Exception.hpp"
#include "com/sun/star/uno/Reference.hxx"
#include "com/sun/star/uno/Sequence.hxx"
#include "comphelper/configuration.hxx"
#include "comphelper/processfactory.hxx"
#include "i18nlangtag/lang.h"
#include "i18nlangtag/languagetag.hxx"
#include "i18nlangtag/mslangid.hxx"
#include "officecfg/Office/Linguistic.hxx"
#include "officecfg/Setup.hxx"
#include "officecfg/System.hxx"
#include "rtl/ustring.hxx"
#include "sal/log.hxx"
#include "sal/types.h"
#include "svl/languageoptions.hxx"

#include "app.hxx"

#include "cmdlineargs.hxx"
#include "langselect.hxx"

namespace desktop { namespace langselect {

namespace {

OUString foundLocale;

OUString getInstalledLocale(
    css::uno::Sequence<OUString> const & installed, OUString const & locale)
{
    for (sal_Int32 i = 0; i != installed.getLength(); ++i) {
        if (installed[i] == locale) {
            return installed[i];
        }
    }
    ::std::vector<OUString> fallbacks( LanguageTag( locale).getFallbackStrings( false));
    for (size_t f=0; f < fallbacks.size(); ++f) {
        const OUString& rf = fallbacks[f];
        for (sal_Int32 i = 0; i != installed.getLength(); ++i) {
            if (installed[i] == rf) {
                return installed[i];
            }
        }
    }
    return OUString();
}

void setMsLangIdFallback(OUString const & locale) {
    // #i32939# setting of default document language
    // See #i42730# for rules for determining source of settings
    if (!locale.isEmpty()) {
        LanguageType type = LanguageTag::convertToLanguageTypeWithFallback(locale);
        switch (SvtLanguageOptions::GetScriptTypeOfLanguage(type)) {
        case SCRIPTTYPE_ASIAN:
            MsLangId::setConfiguredAsianFallback(type);
            break;
        case SCRIPTTYPE_COMPLEX:
            MsLangId::setConfiguredComplexFallback(type);
            break;
        default:
            MsLangId::setConfiguredWesternFallback(type);
            break;
        }
    }
}

}

OUString getEmergencyLocale() {
    if (!foundLocale.isEmpty()) {
        return foundLocale;
    }
    try {
        css::uno::Sequence<OUString> inst(
            officecfg::Setup::Office::InstalledLocales::get()->
            getElementNames());
        OUString locale(
            getInstalledLocale(
                inst,
                officecfg::Office::Linguistic::General::UILocale::get()));
        if (!locale.isEmpty()) {
            return locale;
        }
        locale = getInstalledLocale(
            inst, officecfg::System::L10N::UILocale::get());
        if (!locale.isEmpty()) {
            return locale;
        }
        locale = getInstalledLocale(inst, "en-US");
        if (!locale.isEmpty()) {
            return locale;
        }
        if (inst.hasElements()) {
            return inst[0];
        }
    } catch (css::uno::Exception & e) {
        SAL_WARN("desktop.app", "ignoring Exception \"" << e.Message << "\"");
    }
    return OUString();
}

bool prepareLocale() {
    // #i42730# Get the windows 16Bit locale, it should be preferred over the UI
    // locale:
    setMsLangIdFallback(officecfg::System::L10N::SystemLocale::get());
    // #i32939# Use system locale to set document default locale:
    setMsLangIdFallback(officecfg::System::L10N::Locale::get());
    css::uno::Sequence<OUString> inst(
        officecfg::Setup::Office::InstalledLocales::get()->getElementNames());
    OUString locale(officecfg::Office::Linguistic::General::UILocale::get());
    if (!locale.isEmpty()) {
        locale = getInstalledLocale(inst, locale);
        if (locale.isEmpty()) {
            // Selected language is not/no longer installed:
            try {
                boost::shared_ptr<comphelper::ConfigurationChanges> batch(
                    comphelper::ConfigurationChanges::create());
                officecfg::Office::Linguistic::General::UILocale::set(
                    "", batch);
                batch->commit();
            } catch (css::uno::Exception & e) {
                SAL_WARN(
                    "desktop.app",
                    "ignoring Exception \"" << e.Message << "\"");
            }
        }
    }
    bool cmdLanguage = false;
    if (locale.isEmpty()) {
        locale = getInstalledLocale(
            inst, Desktop::GetCommandLineArgs().GetLanguage());
        if (!locale.isEmpty()) {
            cmdLanguage = true;
        }
    }
    if (locale.isEmpty()) {
        locale = getInstalledLocale(
            inst, officecfg::System::L10N::UILocale::get());
    }
    if (locale.isEmpty()) {
        locale = getInstalledLocale(inst, "en-US");
    }
    if (locale.isEmpty() && inst.hasElements()) {
        locale = inst[0];
    }
    if (locale.isEmpty()) {
        return false;
    }
    LanguageTag tag(locale);
    // Prepare default config provider by localizing it to the selected
    // locale this will ensure localized configuration settings to be
    // selected according to the UI language:
    css::uno::Reference<css::lang::XLocalizable>(
        com::sun::star::configuration::theDefaultProvider::get(
            comphelper::getProcessComponentContext()),
        css::uno::UNO_QUERY_THROW)->setLocale(tag.getLocale(false));
    if (!cmdLanguage) {
        try {
            boost::shared_ptr<comphelper::ConfigurationChanges> batch(
                comphelper::ConfigurationChanges::create());
            officecfg::Setup::L10N::ooLocale::set(locale, batch);
            batch->commit();
        } catch (css::uno::Exception & e) {
            SAL_WARN(
                "desktop.app", "ignoring Exception \"" << e.Message << "\"");
        }
    }
    MsLangId::setConfiguredSystemUILanguage(tag.getLanguageType(false));
    OUString setupSysLoc(officecfg::Setup::L10N::ooSetupSystemLocale::get());
    LanguageTag::setConfiguredSystemLanguage(
        setupSysLoc.isEmpty()
        ? MsLangId::getSystemLanguage()
        : LanguageTag(setupSysLoc).getLanguageType(false));
    // #i32939# setting of default document locale
    // #i32939# this should not be based on the UI language
    setMsLangIdFallback(locale);
    foundLocale = locale;
    return true;
}

} }

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */