LibreOffice Module i18nlangtag (master) 1
inunx.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 * 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 <stdlib.h>
21
22#ifdef MACOSX
23#include <osl/process.h>
24#include <rtl/locale.h>
25#include <rtl/ustring.hxx>
27
28#else // MACOSX
29#include <rtl/string.hxx>
30#endif // MACOSX
31
32#include <osl/mutex.hxx>
33#include <osl/doublecheckedlocking.h>
35
36
39
40
41// Get locale of category LC_CTYPE of environment variables
42static const char* getLangFromEnvironment( bool& rbColonList )
43{
44 static const char* const pFallback = "C";
45 const char *pLang = nullptr;
46
47 rbColonList = false;
48 pLang = getenv ( "LC_ALL" );
49 if (! pLang || pLang[0] == 0)
50 pLang = getenv ( "LC_CTYPE" );
51 if (! pLang || pLang[0] == 0)
52 pLang = getenv( "LANG" );
53 if (! pLang || pLang[0] == 0)
54 pLang = pFallback;
55
56 return pLang;
57}
58
59
60// Get locale of category LC_MESSAGES of environment variables
61static const char* getUILangFromEnvironment( bool& rbColonList )
62{
63 static const char* const pFallback = "C";
64 const char *pLang = nullptr;
65
66 rbColonList = true;
67 pLang = getenv ( "LANGUAGE" ); // respect the GNU extension
68 if (! pLang || pLang[0] == 0)
69 {
70 rbColonList = false;
71 pLang = getenv ( "LC_ALL" );
72 }
73 if (! pLang || pLang[0] == 0)
74 pLang = getenv ( "LC_MESSAGES" );
75 if (! pLang || pLang[0] == 0)
76 pLang = getenv( "LANG" );
77 if (! pLang || pLang[0] == 0)
78 pLang = pFallback;
79
80 return pLang;
81}
82
83
84typedef const char * (*getLangFromEnv)( bool& rbColonList );
85
86static void getPlatformSystemLanguageImpl( LanguageType& rSystemLanguage,
87 getLangFromEnv pGetLangFromEnv )
88{
89 /* get the language from the user environment */
90 LanguageType nLang = rSystemLanguage;
91 if ( nLang != LANGUAGE_DONTKNOW )
92 return;
93
94 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex());
95 nLang = rSystemLanguage;
96 if ( nLang == LANGUAGE_DONTKNOW )
97 {
98#ifdef MACOSX
99 rtl_Locale *procLocale;
100 (void) pGetLangFromEnv; /* unused */
101
102 if ( osl_getProcessLocale(&procLocale) == osl_Process_E_None )
103 {
104 nLang = LanguageTag( *procLocale ).makeFallback().getLanguageType();
105 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
106 rSystemLanguage = nLang;
107#ifdef DEBUG
108 if ( rSystemLanguage == LANGUAGE_DONTKNOW )
109 fprintf( stderr, "intnunx.cxx: failed to convert osl_getProcessLocale() language to system language.\n" );
110#endif
111 }
112#else /* MACOSX */
113 bool bColonList = false;
114 OString aUnxLang( pGetLangFromEnv( bColonList));
115 if (bColonList)
116 {
117 // Only a very simple "take first". If empty try second or keep empty.
118 sal_Int32 n = aUnxLang.indexOf(':');
119 if (n >= 0)
120 {
121 sal_Int32 s = 0;
122 if (n == 0 && aUnxLang.getLength() > 1)
123 {
124 n = aUnxLang.indexOf(':', 1);
125 if (n < 0)
126 n = aUnxLang.getLength();
127 if (n < 2)
128 s = n = 0;
129 else
130 {
131 s = 1;
132 --n;
133 }
134 }
135 aUnxLang = aUnxLang.copy(s,n);
136 }
137 }
139 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
140 rSystemLanguage = nLang;
141#endif /* MACOSX */
142 }
143 else {
144 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
145 }
146}
147
148
150{
152 return nImplSystemLanguage;
153}
154
155
157{
160}
161
162/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Wrapper for liblangtag BCP 47 language tags, MS-LangIDs, locales and conversions in between.
Definition: languagetag.hxx:53
LanguageType getLanguageType(bool bResolveSystem=true) const
Obtain mapping to MS-LangID.
LanguageTag & makeFallback()
Fall back to a known locale.
static LanguageType getPlatformSystemLanguage()
Definition: inunx.cxx:149
static LanguageType getPlatformSystemUILanguage()
Definition: inunx.cxx:156
static LanguageType convertUnxByteStringToLanguage(std::string_view rString)
Definition: isolang.cxx:1458
const char *(* getLangFromEnv)(bool &rbColonList)
Definition: inunx.cxx:84
static LanguageType nImplSystemUILanguage
Definition: inunx.cxx:38
static void getPlatformSystemLanguageImpl(LanguageType &rSystemLanguage, getLangFromEnv pGetLangFromEnv)
Definition: inunx.cxx:86
static LanguageType nImplSystemLanguage
Definition: inunx.cxx:37
static const char * getLangFromEnvironment(bool &rbColonList)
Definition: inunx.cxx:42
static const char * getUILangFromEnvironment(bool &rbColonList)
Definition: inunx.cxx:61
sal_Int64 n
#define LANGUAGE_DONTKNOW
Definition: lang.h:101