]> git.saurik.com Git - apt.git/blame - apt-pkg/aptconfiguration.cc
dd support for the LANGUAGE environment variable
[apt.git] / apt-pkg / aptconfiguration.cc
CommitLineData
e878aedb
DK
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3/* ######################################################################
4
5 Provide access methods to various configuration settings,
6 setup defaults and returns validate settings.
7
8 ##################################################################### */
9 /*}}}*/
10// Include Files /*{{{*/
e878aedb
DK
11#include <apt-pkg/aptconfiguration.h>
12#include <apt-pkg/configuration.h>
d7cf5923
DK
13#include <apt-pkg/fileutl.h>
14#include <apt-pkg/macros.h>
15#include <apt-pkg/strutl.h>
e878aedb
DK
16
17#include <vector>
18#include <string>
8bd02d8b 19#include <algorithm>
e878aedb
DK
20 /*}}}*/
21namespace APT {
22// getCompressionTypes - Return Vector of usbale compressiontypes /*{{{*/
23// ---------------------------------------------------------------------
24/* return a vector of compression types in the prefered order. */
25std::vector<std::string>
26const Configuration::getCompressionTypes(bool const &Cached) {
27 static std::vector<std::string> types;
28 if (types.empty() == false) {
29 if (Cached == true)
30 return types;
31 else
32 types.clear();
33 }
34
8bd02d8b
DK
35 // setup the defaults for the compressiontypes => method mapping
36 _config->CndSet("Acquire::CompressionTypes::bz2","bzip2");
37 _config->CndSet("Acquire::CompressionTypes::lzma","lzma");
38 _config->CndSet("Acquire::CompressionTypes::gz","gzip");
39
e878aedb
DK
40 // Set default application paths to check for optional compression types
41 _config->CndSet("Dir::Bin::lzma", "/usr/bin/lzma");
42 _config->CndSet("Dir::Bin::bzip2", "/bin/bzip2");
43
8bd02d8b
DK
44 // accept non-list order as override setting for config settings on commandline
45 std::string const overrideOrder = _config->Find("Acquire::CompressionTypes::Order","");
46 if (overrideOrder.empty() == false)
47 types.push_back(overrideOrder);
e878aedb 48
8bd02d8b
DK
49 // load the order setting into our vector
50 std::vector<std::string> const order = _config->FindVector("Acquire::CompressionTypes::Order");
51 for (std::vector<std::string>::const_iterator o = order.begin();
52 o != order.end(); o++) {
53 if ((*o).empty() == true)
54 continue;
55 // ignore types we have no method ready to use
56 if (_config->Exists(string("Acquire::CompressionTypes::").append(*o)) == false)
57 continue;
58 // ignore types we have no app ready to use
59 string const appsetting = string("Dir::Bin::").append(*o);
60 if (_config->Exists(appsetting) == true) {
61 std::string const app = _config->FindFile(appsetting.c_str(), "");
62 if (app.empty() == false && FileExists(app) == false)
63 continue;
64 }
65 types.push_back(*o);
e878aedb
DK
66 }
67
8bd02d8b 68 // move again over the option tree to add all missing compression types
e878aedb
DK
69 ::Configuration::Item const *Types = _config->Tree("Acquire::CompressionTypes");
70 if (Types != 0)
71 Types = Types->Child;
72
73 for (; Types != 0; Types = Types->Next) {
8bd02d8b
DK
74 if (Types->Tag == "Order" || Types->Tag.empty() == true)
75 continue;
76 // ignore types we already have in the vector
77 if (std::find(types.begin(),types.end(),Types->Tag) != types.end())
78 continue;
79 // ignore types we have no app ready to use
e878aedb 80 string const appsetting = string("Dir::Bin::").append(Types->Value);
e878aedb
DK
81 if (appsetting.empty() == false && _config->Exists(appsetting) == true) {
82 std::string const app = _config->FindFile(appsetting.c_str(), "");
83 if (app.empty() == false && FileExists(app) == false)
84 continue;
85 }
86 types.push_back(Types->Tag);
87 }
88
89 return types;
90}
91 /*}}}*/
45df0ad2
DK
92// GetLanguages - Return Vector of Language Codes /*{{{*/
93// ---------------------------------------------------------------------
94/* return a vector of language codes in the prefered order.
95 the special word "environment" will be replaced with the long and the short
96 code of the local settings and it will be insured that this will not add
97 duplicates. So in an german local the setting "environment, de_DE, en, de"
98 will result in "de_DE, de, en".
99 The special word "none" is the stopcode for the not-All code vector */
100std::vector<std::string> const Configuration::getLanguages(bool const &All,
d7cf5923 101 bool const &Cached, char const ** const Locale) {
45df0ad2
DK
102 using std::string;
103
104 // The detection is boring and has a lot of cornercases,
105 // so we cache the results to calculated it only once.
106 std::vector<string> static allCodes;
107 std::vector<string> static codes;
108
109 // we have something in the cache
110 if (codes.empty() == false || allCodes.empty() == false) {
111 if (Cached == true) {
112 if(All == true && allCodes.empty() == false)
113 return allCodes;
114 else
115 return codes;
116 } else {
117 allCodes.clear();
118 codes.clear();
119 }
120 }
121
d7cf5923 122 // get the environment language codes: LC_MESSAGES (and later LANGUAGE)
45df0ad2
DK
123 // we extract both, a long and a short code and then we will
124 // check if we actually need both (rare) or if the short is enough
d7cf5923 125 string const envMsg = string(Locale == 0 ? std::setlocale(LC_MESSAGES, NULL) : *Locale);
45df0ad2 126 size_t const lenShort = (envMsg.find('_') != string::npos) ? envMsg.find('_') : 2;
d7cf5923 127 size_t const lenLong = (envMsg.find_first_of(".@") != string::npos) ? envMsg.find_first_of(".@") : (lenShort + 3);
45df0ad2
DK
128
129 string envLong = envMsg.substr(0,lenLong);
130 string const envShort = envLong.substr(0,lenShort);
d7cf5923 131 bool envLongIncluded = true;
45df0ad2
DK
132
133 // first cornercase: LANG=C, so we use only "en" Translation
134 if (envLong == "C") {
135 codes.push_back("en");
d7cf5923 136 allCodes = codes;
45df0ad2
DK
137 return codes;
138 }
139
d7cf5923
DK
140 // to save the servers from unneeded queries, we only try also long codes
141 // for languages it is realistic to have a long code translation fileā€¦
142 // TODO: Improve translation acquire system to drop them dynamic
143 char const *needLong[] = { "cs", "en", "pt", "sv", "zh", NULL };
45df0ad2 144 if (envLong != envShort) {
45df0ad2
DK
145 for (char const **l = needLong; *l != NULL; l++)
146 if (envShort.compare(*l) == 0) {
147 envLongIncluded = false;
148 break;
149 }
150 }
151
152 // we don't add the long code, but we allow the user to do so
153 if (envLongIncluded == true)
154 envLong.clear();
155
156 // FIXME: Remove support for the old APT::Acquire::Translation
157 // it was undocumented and so it should be not very widthly used
158 string const oldAcquire = _config->Find("APT::Acquire::Translation","");
159 if (oldAcquire.empty() == false && oldAcquire != "environment") {
160 if (oldAcquire != "none")
161 codes.push_back(oldAcquire);
d7cf5923 162 allCodes = codes;
45df0ad2
DK
163 return codes;
164 }
165
d7cf5923
DK
166 // It is very likely we will need to environment codes later,
167 // so let us generate them now from LC_MESSAGES and LANGUAGE
168 std::vector<string> environment;
169 // take care of LC_MESSAGES
170 if (envLongIncluded == false)
171 environment.push_back(envLong);
172 environment.push_back(envShort);
173 // take care of LANGUAGE
174 string envLang = Locale == 0 ? getenv("LANGUAGE") : *(Locale+1);
175 if (envLang.empty() == false) {
176 std::vector<string> env = ExplodeString(envLang,':');
177 short addedLangs = 0; // add a maximum of 3 fallbacks from the environment
178 for (std::vector<string>::const_iterator e = env.begin();
179 e != env.end() && addedLangs < 3; ++e) {
180 if (unlikely(e->empty() == true) || *e == "en")
181 continue;
182 if (*e == envLong || *e == envShort)
183 continue;
184 if (std::find(environment.begin(), environment.end(), *e) != environment.end())
185 continue;
186 if (e->find('_') != string::npos) {
187 // Drop LongCodes here - ShortCodes are also included
188 string const shorty = e->substr(0, e->find('_'));
189 char const **n = needLong;
190 for (; *n != NULL; ++n)
191 if (shorty == *n)
192 break;
193 if (*n == NULL)
194 continue;
195 }
196 ++addedLangs;
197 environment.push_back(*e);
198 }
199 }
200
45df0ad2
DK
201 // Support settings like Acquire::Translation=none on the command line to
202 // override the configuration settings vector of languages.
203 string const forceLang = _config->Find("Acquire::Languages","");
204 if (forceLang.empty() == false) {
205 if (forceLang == "environment") {
d7cf5923 206 codes = environment;
45df0ad2
DK
207 } else if (forceLang != "none")
208 codes.push_back(forceLang);
d7cf5923 209 allCodes = codes;
45df0ad2
DK
210 return codes;
211 }
212
213 std::vector<string> const lang = _config->FindVector("Acquire::Languages");
214 // the default setting -> "environment, en"
215 if (lang.empty() == true) {
d7cf5923 216 codes = environment;
45df0ad2
DK
217 if (envShort != "en")
218 codes.push_back("en");
d7cf5923 219 allCodes = codes;
45df0ad2
DK
220 return codes;
221 }
222
223 // the configs define the order, so add the environment
224 // then needed and ensure the codes are not listed twice.
225 bool noneSeen = false;
226 for (std::vector<string>::const_iterator l = lang.begin();
227 l != lang.end(); l++) {
228 if (*l == "environment") {
d7cf5923
DK
229 for (std::vector<string>::const_iterator e = environment.begin();
230 e != environment.end(); ++e) {
231 if (std::find(allCodes.begin(), allCodes.end(), *e) != allCodes.end())
232 continue;
45df0ad2 233 if (noneSeen == false)
d7cf5923
DK
234 codes.push_back(*e);
235 allCodes.push_back(*e);
45df0ad2
DK
236 }
237 continue;
238 } else if (*l == "none") {
239 noneSeen = true;
240 continue;
d7cf5923 241 } else if (std::find(allCodes.begin(), allCodes.end(), *l) != allCodes.end())
45df0ad2
DK
242 continue;
243
244 if (noneSeen == false)
245 codes.push_back(*l);
246 allCodes.push_back(*l);
247 }
248 if (All == true)
249 return allCodes;
250 else
251 return codes;
252}
253 /*}}}*/
e878aedb 254}