-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Hello!

Use WApplication::instance() to get a pointer to current WApplication.
No need to construct application object in handleRequest(). The only
place where application is created is application creating function
(ApplicationCreator), which is passed to WRun() or to
WServer::addEntryPoint(). So, only Wt internals should create new
instances of application, not your code!

WApplication::instance() can be used from event handling functions
(e.g., slots connected to Wt signals) and handleRequest() if WResource
is bind to an application. You can use assert(wApp) to make sure it is
not 0. It can be 0 no application instance is active currently (for
example, in server-global WResource or in main() function).

Macro wApp is equal to WApplication::instance().

Derive from WApplication, it works.
For example, you derived MyApplication from WApplication. To convert
pointer to WApplication to pointer to MyApplication, use boost
polymorphic cast:

#include "boost/cast.cpp"

WApplication* app = WApplication::instance(); // to just wApp
MyApplication* myapp = boost::polymorphic_downcast(app);

boost::polymorphic_downcast uses C++ built-in operators static_cast or
dynamic_cast. Which one of them is used, depends on build
configuration. Release build uses static_cast, Debug build uses
dynamic_cast and checks result is not 0.

If you use boost::polymorphic_downcast many times, it makes sense for
you to create a macro for it:
#define DOWNCAST boost::polymorphic_downcast

Summary: to get WApplication*, use wApp, to get WEnvironment, use
wApp->environment(), to get pointer to your derived application class,
use boost::polymorphic_downcast. wApp is 0 in static WResource.


You seem to use global WResource (added with WServer::addResource()).
In this case, wApp is 0, so you can not use WApplication. To translate
with WString::tr, you can use WServer::setLocalizedStrings(). For
this, you need Wt::WMessageResourceBundle instance. In main(), create
new WMessageResourceBundle, call use() to add your translated XML
files, then pass it to WServer::setLocalizedStrings(). BTW, this
solution does not respect user's locale, all strings are translated to
server's locale. I created feature request for this
http://redmine.webtoolkit.eu/issues/3381

You can workaround this by creation several global
WMessageResourceBundle instances (one per language) and selection one
of them depending on client's language manually. Then you can use
WMessageResourceBundle::resolveKey to translate. To get clients
locale, get HTTP header "Accept-Language" using
Request::headerValue(). (Request instance is passed to
handleRequest().) Parsing it is a complicated task. For example,
"en-US,en;q=0.8,ru;q=0.6" means Russian.



-----BEGIN PGP SIGNATURE-----
Version: OpenPGP.js v0.6.1-dev
Comment: http://openpgpjs.org

wsBcBAEBCAAQBQJTqoQeCRDwHNxVpsK2zgAAlIAIAISN0TnveWfjlgyOArxt
hrimxb09n+KDCqHgJ2Pzi5jpU2JPPO8xrcacanQnCvdGTJbvs3TxRR0hpIIf
kHABHfjtLveMbv3VngSaM5/05lf/kSZtfb6FLUsslety179J8xrl28rCNPKm
z7AdJXI//q5WJtHalBz6E+wfxeaLSWJevVpGHgInrDgDeePOGY7zjBgsRvfI
/USUceUvzm9nSjwIGJ91cqqdbBTCDFii6/nnk52aaDro83t5gFVVeXpTy8An
80f6zw+lbderkWrizdjUKawPJNsHi6QPRRycNCuHOhAfW2celCQ4vALx6OoH
D4lWSxUGgCOT3/JzRbbScmE=
=52AO
-----END PGP SIGNATURE-----



-
Best regards,
Boris Nagaev


On Wed, Jun 25, 2014 at 9:42 AM, Jeffrey Scott Flesher Gmail <
jeffrey.scott.fles...@gmail.com> wrote:

>  Thanks for the reply, do you mean something like this, but not this, this 
> does not work:
>
> void BlogRSSFeed::handleRequest(const Wt::Http::Request &request, 
> Wt::Http::Response &response)
> {
>     const Wt::WEnvironment& env = Wt::WApplication::instance()->environment();
>     Wt::WApplication *app = new Wt::WApplication(env);
>     Wt::WApplication::UpdateLock lock(app);
>     if (lock)
>     {
>         app->messageResourceBundle().use("./app_root/ww-home", false);
>     }
>     // you can see I need to make my blog feed Multilingual, this is why I am 
> doing all this
>     std::string url          = Wt::WString::tr("rss-url").toUTF8();
>     std::string title_       = Wt::WString::tr("rss-title").toUTF8();
>     std::string description_ = Wt::WString::tr("rss-description").toUTF8();
>     ...
> }
>
> It crashes at the 1st line,
> my guess is that it does not like Wt::WApplication::instance()
> Doesn't work
> Wt::WApplication.environment()
> and just to be honest, this is not very intuitive example,
> since WApplication requires and WEnvironment,
> and WEnvironment requires an WApplication,
> its like what came firs the Chicken or the Egg,
> I can't define just the WEnvironment,
> const Wt::WEnvironment& env = new Wt::WEnvironment();
> I have tried every combination I can think of.
>
> I have asked many time in many different scenarios how to create an 
> WApplication object,
> from a class that is not derived from WApplication,
> because I have never been able to figure it out,
> and this is the main issue I am having and I can not find an answer in the 
> forums,
> or any other post, nor could I find an example of anyone ever doing this,
> so it would really help if someone could tell me that one line of code that 
> will make this work?
>
> I understand that this resource is stand alone,
> so deriving it from WApplication is not really an option,
> yet this effects your own website, since its the same code,
> and I notice you did not make the blog feed Multilingual,
> and now I must ask why,
> since I figure you may have run into the same problem,
> otherwise you would have done it yourself,
> and least that is my thinking,
> so my question is can this be done,
> and if so how?
>
> Thanks
>
>
> On Wed, 2014-06-25 at 02:07 +0400, Nagaev Boris wrote:
>
>
> Hello!
>
> Take application lock (Wt::WApplication::UpdateLock) from
> handleRequest() if you change WApplication (by changing message
> resource bundle). Multiple requests may happend simultaneously for a
> single resource.
>
> To get WEnvironment from WApplication, use method environment().
> See 
> http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1WApplication.html
>
> for full description of WApplication.
>
> To get translated strings, use WString::tr() method. It works even in
> handleRequest().
>
>
> -
> Best regards,
> Boris Nagaev
>
>
> On Tue, Jun 24, 2014 at 7:01 AM, Jeffrey Scott Flesher Gmail
> <jeffrey.scott.fles...@gmail.com> wrote:
> > In the Example BlogRSSFeed, I want to use messageResourceBundle,> so in> 
> > BlogRSSFeed::handleRequest>> Wt::WApplication *app = 
> > Wt::WApplication::instance();> app->messageResourceBundle().use("appPath", 
> > false);>> I get a runtime error.>> Any ideas on how I can get this to 
> > function correctly?>> Also I don't know how to get an instance of 
> > WEnvironment from any class not> derived from WApplication, do you?> If I 
> > did, I could write a class to return the tr strings.>> Thanks
> > _______________________________________________> witty-interest mailing 
> > list> witty-interest@lists.sourceforge.net> 
> > https://lists.sourceforge.net/lists/listinfo/witty-interest>
> _______________________________________________
> witty-interest mailing 
> listwitty-interest@lists.sourceforge.nethttps://lists.sourceforge.net/lists/listinfo/witty-interest
>
>
>
> ------------------------------------------------------------------------------
> Open source business process management suite built on Java and Eclipse
> Turn processes into business applications with Bonita BPM Community Edition
> Quickly connect people, data, and systems into organized workflows
> Winner of BOSSIE, CODIE, OW2 and Gartner awards
> http://p.sf.net/sfu/Bonitasoft
> _______________________________________________
> witty-interest mailing list
> witty-interest@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/witty-interest
>
>
------------------------------------------------------------------------------
Open source business process management suite built on Java and Eclipse
Turn processes into business applications with Bonita BPM Community Edition
Quickly connect people, data, and systems into organized workflows
Winner of BOSSIE, CODIE, OW2 and Gartner awards
http://p.sf.net/sfu/Bonitasoft
_______________________________________________
witty-interest mailing list
witty-interest@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/witty-interest

Reply via email to