[Libreoffice-commits] core.git: 2 commits - i18nlangtag/source include/i18nlangtag scripting/Library_stringresource.mk scripting/source

Eike Rathke erack at redhat.com
Thu Jul 11 08:48:37 PDT 2013


 i18nlangtag/source/languagetag/languagetag.cxx     |   45 +++++++++++++++++++++
 include/i18nlangtag/languagetag.hxx                |   31 +++++++++++++-
 scripting/Library_stringresource.mk                |    1 
 scripting/source/stringresource/stringresource.cxx |   31 ++++----------
 4 files changed, 85 insertions(+), 23 deletions(-)

New commits:
commit 38522416bd6a85f206b8c294726dd2e3d6a63567
Author: Eike Rathke <erack at redhat.com>
Date:   Thu Jul 11 17:41:59 2013 +0200

    use LanguageTag::getMatchingFallback()
    
    Change-Id: Ib6fd9581728bdd7c32ccec9ce538d9b4c5658b04

diff --git a/scripting/Library_stringresource.mk b/scripting/Library_stringresource.mk
index bd59637..daa6386 100644
--- a/scripting/Library_stringresource.mk
+++ b/scripting/Library_stringresource.mk
@@ -25,6 +25,7 @@ $(eval $(call gb_Library_use_libraries,stringresource,\
 	cppuhelper \
 	sal \
 	tl \
+	i18nlangtag \
 	$(gb_UWINAPI) \
 ))
 
diff --git a/scripting/source/stringresource/stringresource.cxx b/scripting/source/stringresource/stringresource.cxx
index c5c8b91..7a80699 100644
--- a/scripting/source/stringresource/stringresource.cxx
+++ b/scripting/source/stringresource/stringresource.cxx
@@ -36,6 +36,7 @@
 #include <rtl/ustrbuf.hxx>
 #include <rtl/strbuf.hxx>
 #include <tools/urlobj.hxx>
+#include <i18nlangtag/languagetag.hxx>
 
 using namespace ::com::sun::star;
 using namespace ::com::sun::star::lang;
@@ -679,33 +680,21 @@ LocaleItem* StringResourceImpl::getItemForLocale
     return pRetItem;
 }
 
-// Returns the LocalItem for a given locale, if it exists, otherwise NULL
-// This method performes a closest match search, at least the language must match
+// Returns the LocaleItem for a given locale, if it exists, otherwise NULL.
+// This method performs a closest match search, at least the language must match.
 LocaleItem* StringResourceImpl::getClosestMatchItemForLocale( const Locale& locale )
 {
     LocaleItem* pRetItem = NULL;
 
-    // Search for locale
-    for( sal_Int32 iPass = 0 ; iPass <= 2 ; ++iPass )
+    ::std::vector< Locale > aLocales( m_aLocaleItemVector.size());
+    for( LocaleItemVectorConstIt it = m_aLocaleItemVector.begin(); it != m_aLocaleItemVector.end(); ++it )
     {
-        for( LocaleItemVectorConstIt it = m_aLocaleItemVector.begin(); it != m_aLocaleItemVector.end(); ++it )
-        {
-            LocaleItem* pLocaleItem = *it;
-            if( pLocaleItem )
-            {
-                Locale& cmp_locale = pLocaleItem->m_locale;
-                if( cmp_locale.Language == locale.Language &&
-                    (iPass > 1 || cmp_locale.Country  == locale.Country) &&
-                    (iPass > 0 || cmp_locale.Variant  == locale.Variant) )
-                {
-                    pRetItem = pLocaleItem;
-                    break;
-                }
-            }
-        }
-        if( pRetItem )
-            break;
+        LocaleItem* pLocaleItem = *it;
+        aLocales.push_back( pLocaleItem ? pLocaleItem->m_locale : Locale());
     }
+    ::std::vector< Locale >::const_iterator iFound( LanguageTag::getMatchingFallback( aLocales, locale));
+    if (iFound != aLocales.end())
+        pRetItem = *(m_aLocaleItemVector.begin() + (iFound - aLocales.begin()));
 
     return pRetItem;
 }
