- Add Locale:: bestMatch to find the best match within a set of available Locales.
authorMichael Andres <ma@suse.de>
Wed, 22 Oct 2008 11:03:58 +0000 (11:03 +0000)
committerMichael Andres <ma@suse.de>
Wed, 22 Oct 2008 11:03:58 +0000 (11:03 +0000)
zypp/Locale.cc
zypp/Locale.h

index e9927bf3adc4f8769e0afd139e477dd6a9c5ad90..ac7e5f368c303ff02a83f166f6f78b08fdedd6a1 100644 (file)
@@ -221,6 +221,21 @@ namespace zypp
   //
   Locale Locale::fallback() const
   { return _pimpl->fallback(); }
+
+  Locale Locale::bestMatch( const LocaleSet & avLocales_r, const Locale & requested_r )
+  {
+    if ( ! avLocales_r.empty() )
+    {
+      for ( Locale check( requested_r ); check != noCode; check = check.fallback() )
+      {
+        if ( avLocales_r.find( check ) != avLocales_r.end() )
+          return check;
+      }
+    }
+    return noCode;
+  }
+
+
   /////////////////////////////////////////////////////////////////
 } // namespace zypp
 ///////////////////////////////////////////////////////////////////
index d75ffd8597164b4ee34ae1fc9c32ac78c6c32c2f..e97ba8aa67dddb292c16205f3f172b929ceb0e28 100644 (file)
@@ -25,6 +25,9 @@
 namespace zypp
 { /////////////////////////////////////////////////////////////////
 
+  class Locale;
+  typedef std::tr1::unordered_set<Locale> LocaleSet;
+
   ///////////////////////////////////////////////////////////////////
   //
   //   CLASS NAME : Locale
@@ -83,6 +86,15 @@ namespace zypp
     /** Return a fallback locale for this locale, when giving up, returns empty Locale() */
     Locale fallback() const;
 
+  public:
+
+    /** Return the best match for \ref Locale \c requested_r within the available \c avLocales_r.
+     *
+     * If neither \c requested_r nor any of it's \ref fallback locales are available,
+     * \ref Locale::noCode is returned.
+    */
+    static Locale bestMatch( const LocaleSet & avLocales_r, const Locale & requested_r );
+
   private:
     /** Pointer to implementation */
     RW_pointer<Impl> _pimpl;
@@ -122,10 +134,6 @@ namespace zypp
   }
   //@}
 
-  ///////////////////////////////////////////////////////////////////
-
-  typedef std::tr1::unordered_set<Locale> LocaleSet;
-
   /////////////////////////////////////////////////////////////////
 } // namespace zypp
 ///////////////////////////////////////////////////////////////////