1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
/***************************************************************************
kfindlanguage.cpp - description
-------------------
begin : Tue May 22 2001
copyright : (C) 2002 by Carsten Wolff
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <stdlib.h>
#include <tqstring.h>
#include <tqstringlist.h>
#include <tqmap.h>
#include <ksimpleconfig.h>
#include <kglobal.h>
#include <kstandarddirs.h>
#include <klocale.h>
#include "kfindlanguage.h"
KFindLanguage::KFindLanguage() {
KConfig *config = KGlobal::config();
config->setGroup("Locale");
m_oldlang = config->readEntry("Language");
m_oldlang = m_oldlang.lower();
m_oldlang = m_oldlang.left(m_oldlang.tqfind(':')); // only use the first lang
m_country = config->readEntry("Country", "C");
if (m_country == "C") {
m_country = TQString::tqfromLatin1(getenv("LANG"));
if(m_country.left(5) == "nn_NO") // glibc's nn_NO is KDE's no_NY
m_country = "no";
if(m_country.tqcontains("_"))
m_country = m_country.mid(m_country.tqfind("_")+1);
if(m_country.tqcontains("."))
m_country = m_country.left(m_country.tqfind("."));
if(m_country.tqcontains("@"))
m_country = m_country.left(m_country.tqfind("@"));
if(m_country != "C")
m_country = m_country.lower();
if(m_country == "en") // special-case "en" - should be "en_GB" or "en_US", but plain "en" is in use quite often
m_country = "C";
}
// get the users primary Languages
KSimpleConfig ent(locate("locale", TQString::tqfromLatin1("l10n/%1/entry.desktop").arg(m_country)), true);
ent.setGroup("KCM Locale");
TQStringList langs = ent.readListEntry("Languages");
if (langs.isEmpty())
langs.append("en_US");
// add the primary languages for the country to the list
TQStringList prilang;
for ( TQStringList::ConstIterator it = langs.begin(); it != langs.end(); ++it ) {
TQString str = locate("locale", *it + "/entry.desktop");
if (!str.isNull())
prilang << str;
}
// add all languages to the list
TQStringList alllang = KGlobal::dirs()->findAllResources("locale", "*/entry.desktop", false, true);
alllang.sort();
TQStringList langlist = prilang;
if (langlist.count() > 0)
langlist << TQString::null; // separator
langlist += alllang;
for ( TQStringList::ConstIterator it = langlist.begin(); it != langlist.end(); ++it ) {
KSimpleConfig entry(*it);
entry.setGroup("KCM Locale");
TQString name = entry.readEntry("Name", i18n("without name"));
TQString tag = *it;
int index = tag.tqfindRev('/');
tag = tag.left(index);
index = tag.tqfindRev('/');
tag = tag.mid(index+1);
m_langlist << tag;
m_langmap.insert(tag, name);
}
// now find the best language for the user
TQString compare = m_oldlang;
if (m_oldlang.isEmpty()) {
compare = langs.first();
for(TQStringList::Iterator it = langs.begin(); it != langs.end(); ++it) {
if (*it == TQString::tqfromLatin1(getenv("LANG")).mid(3, 2).lower())
compare = *it;
}
}
if(compare == "c")
compare = "C";
// Find the users's language
int bestmatch = -1;
TQStringList::ConstIterator it;
for( it = m_langlist.begin(); it != m_langlist.end(); ++it) {
int match=0;
TQString l = (*it).left((*it).tqfind(";"));
if (l == "C")
match++;
if(l.tqcontains(compare))
match+=2;
if(l.left(compare.length()) == compare)
match+=10;
if(compare == "en_US" && l == "C")
match+=50;
if (l == compare)
match+=100;
if(match > bestmatch) {
bestmatch=match;
m_bestlang=l;
}
}
}
KFindLanguage::~KFindLanguage() {
}
TQStringList KFindLanguage::getLangList() const {
return m_langlist;
}
TQMap<TQString,TQString> KFindLanguage::getLangMap() const {
return m_langmap;
}
TQString KFindLanguage::getBestLang() const {
return m_bestlang;
}
TQString KFindLanguage::getOldLang() const {
return m_oldlang;
}
TQString KFindLanguage::getCountry() const {
return m_country;
}
|