Bump version to 24.04.3.4
[LibreOffice.git] / i18nlangtag / source / languagetag / languagetagicu.cxx
blobfd1c9bc75670c8524f4d8a9c16a94c89b41386a7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #include <i18nlangtag/languagetagicu.hxx>
11 #include <i18nlangtag/languagetag.hxx>
14 // static
15 icu::Locale LanguageTagIcu::getIcuLocale( const LanguageTag & rLanguageTag )
17 if (rLanguageTag.isIsoLocale())
19 // The simple case.
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());
23 return icu::Locale(
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
29 * is added? */
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());
37 // static
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? */
41 return icu::Locale(
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()
49 // static
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
55 // once.
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: */