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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #define WIN32_LEAN_AND_MEAN
25 #include "nlsupport.hxx"
27 #include <osl/nlsupport.h>
28 #include <osl/diagnose.h>
29 #include <osl/process.h>
30 #include <rtl/tencinfo.h>
31 #include <rtl/ustrbuf.hxx>
32 #include <o3tl/char16_t2wchar_t.hxx>
35 * http://msdn.microsoft.com/en-us/library/windows/desktop/dd373848.aspx
36 * (retrieved 2013-02-13) has some weird description for the LOCALE_SISO*
37 * constants: "The maximum number of characters allowed for this string is
38 * nine, including a terminating null character." NINE?!? In ISO 639 and ISO
41 constexpr int ELP_LANGUAGE_FIELD_LENGTH
= 4;
42 constexpr int ELP_COUNTRY_FIELD_LENGTH
= 3;
44 static int GetLocaleInfoN(LPCWSTR l
, LCTYPE t
, DWORD
& n
)
46 return GetLocaleInfoEx(l
, t
| LOCALE_RETURN_NUMBER
, reinterpret_cast<LPWSTR
>(&n
),
47 sizeof(n
) / sizeof(WCHAR
));
50 rtl_TextEncoding SAL_CALL
osl_getTextEncodingFromLocale( rtl_Locale
* pLocale
)
52 /* if pLocale is NULL, use process locale as default */
53 if( nullptr == pLocale
)
54 osl_getProcessLocale( &pLocale
);
56 if (!pLocale
|| !pLocale
->Language
|| !pLocale
->Language
->length
)
57 return RTL_TEXTENCODING_DONTKNOW
;
59 /* Build a BCP47 tag */
60 OUStringBuffer
sLocale(OUString::unacquired(&pLocale
->Language
));
61 if (pLocale
->Country
&& pLocale
->Country
->length
> 0)
62 sLocale
.append("-" + OUString::unacquired(&pLocale
->Country
));
65 /* query ansi codepage for given locale */
67 if (!GetLocaleInfoN(o3tl::toW(sLocale
.getStr()), LOCALE_IDEFAULTANSICODEPAGE
, codepage
))
69 WCHAR resolved
[LOCALE_NAME_MAX_LENGTH
];
70 if (!ResolveLocaleName(o3tl::toW(sLocale
.getStr()), resolved
, std::size(resolved
)))
71 return RTL_TEXTENCODING_DONTKNOW
;
72 if (!GetLocaleInfoN(resolved
, LOCALE_IDEFAULTANSICODEPAGE
, codepage
))
73 return RTL_TEXTENCODING_DONTKNOW
;
76 /* if GetLocaleInfo returns 0, it is a UNICODE only locale */
78 return RTL_TEXTENCODING_UNICODE
;
80 /* find matching rtl encoding */
81 return rtl_getTextEncodingFromWindowsCodePage(codepage
);
84 void imp_getProcessLocale( rtl_Locale
** ppLocale
)
86 WCHAR locale
[LOCALE_NAME_MAX_LENGTH
];
87 WCHAR langCode
[ELP_LANGUAGE_FIELD_LENGTH
];
88 WCHAR ctryCode
[ELP_COUNTRY_FIELD_LENGTH
];
92 /* get the locale name to retrieve information from */
93 /* and call GetLocaleInfo to retrieve the iso codes */
94 if( GetUserDefaultLocaleName(locale
, std::size(locale
)) &&
95 GetLocaleInfoEx( locale
, LOCALE_SISO639LANGNAME
, langCode
, std::size(langCode
) ) &&
96 GetLocaleInfoEx( locale
, LOCALE_SISO3166CTRYNAME
, ctryCode
, std::size(ctryCode
) ) )
98 *ppLocale
= rtl_locale_register( o3tl::toU(langCode
), o3tl::toU(ctryCode
), u
"" );
102 *ppLocale
= rtl_locale_register( u
"C", u
"", u
"" );
106 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */