LibreOffice Module i18nlangtag (master) 1
languagetagicu.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
12
13
14// static
15icu::Locale LanguageTagIcu::getIcuLocale( const LanguageTag & rLanguageTag )
16{
17 if (rLanguageTag.isIsoLocale())
18 {
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());
26 }
27
28 /* TODO: could we optimize this for the isIsoODF() case where only a script
29 * is added? */
30
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());
34}
35
36
37// static
38icu::Locale LanguageTagIcu::getIcuLocale( const LanguageTag & rLanguageTag, std::u16string_view rVariant, std::u16string_view rKeywords )
39{
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()
46 );
47}
48
49// static
50OUString LanguageTagIcu::getDisplayName( const LanguageTag & rLanguageTag, const LanguageTag & rDisplayLanguage )
51{
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;
58
59 if (aUITag != rDisplayLanguage)
60 {
61 aUITag = rDisplayLanguage;
62 aUILocale = getIcuLocale( rDisplayLanguage);
63 }
64
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());
69}
70
71/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static icu::Locale getIcuLocale(const LanguageTag &rLanguageTag)
Obtain language tag as ICU icu::Locale.
static OUString getDisplayName(const LanguageTag &rLanguageTag, const LanguageTag &rDisplayLanguage)
Obtain the display name for a language tag.
Wrapper for liblangtag BCP 47 language tags, MS-LangIDs, locales and conversions in between.
Definition: languagetag.hxx:53
const css::lang::Locale & getLocale(bool bResolveSystem=true) const
Obtain language tag as Locale.
OUString getLanguage() const
Get ISO 639 language code, or BCP 47 language.
const OUString & getBcp47(bool bResolveSystem=true) const
Obtain BCP 47 language tag.
bool isIsoLocale() const
If language tag is a locale that can be expressed using only ISO 639 language codes and ISO 3166 coun...
OUString getCountry() const
Get ISO 3166 country alpha code.
#define LANGUAGE_SYSTEM
Definition: lang.h:104
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
sal_uInt16 sal_Unicode