ManaPlus
langs.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2011-2019 The ManaPlus Developers
4  * Copyright (C) 2019-2021 Andrei Karas
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "utils/langs.h"
23 
24 #include "configuration.h"
25 
26 #ifndef DYECMD
27 #include "being/playerinfo.h"
28 
30 #endif // DYECMD
31 
32 #include "debug.h"
33 
34 static const char *getLangName()
35 {
36  const char *const lang = getenv("LANG");
37  if ((lang != nullptr) && strlen(lang) > 1000)
38  return nullptr;
39  return lang;
40 }
41 
43 {
44  LangVect langs;
45  std::string lang = config.getStringValue("lang");
46  if (lang.empty())
47  {
48  const char *const lng = getLangName();
49  if (lng == nullptr)
50  return langs;
51  lang = lng;
52  }
53  else if (lang.size() > 1000)
54  {
55  return langs;
56  }
57 
58  size_t dot = lang.find('.');
59  if (dot != std::string::npos)
60  lang = lang.substr(0, dot);
61  langs.push_back(lang);
62  dot = lang.find('_');
63  if (dot != std::string::npos)
64  langs.push_back(lang.substr(0, dot));
65  return langs;
66 }
67 
69 {
70  LangVect langs;
71 #ifndef DYECMD
72  const int id = PlayerInfo::getServerLanguage();
73  const std::string &lang = LanguageDb::getPo(id);
74  if (lang.empty())
75  return langs;
76  langs.push_back(lang);
77  const size_t idx = lang.find('_');
78  if (idx != std::string::npos)
79  langs.push_back(lang.substr(0, idx));
80 #endif // DYECMD
81 
82  return langs;
83 }
84 
85 std::string getLangSimple()
86 {
87  const std::string lang = config.getStringValue("lang");
88  if (lang.empty())
89  {
90  const char *const lng = getLangName();
91  if (lng == nullptr)
92  return "";
93  return lng;
94  }
95  else if (lang.size() > 1000)
96  {
97  return "";
98  }
99  return lang;
100 }
101 
102 std::string getLangShort()
103 {
104  std::string lang = config.getStringValue("lang");
105  if (lang.empty())
106  {
107  const char *const lng = getLangName();
108  if (lng == nullptr)
109  return "";
110  lang = lng;
111  }
112  else if (lang.size() > 1000)
113  {
114  return "";
115  }
116 
117  size_t dot = lang.find('.');
118  if (dot != std::string::npos)
119  lang = lang.substr(0, dot);
120  dot = lang.find('_');
121  if (dot != std::string::npos)
122  return lang.substr(0, dot);
123  return lang;
124 }
std::string getStringValue(const std::string &key) const
Configuration config
std::string getLangShort()
Definition: langs.cpp:102
static const char * getLangName()
Definition: langs.cpp:34
LangVect getServerLang()
Definition: langs.cpp:68
LangVect getLang()
Definition: langs.cpp:42
std::string getLangSimple()
Definition: langs.cpp:85
std::vector< std::string > LangVect
Definition: langs.h:31
const std::string & getPo(const int id)
Definition: languagedb.cpp:119
int getServerLanguage()
Definition: playerinfo.cpp:677