Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkLocaleSwitch.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "mitkLocaleSwitch.h"
18 
19 #include "mitkLogMacros.h"
20 
21 #include <clocale>
22 #include <string>
23 
24 namespace mitk
25 {
26  struct LocaleSwitch::Impl
27  {
28  explicit Impl(const std::string &newLocale);
29 
30  ~Impl();
31 
32  private:
34  std::string m_OldLocale;
35 
37  const std::string m_NewLocale;
38  };
39 
40  LocaleSwitch::Impl::Impl(const std::string &newLocale) : m_NewLocale(newLocale)
41  {
42  // query and keep the current locale
43  const char *currentLocale = std::setlocale(LC_ALL, nullptr);
44  if (currentLocale != nullptr)
45  m_OldLocale = currentLocale;
46  else
47  m_OldLocale = "";
48 
49  // install the new locale if it different from the current one
50  if (m_NewLocale != m_OldLocale)
51  {
52  if (!std::setlocale(LC_ALL, m_NewLocale.c_str()))
53  {
54  MITK_INFO << "Could not switch to locale " << m_NewLocale;
55  m_OldLocale = "";
56  }
57  }
58  }
59 
60  LocaleSwitch::Impl::~Impl()
61  {
62  if (!m_OldLocale.empty() && m_OldLocale != m_NewLocale && !std::setlocale(LC_ALL, m_OldLocale.c_str()))
63  {
64  MITK_INFO << "Could not reset original locale " << m_OldLocale;
65  }
66  }
67 
68  LocaleSwitch::LocaleSwitch(const char *newLocale) : m_LocaleSwitchImpl(new Impl(newLocale)) {}
69  LocaleSwitch::~LocaleSwitch() { delete m_LocaleSwitchImpl; }
70 }
#define MITK_INFO
Definition: mitkLogMacros.h:22
LocaleSwitch(const char *newLocale)
DataCollection - Class to facilitate loading/accessing structured data.