diff options
author | Eike Rathke <erack@redhat.com> | 2013-03-27 22:18:32 +0100 |
---|---|---|
committer | Eike Rathke <erack@redhat.com> | 2013-03-27 22:20:05 +0100 |
commit | a1554c121f5f810c77b70ece3d11522a48091d0f (patch) | |
tree | c60728d36740c89d539752f0ae386afea96494a9 /i18npool | |
parent | 97257aeef6c402b34e316eeccd9b022940a805cd (diff) |
added LanguageTag::getFallback()
Similar to comphelper::Locale::getFallback() but implemented
differently. comphelper::Locale is to be removed later.
Change-Id: I99dc7b51029df102705f2608245c91d81dc96788
Diffstat (limited to 'i18npool')
-rw-r--r-- | i18npool/inc/i18npool/languagetag.hxx | 40 | ||||
-rw-r--r-- | i18npool/source/languagetag/languagetag.cxx | 47 |
2 files changed, 87 insertions, 0 deletions
diff --git a/i18npool/inc/i18npool/languagetag.hxx b/i18npool/inc/i18npool/languagetag.hxx index 4d99e72d193f..eb1a51862a93 100644 --- a/i18npool/inc/i18npool/languagetag.hxx +++ b/i18npool/inc/i18npool/languagetag.hxx @@ -236,6 +236,46 @@ public: */ ::std::vector< OUString > getFallbackStrings() const; + + /** @short search for an equal or at least for a similar locale in a list + of possible ones. + + @descr First search for a locale that is equal to the reference + locale. (means: same BCP47 string) + + If the reference locale could not be located, check for + "similar" locales, in the same order as obtained by + getFallbackStrings(). + + If no similar locale could be located, we search for a locale + "en-US" inside the given locale list. + + If "en-US" could not be located, we search for a locale "en" + inside the given list. + + If no "same" nor any "similar" locale could be found, we try + "x-default" and "x-no-translate" explicitly. Sometimes + variables don't use real localization. For example, in case the + localized value is a fix product name. + + If no locale matched until then, we use any other locale that + exists inside the set of given ones, namely the first + encountered! + + @param rList + the vector of possible locales as BCP47 strings. + + @param rReference + the reference locale, BCP47 string. + + @return An iterator that points to the found element inside the given + locale list. If no matching locale could be found it points to + the end of the list. + */ + static ::std::vector< OUString >::const_iterator getFallback( const ::std::vector< OUString > & rList, + const OUString & rReference ); + + /** Test equality of two LanguageTag, possibly resolving system locale. @param bResolveSystem diff --git a/i18npool/source/languagetag/languagetag.cxx b/i18npool/source/languagetag/languagetag.cxx index 7b284b5a4a8c..be6e228f6ecf 100644 --- a/i18npool/source/languagetag/languagetag.cxx +++ b/i18npool/source/languagetag/languagetag.cxx @@ -1159,4 +1159,51 @@ LanguageTag::Extraction LanguageTag::simpleExtract( const OUString& rBcp47, } +// static +::std::vector< OUString >::const_iterator LanguageTag::getFallback( + const ::std::vector< OUString > & rList, const OUString & rReference ) +{ + if (rList.empty()) + return rList.end(); + + ::std::vector< OUString >::const_iterator it; + + // Try the simple case first without constructing fallbacks. + for (it = rList.begin(); it != rList.end(); ++it) + { + if (*it == rReference) + return it; // exact match + } + + ::std::vector< OUString > aFallbacks( LanguageTag( rReference).getFallbackStrings()); + aFallbacks.erase( aFallbacks.begin()); // first is full BCP47, we already checked that + if (rReference != "en-US") + aFallbacks.push_back( "en-US"); + if (rReference != "en") + aFallbacks.push_back( "en"); + if (rReference != "x-default") + aFallbacks.push_back( "x-default"); + if (rReference != "x-no-translate") + aFallbacks.push_back( "x-no-translate"); + /* TODO: the original comphelper::Locale::getFallback() code had + * "x-notranslate" instead of "x-no-translate", but all .xcu files use + * "x-no-translate" and "x-notranslate" apparently was never used anywhere. + * Did that ever work? Was it supposed to work at all like this? */ + + for (::std::vector< OUString >::const_iterator fb = aFallbacks.begin(); fb != aFallbacks.end(); ++fb) + { + for (it = rList.begin(); it != rList.end(); ++it) + { + if (*it == *fb) + return it; // fallback found + } + } + + // Did not find anything so return something of the list, the first value + // will do as well as any other as none did match any of the possible + // fallbacks. + return rList.begin(); +} + + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |