added LanguageTag::getFallback()

Similar to comphelper::Locale::getFallback() but implemented
differently. comphelper::Locale is to be removed later.

Change-Id: I99dc7b51029df102705f2608245c91d81dc96788
This commit is contained in:
Eike Rathke 2013-03-27 22:18:32 +01:00
parent 97257aeef6
commit a1554c121f
2 changed files with 87 additions and 0 deletions

View File

@ -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

View File

@ -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: */