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/.
10 #include <i18nlangtag/languagetagicu.hxx>
11 #include <i18nlangtag/languagetag.hxx>
15 icu::Locale
LanguageTagIcu::getIcuLocale( const LanguageTag
& rLanguageTag
)
17 if (rLanguageTag
.isIsoLocale())
20 const css::lang::Locale
& rLocale
= rLanguageTag
.getLocale();
21 if (rLocale
.Country
.isEmpty())
22 return icu::Locale( OUStringToOString( rLocale
.Language
, RTL_TEXTENCODING_ASCII_US
).getStr());
24 OUStringToOString( rLocale
.Language
, RTL_TEXTENCODING_ASCII_US
).getStr(),
25 OUStringToOString( rLocale
.Country
, RTL_TEXTENCODING_ASCII_US
).getStr());
28 /* TODO: could we optimize this for the isIsoODF() case where only a script
31 // Let ICU decide how it wants a BCP47 string stuffed into its Locale.
32 return icu::Locale::createFromName(
33 OUStringToOString( rLanguageTag
.getBcp47(), RTL_TEXTENCODING_ASCII_US
).getStr());
38 icu::Locale
LanguageTagIcu::getIcuLocale( const LanguageTag
& rLanguageTag
, std::u16string_view rVariant
, std::u16string_view rKeywords
)
40 /* FIXME: how should this work with any BCP47? */
42 OUStringToOString( rLanguageTag
.getLanguage(), RTL_TEXTENCODING_ASCII_US
).getStr(),
43 OUStringToOString( rLanguageTag
.getCountry(), RTL_TEXTENCODING_ASCII_US
).getStr(),
44 OUStringToOString( rVariant
, RTL_TEXTENCODING_ASCII_US
).getStr(),
45 OUStringToOString( rKeywords
, RTL_TEXTENCODING_ASCII_US
).getStr()
50 OUString
LanguageTagIcu::getDisplayName( const LanguageTag
& rLanguageTag
, const LanguageTag
& rDisplayLanguage
)
52 // This will be initialized by the first call; as the UI language doesn't
53 // change the tag mostly stays the same, unless someone overrides it for a
54 // call here, and thus obtaining the UI icu::Locale has to be done only
56 static thread_local LanguageTag
aUITag( LANGUAGE_SYSTEM
);
57 static thread_local
icu::Locale aUILocale
;
59 if (aUITag
!= rDisplayLanguage
)
61 aUITag
= rDisplayLanguage
;
62 aUILocale
= getIcuLocale( rDisplayLanguage
);
65 icu::Locale
aLocale( getIcuLocale( rLanguageTag
));
66 icu::UnicodeString aResult
;
67 aLocale
.getDisplayName( aUILocale
, aResult
);
68 return OUString( reinterpret_cast<const sal_Unicode
*>(aResult
.getBuffer()), aResult
.length());
71 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */