Author Topic: cfg->Read and cfg->Write problems with UNICODE  (Read 5022 times)

Offline heromyth

  • Multiple posting newcomer
  • *
  • Posts: 98
    • http://
cfg->Read and cfg->Write problems with UNICODE
« on: February 09, 2006, 03:15:07 pm »
I have reported it at http://sourceforge.net/tracker/index.php?func=detail&aid=1428349&group_id=126998&atid=707416

In src\app.cpp, I have changed the code of the function InitLocale() to this:

Quote
void CodeBlocksApp::InitLocale()
{
    ConfigManager* cfg = Manager::Get()->GetConfigManager(_T("app"));
    const wxString langs[] =
    {
        _T("(System default)")
//        ,_T("English (U.S.)")
//        ,_T("English")
        ,_T("Chinese (Simplified)")
//        ,_T("German")
//        ,_T("Russian")
    };

    // Must have the same order than the above
    const long int locales[] =
    {
        wxLANGUAGE_DEFAULT
//        ,wxLANGUAGE_ENGLISH_US
//        ,wxLANGUAGE_ENGLISH
        ,wxLANGUAGE_CHINESE_SIMPLIFIED
//        ,wxLANGUAGE_GERMAN
//        ,wxLANGUAGE_RUSSIAN
    };

    long int lng = cfg->ReadInt(_T("/locale/language"),(long int)-2);

    //wxMessageBox(wxString::Format(" %d ",(int)lng));
   if (WXSIZEOF(langs)>=2 ) // ask only if undefined and there are at least 2 choices
    {
        lng = wxGetSingleChoiceIndex(_T("Please choose language:"), _T("Language"), WXSIZEOF(langs), langs);
        if (lng >= 0 && static_cast<unsigned int>(lng) < WXSIZEOF(locales))
        {
            lng = locales[lng];
        }
    }
//    else
//    {
//        lng = -1; // -1 = Don't use locale
//    }

    if (lng>=0)
    {
        m_locale.Init(lng);
        wxLocale::AddCatalogLookupPathPrefix(ConfigManager::GetDataFolder() + _T("/locale"));
        wxLocale::AddCatalogLookupPathPrefix(wxT("."));
        wxLocale::AddCatalogLookupPathPrefix(wxT(".."));
      int catalogNum = cfg->ReadInt(_T("/locale/catalogNum"), 0);
        wxMessageBox(wxString(_T("catalogNum==>")+wxString::Format(_T("%d"),catalogNum)));
      if (catalogNum == 0)
      {
         catalogNum = 1;
         cfg->Write(_T("/locale/Domain1"), _T("codeblocks"));
         wxMessageBox(wxString(_T("OK")));
      }

      for (int i = 1; i <= catalogNum; ++i)
      {
         wxString tempStr = wxString::Format(_T("/locale/Domain%d"), i);
         wxString catalogName = cfg->Read(tempStr, wxEmptyString);
         wxMessageBox(_T("V")+tempStr+_T("V")+catalogName+_T("V"));
         if (catalogName.IsEmpty())
         {
            cfg->Write(tempStr,
                        cfg->Read(wxString::Format(_T("/locale/Domain%d"), catalogNum)));
            catalogNum--;
            wxMessageBox(tempStr+wxString::Format(_T("/locale/Domain%d"), i));
         }
         else if (cfg->Read(_T("/plugins/") + catalogName))
         {
            m_locale.AddCatalog(catalogName);
            wxMessageBox(tempStr+_T("==>")+catalogName);
         }
      }
      cfg->Write(_T("/locale/catalogNum"), (int)catalogNum);
    }

    cfg->Write(_T("/locale/language"), (int)lng);
}

I don't think I have changed so many things. I just added some functions of wxMessageBox. Compiling it with ANSI and running seems no problem. However, it can't work correctly with UNICODE.

Before running, I deleted the configure file of default.conf. When running, it shows as belown:

with UNICODE
Code
catalogNum==>0
OK
V/locale/Domain1VV
/locale/Domain1/locale/Domain1
......

with ANSI
Code
catalogNum==>0
OK
V/locale/Domain1VcodeblocksV
/locale/Domain1==>codeblocks
......


