[Supertux-Commit] [tinygettext commit] r138 - in branches/tinygettext-portable: . src

codesite-noreply at google.com codesite-noreply at google.com
Sun Feb 1 10:10:55 PST 2009


Author: grumbel
Date: Sun Feb  1 09:28:51 2009
New Revision: 138

Modified:
    branches/tinygettext-portable/TODO
    branches/tinygettext-portable/src/dictionary_manager.cpp
    branches/tinygettext-portable/src/language.cpp

Log:
Added 'proper' matching of languages

Modified: branches/tinygettext-portable/TODO
==============================================================================
--- branches/tinygettext-portable/TODO	(original)
+++ branches/tinygettext-portable/TODO	Sun Feb  1 09:28:51 2009
@@ -24,12 +24,12 @@
  tinygettext API related stuff:
  ==============================

-* translate, translate_ctxt, translate_ctxt_plural, ... could be
-  unified via overloading, not sure if that is a good idea
-
-* enumerating files should fallback to more generic languages, i.e.:
+* Language matching needs a little review/cleanup

-  de_AT should load de.po (somewhat already the case, but different  
mechanic then supertux)
+* translate, translate_ctxt, translate_ctxt_plural, ... could be
+  unified via overloading, not sure if that is a good idea. For the
+  same reason add_translation() could be de-overloaded, to
+  add_translation_ctxt, ...

  * iconv handling needs cleanup, since some systems don't provide
    iconv, but SDL does and is used by many games, currently HAVE_SDL
@@ -49,6 +49,9 @@

  tinygettext implementation details:
  ===================================
+
+* with PluralForms moved into Dictionary a lot of the Langugae stuff
+  is pointless

  * get rid of goto


Modified: branches/tinygettext-portable/src/dictionary_manager.cpp
==============================================================================
--- branches/tinygettext-portable/src/dictionary_manager.cpp	(original)
+++ branches/tinygettext-portable/src/dictionary_manager.cpp	Sun Feb  1  
09:28:51 2009
@@ -17,6 +17,7 @@
  //  along with this program; if not, write to the Free Software
  //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,  
USA.

+#include <assert.h>
  #include <stdlib.h>
  #include <string.h>
  #include <fstream>
@@ -93,6 +94,7 @@
  {
    //log_debug << "Dictionary for language \"" << spec << "\" requested" <<  
std::endl;
    //log_debug << "...normalized as \"" << lang << "\"" << std::endl;
+  assert(language);

    Dictionaries::iterator i = dictionaries.find(language);
    if (i != dictionaries.end())
@@ -115,30 +117,65 @@
              }
            else
              {
+              const char* best_filename = 0;
+              int best_score = -1;
                for(const char* const* filename = files; *filename != 0;  
filename++)
                  {
                    // check if filename matches requested language
                    if (has_suffix(*filename, ".po"))
                      { // ignore anything that isn't a .po file
                        Language po_language =  
Language::from_env(std::string(*filename, strlen(*filename)-3));
-
-                      // FIXME: Should compare all things and find the  
best match
-                      if (language.get_language() ==  
po_language.get_language())
+
+                      if (!po_language)
                          {
-                          //log_debug << "Loading dictionary for language  
\"" << lang << "\" from \"" << filename << "\"" << std::endl;
-                          std::string pofile = *p + "/" + *filename;
-                          try
-                            {
-                              std::istream* in =  
dir_op.open_file(pofile.c_str());
-                              POParser::parse(pofile, *in, *dict);
-                              delete in;
-                            }
-                          catch(std::exception& e)
+                          log_warning << *filename << ": warning:  
ignoring, unknown language" << std::endl;
+                        }
+                      else
+                        {
+                          if (language.get_language() ==  
po_language.get_language())
                              {
-                              log_error << "error: failure file opening: "  
<< pofile << std::endl;
-                              log_error << e.what() << "" << std::endl;
+                              int score = 0;
+
+                              if (language.get_country().empty() ||  
po_language.get_country().empty())
+                                score += 1;
+
+                              score += 2*(language.get_country() ==  
po_language.get_country());
+
+                              if (language.get_modifier().empty() ||  
po_language.get_modifier().empty())
+                                score += 1;
+
+                              score += 1*(language.get_modifier() ==  
po_language.get_modifier());
+
+                              if (score > best_score)
+                                {
+                                  best_score = score;
+                                  best_filename = *filename;
+                                }
                              }
                          }
+                    }
+                }
+
+              if (best_filename)
+                {
+                  std::string pofile = *p + "/" + best_filename;
+                  try
+                    {
+                      std::istream* in = dir_op.open_file(pofile.c_str());
+                      if (!*in)
+                        {
+                          log_error << "error: failure opening: " <<  
pofile << std::endl;
+                        }
+                      else
+                        {
+                          POParser::parse(pofile, *in, *dict);
+                        }
+                      delete in;
+                    }
+                  catch(std::exception& e)
+                    {
+                      log_error << "error: failure parsing: " << pofile <<  
std::endl;
+                      log_error << e.what() << "" << std::endl;
                      }
                  }
                dir_op.free_list(files);

Modified: branches/tinygettext-portable/src/language.cpp
==============================================================================
--- branches/tinygettext-portable/src/language.cpp	(original)
+++ branches/tinygettext-portable/src/language.cpp	Sun Feb  1 09:28:51 2009
@@ -433,8 +433,27 @@
    std::map<std::string, std::vector<LanguageSpec*> >::iterator i =  
language_map.find(language);
    if (i != language_map.end())
      {
-      // FIXME: Not handling country and modifier here
-      return Language(i->second[0]);
+      std::vector<LanguageSpec*>& lst = i->second;
+
+      LanguageSpec* best_match = i->second[0];
+      int best_match_score = 0;
+      for(std::vector<LanguageSpec*>::iterator j = lst.begin()+1; j !=  
lst.end(); ++j)
+        { // Search for the language that best matches the given spec,  
value country more then modifier
+          int score = 0;
+          if ((*j)->country)
+            score += 2*(country == (*j)->country);
+
+          if ((*j)->modifier)
+            score += 1*(modifier == (*j)->modifier);
+
+          if (score > best_match_score)
+            {
+              best_match = *j;
+              best_match_score = score;
+            }
+        }
+
+      return Language(best_match);
      }
    else
      {


More information about the Supertux-Commit mailing list