diff options
author | Eike Rathke <erack@redhat.com> | 2013-07-13 00:54:24 +0200 |
---|---|---|
committer | Eike Rathke <erack@redhat.com> | 2013-07-13 11:54:37 +0200 |
commit | 5d1a8e0d47b6d6e9e3e5735d0ee52fa0b4782202 (patch) | |
tree | 4d01c413a5ffeaa7871ac45c8d4985a31a1f07f7 | |
parent | ec07170735331cb64f2e967682ad74def733a931 (diff) |
added static convertTo...() methods
Standalone conversions from Locale and LangID do not need a LanguageTag
instance. Provide simple static methods. For consistency also static
methods to convert from BCP47, but using a temporary instance.
Change-Id: I5edd27e917ef9e92e8ec457e715b7558d7e4660f
-rw-r--r-- | i18nlangtag/source/languagetag/languagetag.cxx | 126 | ||||
-rw-r--r-- | include/i18nlangtag/languagetag.hxx | 68 |
2 files changed, 174 insertions, 20 deletions
diff --git a/i18nlangtag/source/languagetag/languagetag.cxx b/i18nlangtag/source/languagetag/languagetag.cxx index c1319dd70b51..d35f82fe1407 100644 --- a/i18nlangtag/source/languagetag/languagetag.cxx +++ b/i18nlangtag/source/languagetag/languagetag.cxx @@ -616,17 +616,7 @@ void LanguageTag::convertLocaleToBcp47() } else { - /* XXX NOTE: most legacy code never evaluated the Variant field, so for - * now just concatenate language and country. In case we stumbled over - * variant aware code we'd have to take care of that. */ - if (maLocale.Country.isEmpty()) - maBcp47 = maLocale.Language; - else - { - OUStringBuffer aBuf( maLocale.Language.getLength() + 1 + maLocale.Country.getLength()); - aBuf.append( maLocale.Language).append( '-').append( maLocale.Country); - maBcp47 = aBuf.makeStringAndClear(); - } + maBcp47 = convertToBcp47( maLocale, true); } mbInitializedBcp47 = true; } @@ -640,10 +630,7 @@ void LanguageTag::convertLocaleToLang() } else { - /* FIXME: this is temporary until code base is converted to not use - * MsLangId::convert...() anymore. After that, proper new method has to - * be implemented to allow I18NLANGTAG_QLT and sript tag and such. */ - mnLangID = MsLangId::Conversion::convertLocaleToLanguage( maLocale); + mnLangID = convertToLanguageType( maLocale, true); } mbInitializedLangID = true; } @@ -694,11 +681,8 @@ void LanguageTag::convertLangToLocale() mnLangID = MsLangId::getRealLanguage( LANGUAGE_SYSTEM); mbInitializedLangID = true; } - /* FIXME: this is temporary until code base is converted to not use - * MsLangId::convert...() anymore. After that, proper new method has to be - * implemented to allow I18NLANGTAG_QLT and script tag and such. */ - // Resolve system here! - maLocale = MsLangId::Conversion::convertLanguageToLocale( mnLangID, true); + // Resolve system here! The original is remembered as mbSystemLocale. + maLocale = convertToLocale( mnLangID, true); mbInitializedLocale = true; } @@ -1374,4 +1358,106 @@ LanguageTag::Extraction LanguageTag::simpleExtract( const OUString& rBcp47, return rList.end(); } + +static bool lcl_isSystem( LanguageType nLangID ) +{ + if (nLangID == LANGUAGE_SYSTEM) + return true; + // There are some special values that simplify to SYSTEM, + // getRealLanguage() catches and resolves them. + LanguageType nNewLangID = MsLangId::getRealLanguage( nLangID); + if (nNewLangID != nLangID) + return true; + return false; +} + + +// static +com::sun::star::lang::Locale LanguageTag::convertToLocale( LanguageType nLangID, bool bResolveSystem ) +{ + if (!bResolveSystem && lcl_isSystem( nLangID)) + return lang::Locale(); + + /* FIXME: this is temporary until code base is converted to not use + * MsLangId::convert...() anymore. After that, proper new method has to be + * implemented to allow I18NLANGTAG_QLT and script tag and such. */ + return MsLangId::Conversion::convertLanguageToLocale( nLangID, bResolveSystem); +} + + +// static +LanguageType LanguageTag::convertToLanguageType( const com::sun::star::lang::Locale& rLocale, bool bResolveSystem ) +{ + if (rLocale.Language.isEmpty() && !bResolveSystem) + return LANGUAGE_SYSTEM; + + /* FIXME: this is temporary until code base is converted to not use + * MsLangId::convert...() anymore. After that, proper new method has to + * be implemented to allow I18NLANGTAG_QLT and sript tag and such. */ + return MsLangId::Conversion::convertLocaleToLanguage( rLocale); +} + + +// static +OUString LanguageTag::convertToBcp47( const com::sun::star::lang::Locale& rLocale, bool bResolveSystem ) +{ + OUString aBcp47; + if (rLocale.Language.isEmpty()) + { + if (bResolveSystem) + aBcp47 = convertToBcp47( LANGUAGE_SYSTEM, true); + // else aBcp47 stays empty + } + else if (rLocale.Language == I18NLANGTAG_QLT) + { + aBcp47 = rLocale.Variant; + } + else + { + /* XXX NOTE: most legacy code never evaluated the Variant field, so for + * now just concatenate language and country. In case we stumbled over + * variant aware code we'd have to take care of that. */ + if (rLocale.Country.isEmpty()) + aBcp47 = rLocale.Language; + else + { + OUStringBuffer aBuf( rLocale.Language.getLength() + 1 + rLocale.Country.getLength()); + aBuf.append( rLocale.Language).append( '-').append( rLocale.Country); + aBcp47 = aBuf.makeStringAndClear(); + } + } + return aBcp47; +} + + +// static +OUString LanguageTag::convertToBcp47( LanguageType nLangID, bool bResolveSystem ) +{ + // Catch this first so we don't need the rest. + if (!bResolveSystem && lcl_isSystem( nLangID)) + return OUString(); + + lang::Locale aLocale( convertToLocale( nLangID, bResolveSystem)); + // If system for some reason (should not happen.. haha) could not be + // resolved DO NOT CALL convertToBcp47() because that would recurse into + // this method here! + if (aLocale.Language.isEmpty() && bResolveSystem) + return OUString(); // bad luck, bail out + return convertToBcp47( aLocale, bResolveSystem); +} + + +// static +com::sun::star::lang::Locale LanguageTag::convertToLocale( const OUString& rBcp47, bool bResolveSystem ) +{ + return LanguageTag( rBcp47).getLocale( bResolveSystem); +} + + +// static +LanguageType LanguageTag::convertToLanguageType( const OUString& rBcp47, bool bResolveSystem ) +{ + return LanguageTag( rBcp47).getLanguageType( bResolveSystem); +} + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/i18nlangtag/languagetag.hxx b/include/i18nlangtag/languagetag.hxx index bbda176a8f20..1a1d69deaae6 100644 --- a/include/i18nlangtag/languagetag.hxx +++ b/include/i18nlangtag/languagetag.hxx @@ -41,6 +41,9 @@ typedef struct _rtl_Locale rtl_Locale; // as in rtl/locale.h Note that member variables are mutable and may change their values even in const methods. Getter methods return either the original value or matching converted values. + + For standalone conversions if no LanguageTag instance is at hand, static + convertTo...() methods exist. */ class I18NLANGTAG_DLLPUBLIC LanguageTag { @@ -365,6 +368,71 @@ public: */ bool operator!=( const LanguageTag & rLanguageTag ) const; + + /** Convert MS-LangID to Locale. + + @param bResolveSystem + If TRUE, resolve an empty language tag denoting the system + locale to the real locale used. + If FALSE, return an empty Locale for such a tag. + */ + static com::sun::star::lang::Locale convertToLocale( LanguageType nLangID, bool bResolveSystem = true ); + + /** Convert Locale to MS-LangID. + + @param bResolveSystem + If TRUE, resolve an empty language tag denoting the system + locale to the real locale used. + If FALSE, return LANGUAGE_SYSTEM for such a tag. + */ + static LanguageType convertToLanguageType( const com::sun::star::lang::Locale& rLocale, bool bResolveSystem = true ); + + /** Convert MS-LangID to BCP 47 string. + + @param bResolveSystem + If TRUE, resolve an empty language tag denoting the system + locale to the real locale used. + If FALSE, return an empty OUString for such a tag. + */ + static OUString convertToBcp47( LanguageType nLangID, bool bResolveSystem = true ); + + /** Convert Locale to BCP 47 string. + + @param bResolveSystem + If TRUE, resolve an empty language tag denoting the system + locale to the real locale used. + If FALSE, return an empty OUString for such a tag. + */ + static OUString convertToBcp47( const com::sun::star::lang::Locale& rLocale, bool bResolveSystem = true ); + + /** Convert BCP 47 string to Locale, convenience method. + + NOTE: exists only for consistency with the other convertTo...() + methods, internally uses a temporary LanguageTag instance for + conversion so does not save anything compared to + LanguageTag(rBcp47).getLocale(bResolveSystem). + + @param bResolveSystem + If TRUE, resolve an empty language tag denoting the system + locale to the real locale used. + If FALSE, return an empty Locale for such a tag. + */ + static com::sun::star::lang::Locale convertToLocale( const OUString& rBcp47, bool bResolveSystem = true ); + + /** Convert BCP 47 string to MS-LangID, convenience method. + + NOTE: exists only for consistency with the other convertTo...() + methods, internally uses a temporary LanguageTag instance for + conversion so does not save anything compared to + LanguageTag(rBcp47).getLanguageType(bResolveSystem). + + @param bResolveSystem + If TRUE, resolve an empty language tag denoting the system + locale to the real locale used. + If FALSE, return LANGUAGE_SYSTEM for such a tag. + */ + static LanguageType convertToLanguageType( const OUString& rBcp47, bool bResolveSystem = true ); + private: enum Decision |