Project

General

Profile

Problems with >messageResourceBundle().use(); -Unresolved strings

Added by Dushan Savich over 14 years ago

Hi all,

I'm writing here instead to the mailing list, just to compare the response times :)

Anyway, here's the problem :

Wt can't resolve my strings . I'm sure that it has something to do with paths ,

I've created XML text resource bundle like this :

//-------------------------------------------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF8"?>



Search



LakoDoRechi.com - vas online rechnik

////------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------------------------------------------

Here's the code :

//-------------------------------------------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------------------------------------------

const std::string PATH_TO_MESSAGES = "resources/text/strings.xml";

LrsApp::LrsApp(const WEnvironment& env) : WApplication(env)

{

this->messageResourceBundle().use(PATH_TO_MESSAGES);

std::string templ = strFromFile("templates/index.wtml");

setTitle( WString::tr("website-title") );

WTemplate *t = new WTemplate(templ, root() );

buttonSearch_ = new WPushButton( WString::tr("search-button") , root() );

}

//-------------------------------------------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------------------------------------------

And when I start the program like this:

---http-address=0.0.0.0 ---http-port=8080 ---docroot=.

, I get those 'search-button' unresolved strings on my widgets :) .

You all know what I'm talking about , so there's no need for a screenshot :)

I know I'm missing something very small here, some small step, and I've figured that you can point me to what is it :)

Thanks in advance,

Dushan


Replies (10)

RE: Problems with >messageResourceBundle().use(); -Unresolved strings - Added by Koen Deforche over 14 years ago

Hey Dushan,

You should reference the resource bundle: const std::string PATH_TO_MESSAGES = "resources/text/strings" (i.e. without the xml)

The reason for this major gotcha (it has bitten me too a few times) is that it will append ".xml" or "_nl.xml" etc... depending on the locale.

Are you clocking the response time ? :-)

Regards,

koen

RE: Problems with >messageResourceBundle().use(); -Unresolved strings - Added by Dushan Savich over 14 years ago

WHUUUSHHH!!

Look, up in the sky!

It's a plane !

It's a comet!

No , it's Koen writting responces in the Redmine! :D lol

Anyway , it seems you really know thing or two about this library , since the fix worked ,

Thank you :)

Do you think if it would be a good idea to put this into FAQ and/or documentation ?

Regs,

Dushan

RE: Problems with >messageResourceBundle().use(); -Unresolved strings - Added by Gaël Chamoulaud over 13 years ago

Hi everybody,

I exactly have the same problem but just with Google Chrome. The same code works very well on Firefox but with chrome, every strings are unresolved and are displayed like key ...

Cheers,

Gaël

RE: Problems with >messageResourceBundle().use(); -Unresolved strings - Added by Wim Dumon over 13 years ago

Probably a similar reason: a resource bundle that is not found. This should however not give different behaviour on Chrome vs Firefox.

Every string within a tr("key") should be found in a resource bundle, or it will be rendered as key. Did you restart your application from a different path? Did you run the two browsers simultaneously and observe this problem? Is the language setting of one browser different from the other?

BR,

Wim.

RE: Problems with >messageResourceBundle().use(); -Unresolved strings - Added by Gaël Chamoulaud over 13 years ago

Thanks for your reply, Wim,

I resolved my problem ... it was in the language setting of my chrome browser... :/

EN in Chrome !

FR in Firefox !

Drrr!

Gaël

RE: Problems with >messageResourceBundle().use(); -Unresolved strings - Added by Wim Dumon over 13 years ago

If one of your resource files doesn't have a language extension (mybundle.xml), it will be the default language bundle. If the key is not found in the language-specific resource file (mybundle_fr.xml), the default bundle will still be searched for that key.

Generally, it's not a bad idea to have a 'default' bundle that contains all your keys and translates them in some language.

Wim.

RE: Problems with >messageResourceBundle().use(); -Unresolved strings - Added by Alexander Lyalin about 13 years ago

I have some problem.

Code:

class ServiceApp : public Wt::WApplication

{

public:

ServiceApp(const Wt::WEnvironment& env)

{

this->setLocale("");

this->messageResourceBundle().use(WApplication::appRoot() + "Service");

Wt::WText *name = new Wt::WText(root());

name->setText(Wt::WWidget::tr("test"));

}

}

String resources(D:\dev\wdi\Bin\Debug\Service\xml):

<?xml version="1.0" encoding="iso-8859-1" ?>



Test

I start the program like this:

service.exe ---http-address=0.0.0.0 ---http-port=8080 ---docroot=D:\dev\wdi\Bin\Debug

I get as a result of:

??test??

What am I doing wrong?

Thanks.

RE: Problems with >messageResourceBundle().use(); -Unresolved strings - Added by Alexander Lyalin about 13 years ago

Sorry, path to xml is D:\dev\wdi\Bin\Debug\Service.xml

RE: Problems with >messageResourceBundle().use(); -Unresolved strings - Added by Wim Dumon about 13 years ago

Hello Alexander,

The code looks fine.

There are two important paths in Wt that are different in an important way:

  • docroot: points to the base directory of the files you want to serve through the built-in httpd. Every file in this directory or subdirectories thereof can be downloaded from your computer through HTTP when your application is running. You should set it to the directory that contains your css files, the resources directory, images, and other files that the browser needs access to while executing your application. In short, it is the directory that contains the files that the client can access.
  • approot: this is a mechanism in Wt that allows you to specify where files that the application needs, but that are not intended to be served, reside. A translation bundle is indeed something that your application needs server-side, but a browser has no interest in downloading it. Other examples are database file locations, configuration files for your application, ... The use of approot is optional. Whenever you need to access a file on your local disk, we suggest that you write (wApp->appRoot() + "Filename") instead of simply Filename, and then Wt provides a means to allow you to move those files around, or start multiple instances of your application using files in different locations. Again, this mechanism is optional, and we provide it as a conventional way to access such files, but you may also choose to use relative or absolute paths in your application, or use yet another method to locate these files. If you did not configure approot specifically for your application, it defaults to an empty string, so the files will be relative to your 'current work directory'.

A docroot set to d:/dev/wdi/Bin/Debug is almost always wrong (assuming that this is your build directory containing your executable). This means that your .exe file can be downloaded though the built-in http server.

You don't specify an approot path (assuming you didn't specify it in wt_config.xml neither), so it defaults to the CWD. What is the working directory when you start service.exe? It should be d:/dev/... Alternatively, specify approot on the commandline, and then the CWD will not matter anymore to find your resources file:

service.exe --http-address=0.0.0.0 --http-port=8080 --docroot=D:\dev\wdi\Bin\Debug --approot=D:\dev\wdi\Bin\Debug

Note that approot is a new option in wt 3.1.9, and that docroot in the line above is probably still pointing to the wrong location. You can set approot also in wt_config.xml, and other connectors may allow you to set it in yet other ways.

Hope this helps,

Wim.

    (1-10/10)