Re: [Cppcms-users] How to format readable date from unix timestamp within template?
Brought to you by:
artyom-beilis
|
From: Artyom B. <art...@ya...> - 2013-12-09 06:24:50
|
The problem with this clode
std::ostringstream buf;
app()->render(std::string("my_skin"), std::string("book_page"), buf, c);
cp->rContent = buf.str();
is that buf does not have a locale object associated with it.
You need to add following line
buf.imbue(app()->context().locale());
i.e.
std::ostringstream buf;
buf.imbue(app()->context().locale());
app()->render(std::string("my_skin"), std::string("book_page"), buf, c);
cp->rContent = buf.str();
The response().out() - the default output already has a locale embedded in it.
when you create a generic std::ostream you need to imbue application
specific locale (even if it is default system locale) to make various localization
related tools to work.
Artyom Beilis
--------------
CppCMS - C++ Web Framework: http://cppcms.com/
CppDB - C++ SQL Connectivity: http://cppcms.com/sql/cppdb/
--------------------------------------------
On Mon, 12/9/13, augustin <aug...@ov...> wrote:
Subject: Re: [Cppcms-users] How to format readable date from unix timestamp within template?
To: cpp...@li...
Date: Monday, December 9, 2013, 2:25 AM
On Sunday, December 08, 2013 05:52:31
PM Artyom Beilis wrote:
> > r.date_returned is a time_t unix timestamp.
> >
> > <% gt "{1,datetime=s}" using
r.date_returned %>
> > <%= r.date_returned | date %>
> > <%= r.date_returned |
strftime("%d/%m/%Y") %>
> >
> > What am I missing?
> >
> > thanks,
> >
> > augustin.
>
> It actually should work....
>
> How do you render the template?
> What are the locale settings?
> Do you render to a stream that isn't the
respone().out()?
>
> The only point I can see that this isn't working is
that the stream you
> are rendering into is missing the locale
configuration.
Thanks for your reply, Artyom, as always.
For my locale, I have in config.js:
"localization" : {
"locales" : ["en_US.UTF-8"],
},
I have not yet specifically studied locale and translations.
For the time
being English is hard coded in my application.
That particular piece of template is rendered thus:
namespace content {
struct book_page : public cppcms::base_content;
}
void view_book::render_main_content(content::page* cp) {
content::book_page c;
// ... snipped parts where I add content to c,
including r.date_returned.
std::ostringstream buf;
// app() returns a pointer to my class myApp : public
cppcms::application;
app()->render(std::string("my_skin"),
std::string("book_page"), buf, c);
cp->rContent = buf.str();
}
I have simplified the code above. Hopefully, I have not
removed anything
relevant.
The wiki page about template filters is pretty long:
http://cppcms.com/wikipp/en/page/cppcms_1x_templates_comm
Do you think I should complement it by setting up a separate
wiki page about
best practices for rendering times and dates?
Thanks,
augustin.
--
Friends: http://www.reuniting.info/
My projects:
http://astralcity.org/ http://lesenjeux.fr/ http://linux.overshoot.tv/
http://overshoot.tv/ http://charityware.info/ http://masquilier.org/
http://openteacher.info/ http://minguo.info/ http://jacqueslemaire.fr/
http://www.wechange.org/ http://searching911.info/
.
------------------------------------------------------------------------------
Sponsored by Intel(R) XDK
Develop, test and display web and hybrid apps with a single
code base.
Download it for free now!
http://pubads.g.doubleclick.net/gampad/clk?id=111408631&iu=/4140/ostg.clktrk
_______________________________________________
Cppcms-users mailing list
Cpp...@li...
https://lists.sourceforge.net/lists/listinfo/cppcms-users
|