Medical Imaging Interaction Toolkit  2018.4.99-389bf124
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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkLocaleSwitch.h"
14 
15 #include "mitkLogMacros.h"
16 
17 #include <clocale>
18 #include <string>
19 
20 namespace mitk
21 {
22  struct LocaleSwitch::Impl
23  {
24  explicit Impl(const std::string &newLocale);
25 
26  ~Impl();
27 
28  private:
30  std::string m_OldLocale;
31 
33  const std::string m_NewLocale;
34  };
35 
36  LocaleSwitch::Impl::Impl(const std::string &newLocale) : m_NewLocale(newLocale)
37  {
38  // query and keep the current locale
39  const char *currentLocale = std::setlocale(LC_ALL, nullptr);
40  if (currentLocale != nullptr)
41  m_OldLocale = currentLocale;
42  else
43  m_OldLocale = "";
44 
45  // install the new locale if it different from the current one
46  if (m_NewLocale != m_OldLocale)
47  {
48  if (!std::setlocale(LC_ALL, m_NewLocale.c_str()))
49  {
50  MITK_INFO << "Could not switch to locale " << m_NewLocale;
51  m_OldLocale = "";
52  }
53  }
54  }
55 
56  LocaleSwitch::Impl::~Impl()
57  {
58  if (!m_OldLocale.empty() && m_OldLocale != m_NewLocale && !std::setlocale(LC_ALL, m_OldLocale.c_str()))
59  {
60  MITK_INFO << "Could not reset original locale " << m_OldLocale;
61  }
62  }
63 
64  LocaleSwitch::LocaleSwitch(const char *newLocale) : m_LocaleSwitchImpl(new Impl(newLocale)) {}
65  LocaleSwitch::~LocaleSwitch() { delete m_LocaleSwitchImpl; }
66 }
#define MITK_INFO
Definition: mitkLogMacros.h:18
LocaleSwitch(const char *newLocale)
DataCollection - Class to facilitate loading/accessing structured data.