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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
#include <comphelper/locale.hxx>
//_______________________________________________
// includes
#include <rtl/ustrbuf.hxx>
//_______________________________________________
// namespace
namespace comphelper{
//-----------------------------------------------
const sal_Unicode Locale::SEPERATOR_LC = (sal_Unicode)'-';
const sal_Unicode Locale::SEPERATOR_CV = (sal_Unicode)'_';
const sal_Unicode Locale::SEPERATOR_CV_LINUX = (sal_Unicode)'.';
//-----------------------------------------------
const Locale Locale::X_DEFAULT()
{
static Locale aLocale(
::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("x")),
::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("default")));
return aLocale;
}
//-----------------------------------------------
const Locale Locale::EN_US()
{
static Locale aLocale(
::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("en")),
::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("US")));
return aLocale;
}
//-----------------------------------------------
const Locale Locale::X_NOTRANSLATE()
{
static Locale aLocale(
::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("x")),
::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("notranslate")));
return aLocale;
}
//-----------------------------------------------
Locale::Locale(const ::rtl::OUString& sISO)
throw(Locale::MalFormedLocaleException)
{
fromISO(sISO);
}
//-----------------------------------------------
Locale::Locale(const ::rtl::OUString& sLanguage,
const ::rtl::OUString& sCountry ,
const ::rtl::OUString& sVariant )
{
// Use set methods to check values too!
setLanguage(sLanguage);
setCountry (sCountry );
setVariant (sVariant );
}
//-----------------------------------------------
Locale::Locale()
{
// Initialize instance ... otherwhise user will
// may be get exceptions if he e.g. copy this instance ...
(*this) = X_NOTRANSLATE();
}
//-----------------------------------------------
Locale::Locale(const Locale& aCopy)
{
(*this) = aCopy; // recycle assign operator
}
//-----------------------------------------------
::rtl::OUString Locale::getLanguage() const
{
return m_sLanguage;
}
//-----------------------------------------------
::rtl::OUString Locale::getCountry() const
{
return m_sCountry;
}
//-----------------------------------------------
::rtl::OUString Locale::getVariant() const
{
return m_sVariant;
}
//-----------------------------------------------
void Locale::setLanguage(const ::rtl::OUString& sLanguage)
{
m_sLanguage = sLanguage;
}
//-----------------------------------------------
void Locale::setCountry(const ::rtl::OUString& sCountry)
{
m_sCountry = sCountry;
}
//-----------------------------------------------
void Locale::setVariant(const ::rtl::OUString& sVariant)
{
m_sVariant = sVariant;
}
//-----------------------------------------------
/* Attention: Use own interface methods to set the
different parts of this locale. Because the
check the incoming value and throw an exception
automaticly ...
*/
void Locale::fromISO(const ::rtl::OUString& sISO)
throw(Locale::MalFormedLocaleException)
{
m_sLanguage = ::rtl::OUString();
m_sCountry = ::rtl::OUString();
m_sVariant = ::rtl::OUString();
::rtl::OUString sParser(sISO);
sParser.trim();
sal_Int32 nStart = 0;
sal_Int32 nEnd = 0;
// extract language part
nEnd = sParser.indexOf(SEPERATOR_LC, nStart);
if (nEnd<0)
{
setLanguage(sParser);
return;
}
setLanguage(sParser.copy(nStart, nEnd-nStart));
nStart = nEnd+1;
// extract country
nEnd = sParser.indexOf(SEPERATOR_CV, nStart);
if (nEnd<0)
nEnd = sParser.indexOf(SEPERATOR_CV_LINUX, nStart);
if (nEnd<0)
{
setCountry(sParser.copy(nStart, sParser.getLength()-nStart));
return;
}
nStart = nEnd+1;
// extract variant
setVariant(sParser.copy(nStart, sParser.getLength()-nStart));
}
//-----------------------------------------------
::rtl::OUString Locale::toISO() const
{
::rtl::OUStringBuffer sISO(64);
sISO.append(m_sLanguage);
if (m_sCountry.getLength())
{
sISO.append(SEPERATOR_LC);
sISO.append(m_sCountry);
if (m_sVariant.getLength())
{
sISO.append(SEPERATOR_CV);
sISO.append(m_sVariant);
}
}
return sISO.makeStringAndClear();
}
//-----------------------------------------------
sal_Bool Locale::equals(const Locale& aComparable) const
{
return (
m_sLanguage.equals(aComparable.m_sLanguage) &&
m_sCountry.equals (aComparable.m_sCountry ) &&
m_sVariant.equals (aComparable.m_sVariant )
);
}
//-----------------------------------------------
sal_Bool Locale::similar(const Locale& aComparable) const
{
return (m_sLanguage.equals(aComparable.m_sLanguage));
}
//-----------------------------------------------
::std::vector< ::rtl::OUString >::const_iterator Locale::getFallback(const ::std::vector< ::rtl::OUString >& lISOList ,
const ::rtl::OUString& sReferenceISO)
throw(Locale::MalFormedLocaleException)
{
Locale aReference(sReferenceISO);
// Note: The same language or "en"/"en-US" should be preferred as fallback.
// On the other side some localized variables doesnt use localzation in real.
// May be the use a "fix" value only ... marked as X-DEFAULT or X-NOTRANSLATE.
// At least it can be discussed, if any language is a valid fallback ...
// But in case some office functionality depends on that (that means real functionality instead
// of pure UI descriptions) we should do anything, so it can work.
::std::vector< ::rtl::OUString >::const_iterator pSimilar = lISOList.end();
::std::vector< ::rtl::OUString >::const_iterator pEN_US = lISOList.end();
::std::vector< ::rtl::OUString >::const_iterator pEN = lISOList.end();
::std::vector< ::rtl::OUString >::const_iterator pXDefault = lISOList.end();
::std::vector< ::rtl::OUString >::const_iterator pXNoTranslate = lISOList.end();
::std::vector< ::rtl::OUString >::const_iterator pAny = lISOList.end();
::std::vector< ::rtl::OUString >::const_iterator pIt;
for ( pIt = lISOList.begin();
pIt != lISOList.end() ;
++pIt )
{
Locale aCheck(*pIt);
// found Locale, which match with 100% => return it
if (aCheck.equals(aReference))
return pIt;
// found similar Locale => safe it as possible fallback
if (
(pSimilar == lISOList.end()) &&
(aCheck.similar(aReference))
)
{
pSimilar = pIt;
}
else
// found en-US => safe it as fallback
if (
(pEN_US == lISOList.end()) &&
(aCheck.equals(EN_US()) )
)
{
pEN_US = pIt;
}
else
// found en[-XX] => safe it as fallback
if (
(pEN == lISOList.end() ) &&
(aCheck.similar(EN_US()))
)
{
pEN = pIt;
}
else
// found an explicit default value(!) => safe it as fallback
if (
(pXDefault == lISOList.end()) &&
(aCheck.equals(X_DEFAULT()) )
)
{
pXDefault = pIt;
}
else
// found an implicit default value(!) => safe it as fallback
if (
(pXNoTranslate == lISOList.end()) &&
(aCheck.equals(X_NOTRANSLATE()) )
)
{
pXNoTranslate = pIt;
}
else
// safe the first locale, which isn't an explicit fallback
// as "last possible fallback"
if (pAny == lISOList.end())
pAny = pIt;
}
if (pSimilar != lISOList.end())
return pSimilar;
if (pEN_US != lISOList.end())
return pEN_US;
if (pEN != lISOList.end())
return pEN;
if (pXDefault != lISOList.end())
return pXDefault;
if (pXNoTranslate != lISOList.end())
return pXNoTranslate;
if (pAny != lISOList.end())
return pAny;
return lISOList.end();
}
//-----------------------------------------------
void Locale::operator=(const Locale& rCopy)
{
// Take over these values without checking ...
// They was already checked if the copy was constructed
// and must be valid now!
m_sLanguage = rCopy.m_sLanguage;
m_sCountry = rCopy.m_sCountry;
m_sVariant = rCopy.m_sVariant;
}
//-----------------------------------------------
sal_Bool Locale::operator==(const Locale& aComparable) const
{
return equals(aComparable);
}
//-----------------------------------------------
sal_Bool Locale::operator!=(const Locale& aComparable) const
{
return !equals(aComparable);
}
} // namespace comphelper
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|