LCOV - code coverage report
Current view: top level - i18nlangtag/source/isolang - inunx.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 23 39 59.0 %
Date: 2014-04-14 Functions: 3 5 60.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : #include <stdio.h>
      22             : 
      23             : #ifdef MACOSX
      24             : #include <osl/process.h>
      25             : #include <rtl/locale.h>
      26             : #include <rtl/ustring.hxx>
      27             : 
      28             : #else   // MACOSX
      29             : #include <rtl/string.hxx>
      30             : 
      31             : #endif  // MACOSX
      32             : #include <rtl/instance.hxx>
      33             : #include "i18nlangtag/languagetag.hxx"
      34             : #include "i18nlangtag/mslangid.hxx"
      35             : 
      36             : 
      37             : 
      38             : static LanguageType nImplSystemLanguage = LANGUAGE_DONTKNOW;
      39             : static LanguageType nImplSystemUILanguage = LANGUAGE_DONTKNOW;
      40             : 
      41             : 
      42             : 
      43             : // Get locale of category LC_CTYPE of environment variables
      44           1 : static const sal_Char* getLangFromEnvironment()
      45             : {
      46             :     static const sal_Char* pFallback = "C";
      47           1 :     const sal_Char *pLang = NULL;
      48             : 
      49           1 :     pLang = getenv ( "LC_ALL" );
      50           1 :     if (! pLang || pLang[0] == 0)
      51           1 :         pLang = getenv ( "LC_CTYPE" );
      52           1 :     if (! pLang || pLang[0] == 0)
      53           1 :         pLang = getenv( "LANG" );
      54           1 :     if (! pLang || pLang[0] == 0)
      55           0 :         pLang = pFallback;
      56             : 
      57           1 :     return pLang;
      58             : }
      59             : 
      60             : 
      61             : 
      62             : // Get locale of category LC_MESSAGES of environment variables
      63           0 : static const sal_Char* getUILangFromEnvironment()
      64             : {
      65             :     static const sal_Char* pFallback = "C";
      66           0 :     const sal_Char *pLang = NULL;
      67             : 
      68           0 :     pLang = getenv ( "LANGUAGE" );      // respect the GNU extension
      69           0 :     if (! pLang || pLang[0] == 0)
      70           0 :         pLang = getenv ( "LC_ALL" );
      71           0 :     if (! pLang || pLang[0] == 0)
      72           0 :         pLang = getenv ( "LC_MESSAGES" );
      73           0 :     if (! pLang || pLang[0] == 0)
      74           0 :         pLang = getenv( "LANG" );
      75           0 :     if (! pLang || pLang[0] == 0)
      76           0 :         pLang = pFallback;
      77             : 
      78           0 :     return pLang;
      79             : }
      80             : 
      81             : 
      82             : 
      83             : typedef const sal_Char * (*getLangFromEnv)();
      84             : 
      85           1 : static void getPlatformSystemLanguageImpl( LanguageType& rSystemLanguage,
      86             :         getLangFromEnv pGetLangFromEnv )
      87             : {
      88             :     /* get the language from the user environment */
      89           1 :     LanguageType nLang = rSystemLanguage;
      90           1 :     if ( nLang == LANGUAGE_DONTKNOW )
      91             :     {
      92           1 :         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex());
      93           1 :         nLang = rSystemLanguage;
      94           1 :         if ( nLang == LANGUAGE_DONTKNOW )
      95             :         {
      96             : #ifdef MACOSX
      97             :             rtl_Locale    *procLocale;
      98             :             (void) pGetLangFromEnv; /* unused */
      99             : 
     100             :             if ( osl_getProcessLocale(&procLocale) == osl_Process_E_None )
     101             :             {
     102             :                 nLang = LanguageTag( *procLocale ).makeFallback().getLanguageType();
     103             :                 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
     104             :                 rSystemLanguage = nLang;
     105             : #ifdef DEBUG
     106             :                 if ( rSystemLanguage == LANGUAGE_DONTKNOW )
     107             :                     fprintf( stderr, "intnunx.cxx:  failed to convert osl_getProcessLocale() language to system language.\n" );
     108             : #endif
     109             :             }
     110             : #else   /* MACOSX */
     111           1 :             OString aUnxLang( (pGetLangFromEnv)() );
     112           1 :             nLang = MsLangId::convertUnxByteStringToLanguage( aUnxLang );
     113             :             OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
     114           1 :             rSystemLanguage = nLang;
     115             : #endif  /* MACOSX */
     116             :         }
     117             :         else {
     118             :             OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
     119           1 :         }
     120             :     }
     121           1 : }
     122             : 
     123             : 
     124             : 
     125           1 : LanguageType MsLangId::getPlatformSystemLanguage()
     126             : {
     127           1 :     getPlatformSystemLanguageImpl( nImplSystemLanguage, &getLangFromEnvironment);
     128           1 :     return nImplSystemLanguage;
     129             : }
     130             : 
     131             : 
     132             : 
     133           0 : LanguageType MsLangId::getPlatformSystemUILanguage()
     134             : {
     135           0 :     getPlatformSystemLanguageImpl( nImplSystemUILanguage, &getUILangFromEnvironment);
     136           0 :     return nImplSystemUILanguage;
     137             : }
     138             : 
     139             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10