Dear all,
when I call wxDateTime.Format I always get back a date written in English, even though my win OS is set to another language. If I set say wxLocale locale(wxLANGUAGE_ITALIAN); then the date is formatted in Italian, but ONLY locally in the wxLocale locale object scope.
How can I make the settings global to my whole application, please?
BR,
Max.
Date format in wxLocale Topic is solved
Re: Date format in wxLocale
You answered your own question already. In order to preserve the set locale, its instance can not go out of scope. If your locale is supposed to be application-wide, set it e.g. in the OnInit() method of your wxApp instance. You can take a look at the code in the internat sample (located in %WXWIN%\samples\internat folder).
Re: Date format in wxLocale
OK. And that way I get a m_locale member of the mainframe of my app which I can use app-spread. But what if I have e.g. a wxGrid and i set a cell renderer for the date:PB wrote:You answered your own question already. In order to preserve the set locale, its instance can not go out of scope. If your locale is supposed to be application-wide, set it e.g. in the OnInit() method of your wxApp instance. You can take a look at the code in the internat sample (located in %WXWIN%\samples\internat folder).
Code: Select all
wxGridCellDateTimeRenderer *dateRenderer = new wxGridCellDateTimeRenderer(wxLocale::GetInfo(wxLOCALE_LONG_DATE_FMT), wxLocale::GetInfo(wxLOCALE_DATE_TIME_FMT));
Grid->SetCellRenderer(row, (*mapArray)[col].pos, dateRenderer);
Thanks a lot, BR,
Max.
Re: Date format in wxLocale
Sorry, I have no experience whatsoever with wxGridCellDateTimeRenderer, so I just ask again: Is the locale you set earlier still in effect during the lifetime of your Grid? In another words, set your locale before you create the grid and ensure its instance doesn't get destroyed before the grid/frame does, e.g. the locale is a member variable of the class you derived from wxApp or wxFrame.
Re: Date format in wxLocale
Well, here is happening something I don't understand: all my app has the locale I initialize in wxApp::OnInit() and I pass to wxLocale wxFrame::m_locale of my app.
But the wxGrid, which I create in the constructor of wxFrame has a different wxLocale. Actually the wxGrid is child of a wxAuiNotebook object which is a child of the wxFrame of the app, and is a pane to a wxAuiManager that manages the wxFrame itself. I wonder if in one of these passages there is a new wxLocale getting focus over the subsequent childs...
But the wxGrid, which I create in the constructor of wxFrame has a different wxLocale. Actually the wxGrid is child of a wxAuiNotebook object which is a child of the wxFrame of the app, and is a pane to a wxAuiManager that manages the wxFrame itself. I wonder if in one of these passages there is a new wxLocale getting focus over the subsequent childs...
Re: Date format in wxLocale
I'm not quite sure what you mean here.all my app has the locale I initialize in wxApp::OnInit() and I pass to wxLocale wxFrame::m_locale of my app.
Anyway, it's pretty simple in principle: The locale settings is always application-wide. You only need one active wxLocale instance, usually as member variable of wxApp and initialize it in wxApp::OnInit(). After that you don't have to touch it any more and you don't have to pass the object around to other classes.
Alternatively, calling wxApp::SetCLocale() should also work.
Use the source, Luke!
Re: Date format in wxLocale
what I mean is if I put a piece of code likedoublemax wrote:I'm not quite sure what you mean here.all my app has the locale I initialize in wxApp::OnInit() and I pass to wxLocale wxFrame::m_locale of my app.
Code: Select all
wxString str;
str.Printf(wxT("current date: %s"), wxDateTime::Now().Format("%A %d %B %Y"));
wxMessageBox( str );
So wxDateTime::Format works anywhere in my app, but in the wxWidgets standard code that actually writes the date in the wxGrid cell the wxLocale settings seem to be ignored:
Code: Select all
wxString wxGridCellDateTimeRenderer::GetString(const wxGrid& grid, int row, int col)
{
wxGridTableBase *table = grid.GetTable();
bool hasDatetime = false;
wxDateTime val;
wxString text;
if ( table->CanGetValueAs(row, col, wxGRID_VALUE_DATETIME) )
{
void * tempval = table->GetValueAsCustom(row, col,wxGRID_VALUE_DATETIME);
if (tempval)
{
val = *((wxDateTime *)tempval);
hasDatetime = true;
delete (wxDateTime *)tempval;
}
}
if (!hasDatetime )
{
text = table->GetValue(row, col);
const char * const end = val.ParseFormat(text, m_iformat, m_dateDef);
hasDatetime = end && !*end;
}
if ( hasDatetime )
text = val.Format(m_oformat, m_tz );
// If we failed to parse string just show what we where given?
return text;
}
If I derive my own renderer and override the Draw function so that it calls my own version of GetString, I should have solved the issue, but it's really crappy... How comes the standard GetString doesn't work?? Is it a bug?
Re: Date format in wxLocale
The code in wxGridCellDateTimeRenderer looks ok. But as the "grid" sample doesn't use this class, i can't test this easily.
So if you want someone to look into this, you'll have to create a patch to the grid sample that shows the problem.
So if you want someone to look into this, you'll have to create a patch to the grid sample that shows the problem.
Use the source, Luke!
Re: Date format in wxLocale
Dear all,
I've been misled by the internat sample: There wxLocale::Init is used, which in my project didn't work well (I got asserts in intl.cpp ln 1445 of method wxLocale::GetInfo) I don't know what was I doing wrong, but for the moment I don't care much, since I've no internationalization nor dictionary files. I guess something in wxLocale::Init or in m_locale.AddCatalog("wxstd"); was messing it up... Anyway after that, I ended up with a wxLocale object local to wxApp::OnInit(). So it was alive until the end of the wxFrame::wxFrame c'tor, then it was deleted. By declaring it as wxLocale *wxApp::m_locale, everything works well, the wxGrid too. Actually even though the wxGrid was instantiated by the wxFrame::wxFrame c'tor, then the wxGridCellDateTimeRenderer::Draw was called on mouse event when the part of the grid containing the date had to be drawn, and that was in the eventloop, outside the wxFrame::wxFrame c'tor.
So sorry for wasting your time, hope somebody finds this post useful in case he does my same mistake.
BR,
Max.
I've been misled by the internat sample: There wxLocale::Init is used, which in my project didn't work well (I got asserts in intl.cpp ln 1445 of method wxLocale::GetInfo) I don't know what was I doing wrong, but for the moment I don't care much, since I've no internationalization nor dictionary files. I guess something in wxLocale::Init or in m_locale.AddCatalog("wxstd"); was messing it up... Anyway after that, I ended up with a wxLocale object local to wxApp::OnInit(). So it was alive until the end of the wxFrame::wxFrame c'tor, then it was deleted. By declaring it as wxLocale *wxApp::m_locale, everything works well, the wxGrid too. Actually even though the wxGrid was instantiated by the wxFrame::wxFrame c'tor, then the wxGridCellDateTimeRenderer::Draw was called on mouse event when the part of the grid containing the date had to be drawn, and that was in the eventloop, outside the wxFrame::wxFrame c'tor.
So sorry for wasting your time, hope somebody finds this post useful in case he does my same mistake.
BR,
Max.