commit 00de85cc75e3501a5597c2f293407b1b76fef429
Author: Eike Rathke <erack at redhat.com>
Date:   Thu Jul 11 16:46:20 2013 +0200

    added getMatchingFallback()
    
    Change-Id: I5805ab98049bd7dfd8ca1b58632f6c0115197bdd

diff --git a/i18nlangtag/source/languagetag/languagetag.cxx b/i18nlangtag/source/languagetag/languagetag.cxx
index 13e25f8..4aefa79 100644
--- a/i18nlangtag/source/languagetag/languagetag.cxx
+++ b/i18nlangtag/source/languagetag/languagetag.cxx
@@ -1312,4 +1312,49 @@ LanguageTag::Extraction LanguageTag::simpleExtract( const OUString& rBcp47,
 }
 
 
+// static
+::std::vector< com::sun::star::lang::Locale >::const_iterator LanguageTag::getMatchingFallback(
+        const ::std::vector< com::sun::star::lang::Locale > & rList,
+        const com::sun::star::lang::Locale & rReference )
+{
+    if (rList.empty())
+        return rList.end();
+
+    ::std::vector< lang::Locale >::const_iterator it;
+
+    // Try the simple case first without constructing fallbacks.
+    for (it = rList.begin(); it != rList.end(); ++it)
+    {
+        if (    (*it).Language == rReference.Language &&
+                (*it).Country  == rReference.Country  &&
+                (*it).Variant  == rReference.Variant)
+            return it;  // exact match
+    }
+
+    // Now for each reference fallback test the fallbacks of the list in order.
+    ::std::vector< OUString > aFallbacks( LanguageTag( rReference).getFallbackStrings());
+    aFallbacks.erase( aFallbacks.begin());  // first is full BCP47, we already checked that
+    ::std::vector< ::std::vector< OUString > > aListFallbacks( rList.size());
+    for (it = rList.begin(); it != rList.end(); ++it)
+    {
+        ::std::vector< OUString > aTmp( LanguageTag( *it).getFallbackStrings());
+        aListFallbacks.push_back( aTmp);
+    }
+    for (::std::vector< OUString >::const_iterator rfb( aFallbacks.begin()); rfb != aFallbacks.end(); ++rfb)
+    {
+        for (::std::vector< ::std::vector< OUString > >::const_iterator lfb( aListFallbacks.begin());
+                lfb != aListFallbacks.end(); ++lfb)
+        {
+            for (::std::vector< OUString >::const_iterator fb( (*lfb).begin()); fb != (*lfb).end(); ++fb)
+            {
+                if (*rfb == *fb)
+                    return rList.begin() + (lfb - aListFallbacks.begin());
+            }
+        }
+    }
+
+    // No match found.
+    return rList.end();
+}
+
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/include/i18nlangtag/languagetag.hxx b/include/i18nlangtag/languagetag.hxx
index d779f9d..bbda176 100644
--- a/include/i18nlangtag/languagetag.hxx
+++ b/include/i18nlangtag/languagetag.hxx
@@ -273,7 +273,7 @@ public:
     ::std::vector< OUString >       getFallbackStrings() const;
 
 
-    /** @short  search for an equal or at least for a similar locale in a list
+    /** @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
@@ -306,12 +306,39 @@ public:
 
         @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.
+                the beginning of the list.
      */
     static ::std::vector< OUString >::const_iterator getFallback( const ::std::vector< OUString > & rList,
                                                                   const OUString & rReference );
 
 
+    /** @short  Search for an equal or for a similar locale in a list
+                of possible ones where at least the language matches.
+
+        @descr  First search for a locale that is equal to the reference
+                locale.
+
+                If the reference locale could not be located, check for
+                "similar" locales, in the same order as obtained by
+                getFallbackStrings().
+
+                If no locale matches, rList.end() is returned.
+
+        @param  rList
+                the vector of possible locales.
+
+        @param  rReference
+                the reference locale.
+
+        @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< com::sun::star::lang::Locale >::const_iterator getMatchingFallback(
+            const ::std::vector< com::sun::star::lang::Locale > & rList,
+            const com::sun::star::lang::Locale & rReference );
+
+
     /** Test equality of two LanguageTag, possibly resolving system locale.
 
         @param bResolveSystem


More information about the Libreoffice-commits mailing list