With UNICODE and no the file of default.conf, the cfg->Read and cfg->Write seems can't work correctly. I have tested that the two functions work correctly if the file of default.conf exists. Although the file doesn't exist, yet the two functions work correctly with ANSI.

Although whithout my moditation to the function InitLocale(), you can compare the two files of default.conf genternated by CB with UNICODE and with ANSI. Notice the value of /locale/Domain1.

Because of this problem, I can't finish the i18n of CB with UNICODE. Now, I can easily finish the i18n because having found the reason. I have try to solve it but failed.
« Last Edit: February 09, 2006, 03:35:03 pm by heromyth »

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: cfg->Read and cfg->Write problems with UNICODE
« Reply #1 on: February 09, 2006, 04:05:45 pm »
You wrote that code in the first place, didn't you? (I asked Yiannis, and he says he is innocent :lol:)

I am asking because I don't quite understand what the purpose of this section of the config file is. Looking at the data, it seems to me like you write the name of every plugin (translated?) to the config file. Is that correct?

If that is the case, you could simply write a wxArrayString, which might make it work because you don't have tag names with numbers then. ConfigManager supports writing and reading complete wxArrayStrings in a safe way. Don't make your life complicated if you don't need to ;)

If you need to save more than just a couple of strings, that's no problem either. ConfigManager supports writing and reading serializable objects and maps thereof. Just derive a class From ISerializable and feed it to ConfigManager. When you need it back, ask for it, and you get an object.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline heromyth

  • Multiple posting newcomer
  • *
  • Posts: 98
    • http://
Re: cfg->Read and cfg->Write problems with UNICODE
« Reply #2 on: February 10, 2006, 02:29:03 am »
You wrote that code in the first place, didn't you? (I asked Yiannis, and he says he is innocent :lol:)
Ok, I admit I have written some of them. My problem is that these codes work correctly with ANSI mode and incorrectly with UNICODE mode.
Quote
I am asking because I don't quite understand what the purpose of this section of the config file is. Looking at the data, it seems to me like you write the name of every plugin (translated?) to the config file. Is that correct?
My purpose is for the convience of all plugin's i18n. Every plugin now has its own mo file.
Quote
If that is the case, you could simply write a wxArrayString, which might make it work because you don't have tag names with numbers then. ConfigManager supports writing and reading complete wxArrayStrings in a safe way. Don't make your life complicated if you don't need to ;)

If you need to save more than just a couple of strings, that's no problem either. ConfigManager supports writing and reading serializable objects and maps thereof. Just derive a class From ISerializable and feed it to ConfigManager. When you need it back, ask for it, and you get an object.
I'll try. Thanks!

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: cfg->Read and cfg->Write problems with UNICODE
« Reply #3 on: February 10, 2006, 11:22:04 am »
My purpose is for the convience of all plugin's i18n. Every plugin now has its own mo file.
Hmmm... if all you want is an individual file name, then why not simply use:
Code
wxString moFile = thePlugin->PluginName() + _T(".mo");

That way, you don't need to store anything in the configuration at all. PluginName is guaranteed to be suitable for a filename, since it is created via a macro that uses ## (so there cannot be any white space or special characters inside).
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline heromyth

  • Multiple posting newcomer
  • *
  • Posts: 98
    • http://
Re: cfg->Read and cfg->Write problems with UNICODE
« Reply #4 on: February 11, 2006, 03:59:56 pm »
Hmmm... if all you want is an individual file name, then why not simply use:
Code
wxString moFile = thePlugin->PluginName() + _T(".mo");

In my way,a new plungin can't use its own mofile until the CB reload it. That's because the i18n should be done at first. If a plugin need to use its own mo file when it loads,  the variable of m_locale in app.h must to be passed the place where the plugin is. It seems to have much more effects on the CB's SDK. Early I did some tests, but failed.

I still belive the cfg->Read and cfg->Write have some problems with UNICODE mode. I'll try my best to find it out.

Offline heromyth

  • Multiple posting newcomer
  • *
  • Posts: 98
    • http://
Re: cfg->Read and cfg->Write problems with UNICODE
« Reply #5 on: February 12, 2006, 08:56:30 am »
This problem has been solved. The patch is at http://developer.berlios.de/patch/?func=detailpatch&patch_id=819&group_id=5358.
It's just a little bug. :P
« Last Edit: February 12, 2006, 08:58:57 am by heromyth »