diff options
author | Kurt Zenker <kz@openoffice.org> | 2004-06-10 14:58:51 +0000 |
---|---|---|
committer | Kurt Zenker <kz@openoffice.org> | 2004-06-10 14:58:51 +0000 |
commit | e83777e31f7a00e1b508d2601bdd28ed885d02d3 (patch) | |
tree | 952481577541076516106b9e93ecd3634c9a3809 /comphelper/source/misc/locale.cxx | |
parent | 31bbfca8e94d3e0bdbedc4790560d079d06b96e3 (diff) |
INTEGRATION: CWS fwk02ea (1.1.2); FILE ADDED
2004/05/27 12:09:20 as 1.1.2.3: #115945# syntax
2004/05/25 09:08:29 as 1.1.2.2: #115945# handle x_default as possible locale fallback too
2004/05/13 09:45:26 as 1.1.2.1: #115945# new locale class with support of fallback mechanism
Diffstat (limited to 'comphelper/source/misc/locale.cxx')
-rw-r--r-- | comphelper/source/misc/locale.cxx | 615 |
1 files changed, 615 insertions, 0 deletions
diff --git a/comphelper/source/misc/locale.cxx b/comphelper/source/misc/locale.cxx new file mode 100644 index 000000000000..0b0313678c75 --- /dev/null +++ b/comphelper/source/misc/locale.cxx @@ -0,0 +1,615 @@ +/************************************************************************* + * + * $RCSfile: locale.cxx,v $ + * + * $Revision: 1.2 $ + * + * last change: $Author: kz $ $Date: 2004-06-10 15:58:51 $ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library 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 for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (the "License"); You may not use this file + * except in compliance with the License. You may obtain a copy of the + * License at http://www.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2000 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ + +#ifndef _COMPHELPER_LOCALE_HXX_ +#include <comphelper/locale.hxx> +#endif + +//_______________________________________________ +// includes + +#ifndef _RTL_USTRBUF_HXX_ +#include <rtl/ustrbuf.hxx> +#endif + +//_______________________________________________ +// 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::createFromAscii("x"), + ::rtl::OUString::createFromAscii("default")); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::EN_US() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("en"), + ::rtl::OUString::createFromAscii("US")); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::DE_DE() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("de"), + ::rtl::OUString::createFromAscii("DE")); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::DE_CH() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("de"), + ::rtl::OUString::createFromAscii("CH")); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::DE_AT() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("de"), + ::rtl::OUString::createFromAscii("AT")); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::AR() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("ar"), + ::rtl::OUString()); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::CA() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("ca"), + ::rtl::OUString()); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::CS() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("cs"), + ::rtl::OUString()); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::DA() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("da"), + ::rtl::OUString()); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::EL() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("el"), + ::rtl::OUString()); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::ES() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("es"), + ::rtl::OUString()); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::FI() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("fi"), + ::rtl::OUString()); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::FR() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("fr"), + ::rtl::OUString()); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::HE() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("he"), + ::rtl::OUString()); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::HI_IN() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("hi"), + ::rtl::OUString::createFromAscii("IN")); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::HU() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("hu"), + ::rtl::OUString()); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::IT() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("it"), + ::rtl::OUString()); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::JA() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("ja"), + ::rtl::OUString()); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::KO() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("ko"), + ::rtl::OUString()); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::NL() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("nl"), + ::rtl::OUString()); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::PL() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("pl"), + ::rtl::OUString()); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::PT() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("pt"), + ::rtl::OUString()); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::PT_BR() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("pt"), + ::rtl::OUString::createFromAscii("BR")); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::RU() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("ru"), + ::rtl::OUString()); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::SK() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("sk"), + ::rtl::OUString()); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::SL() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("sl"), + ::rtl::OUString()); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::SV() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("sv"), + ::rtl::OUString()); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::TH() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("th"), + ::rtl::OUString()); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::TR() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("tr"), + ::rtl::OUString()); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::X_COMMENT() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("x"), + ::rtl::OUString::createFromAscii("comment")); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::X_TRANSLATE() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("x"), + ::rtl::OUString::createFromAscii("translate")); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::ZH_CN() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("zh"), + ::rtl::OUString::createFromAscii("CN")); + return aLocale; +} + +//----------------------------------------------- +const Locale Locale::ZH_TW() +{ + static Locale aLocale( + ::rtl::OUString::createFromAscii("zh"), + ::rtl::OUString::createFromAscii("TW")); + 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(const Locale& aCopy) +{ + // Take over these values without checking ... + // They was already checked if the copy was constructed + // and must be valid now! + m_sLanguage = aCopy.m_sLanguage; + m_sCountry = aCopy.m_sCountry; + m_sVariant = aCopy.m_sVariant; +} + +//----------------------------------------------- +::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: Only the same language or "en"/"en-US" can be used as fallback ... + // Other languages are mostly not understandable for a normal user! + ::std::vector< ::rtl::OUString >::const_iterator pSimilar = lISOList.end(); + ::std::vector< ::rtl::OUString >::const_iterator pDefault = 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 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 an explicit default value(!) => safe it as fallback + if ( + (pDefault == lISOList.end()) && + (aCheck.equals(X_DEFAULT())) + ) + { + pDefault = 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; + } + } + + if (pSimilar != lISOList.end()) + return pSimilar; + + if (pDefault != lISOList.end()) + return pDefault; + + if (pEN_US != lISOList.end()) + return pEN_US; + + if (pEN != lISOList.end()) + return pEN; + + return lISOList.end(); +} + +//----------------------------------------------- +sal_Bool Locale::operator==(const Locale& aComparable) const +{ + return equals(aComparable); +} + +//----------------------------------------------- +sal_Bool Locale::operator!=(const Locale& aComparable) const +{ + return !equals(aComparable); +} + +} // namespace comphelper + |