Example #1
0
void Trans_SetLanguage( const char* lang )
{
	Language requestLang = Language::from_env( std::string( lang ) );

	// default to english
	Language bestLang = Language::from_env( "en" );
	int bestScore = Language::match( requestLang, bestLang );

	std::set<Language> langs = trans_manager.get_languages();

	for( std::set<Language>::iterator i = langs.begin(); i != langs.end(); i++ )
	{
		int score = Language::match( requestLang, *i );

		if( score > bestScore )
		{
			bestScore = score;
			bestLang = *i;
		}
	}

	// language not found, display warning
	if( bestScore == 0 )
	{
		Com_Printf( S_WARNING "Language \"%s\" (%s) not found. Default to \"English\" (en)\n",
					requestLang.get_name().empty() ? "Unknown Language" : requestLang.get_name().c_str(),
					lang );
	}

	trans_manager.set_language( bestLang );
	trans_managergame.set_language( bestLang );

	Com_Printf( _( "Set language to %s\n" ), bestLang.get_name().c_str() );
}
int main(int argc, char** argv)
{
  try
  {
    if (argc == 3 && strcmp(argv[1], "language-dir") == 0)
    {
      DictionaryManager dictionary_manager;
      dictionary_manager.add_directory(argv[2]);
      const std::set<Language>& languages = dictionary_manager.get_languages();
      std::cout << "Number of languages: " << languages.size() << std::endl;
      for (std::set<Language>::const_iterator i = languages.begin(); i != languages.end(); ++i)
      {
        const Language& language = *i;
        std::cout << "Env:       " << language.str()           << std::endl
                  << "Name:      " << language.get_name()      << std::endl
                  << "Language:  " << language.get_language()  << std::endl
                  << "Country:   " << language.get_country()   << std::endl
                  << "Modifier:  " << language.get_modifier()  << std::endl
                  << std::endl;
      }
    }
    else if (argc == 3 && strcmp(argv[1], "language") == 0)
    {
      const char* language_cstr = argv[2];
      Language language = Language::from_name(language_cstr);

      if (language)
        std::cout << "Env:       " << language.str()           << std::endl
                  << "Name:      " << language.get_name()      << std::endl
                  << "Language:  " << language.get_language()  << std::endl
                  << "Country:   " << language.get_country()   << std::endl
                  << "Modifier:  " << language.get_modifier()  << std::endl;
      else
        std::cout << "not found" << std::endl;
    }
    else if (argc == 4 && strcmp(argv[1], "translate") == 0)
    {
      const char* filename = argv[2];
      const char* message  = argv[3];

      Dictionary dict;
      read_dictionary(filename, dict);
      std::cout << "TRANSLATION: \"\"\"" << dict.translate(message) << "\"\"\""<< std::endl;
    }
    else if (argc == 5 && strcmp(argv[1], "translate") == 0)
    {
      const char* filename = argv[2];
      const char* context  = argv[3];
      const char* message  = argv[4];

      Dictionary dict;
      read_dictionary(filename, dict);
      std::cout << dict.translate_ctxt(context, message) << std::endl;
    }
    else if (argc == 6 && strcmp(argv[1], "translate") == 0)
    {
      const char* filename = argv[2];
      const char* message_singular = argv[3];
      const char* message_plural   = argv[4];
      int num = atoi(argv[5]);

      Dictionary dict;
      read_dictionary(filename, dict);
      std::cout << dict.translate_plural(message_singular, message_plural, num) << std::endl;
    }
    else if (argc == 7 && strcmp(argv[1], "translate") == 0)
    {
      const char* filename = argv[2];
      const char* context  = argv[3];
      const char* message_singular = argv[4];
      const char* message_plural   = argv[5];
      int num = atoi(argv[6]);

      Dictionary dict;
      read_dictionary(filename, dict);
      std::cout << dict.translate_ctxt_plural(context, message_singular, message_plural, num) << std::endl;
    }
    else if ((argc == 4 || argc == 5) && strcmp(argv[1], "directory") == 0)
    {
      const char* directory = argv[2];
      const char* message   = argv[3];
      const char* language  = (argc == 5) ? argv[4] : NULL;

      DictionaryManager manager;
      manager.add_directory(directory);

      if (language)
      {
        Language lang = Language::from_name(language);
        if (lang)
        {
          manager.set_language(lang);
        }
        else
        {
          std::cout << "Unknown language: " << language << std::endl;
          exit(EXIT_FAILURE);
        }
      }

      std::cout << "Directory:   '" << directory << "'"  << std::endl;
      std::cout << "Message:     '" << message << "'" << std::endl;
      std::cout << "Language:    '" << manager.get_language().str() << "' (name: '"
                << manager.get_language().get_name() << "', language: '"
                << manager.get_language().get_language() << "', country: '"
                << manager.get_language().get_country() << "', modifier: '"
                << manager.get_language().get_modifier() << "')"
                << std::endl;
      std::cout << "Translation: '" << manager.get_dictionary().translate(message) << "'" << std::endl;
    }
    else if ((argc == 3) && strcmp(argv[1], "list-msgstrs") == 0)
    {
      const char* filename = argv[2];

      Dictionary dict;
      read_dictionary(filename, dict);
      dict.foreach(print_msg);
      dict.foreach_ctxt(print_msg_ctxt);
    }
    else
    {
      print_usage(argc, argv);
    }
  }
  catch(std::exception& err)
  {
    std::cout << "Exception: " << err.what() << std::endl;
  }

  return 0;
}