Page 1 of 1
Read string out of ResourceDictionary in C++?
Posted: 24 Apr 2019, 13:18
by unvestigate
Hi,
I am working on the localization of my game and I have set up a system very similar to the localization sample. Just like in the same I have a resource dictionary and xaml file per language and I can bind the UI controls to the language just like in the sample. Sometimes however, you don't know the key of the string until at runtime. Eg. I want to update a piece of the HUD with a new string and my gameplay code passes the string key to the HUD view model.
Now I would like to read the string out of the resource dictionary in C++, do a little bit of transformation on the string and then set it as the text. My question is, how do I get the string out of the resource dictionary. Looks like you can get BaseComponent pointers out of the dictionary, but not strings.
Is there a way to do this?
Re: Read string out of ResourceDictionary in C++?
Posted: 24 Apr 2019, 13:48
by unvestigate
I managed to do it like this. Is this the correct way to handle it?
const char* Localization::getLocalizedString(const char* key)
{
Noesis::Ptr<Noesis::ResourceKeyString> resKeyStr = Noesis::ResourceKeyString::TryCreate(key);
if (!resKeyStr)
{
return nullptr;
}
if (!mSelectedLanguage->getResources()->Contains(resKeyStr))
{
return nullptr;
}
Noesis::BaseComponent* bc = mSelectedLanguage->getResources()->Get(resKeyStr);
if (!bc)
{
return nullptr;
}
NsString localizedString = bc->ToString();
return StringUtility::tempFormat("%s", localizedString.c_str());
}
I am also wondering about the creation of the ResourceKeyString object. I don't expect the calls to getLocalizedString() to happen every frame, but they can still occur quite often. Should I cache the ResourceKeyString objects in a hash map or something for performance or would you say it is fine to create temp objects like this?
Re: Read string out of ResourceDictionary in C++?
Posted: 24 Apr 2019, 13:50
by unvestigate
One more question. Should I release the BaseComponent pointer after I am done with it? The docs don't specify whether or not the Get() method increases the ref count or not.
Re: Read string out of ResourceDictionary in C++?
Posted: 24 Apr 2019, 15:13
by sfernandez
To get a string resource from a dictionary you can do it like this:
const char* Localization::getLocalizedString(const char* key)
{
Noesis::ResourceDictionary* resources = mSelectedLanguage->getResources();
Noesis::Ptr<Noesis::ResourceKeyString> resourceKey = Noesis::ResourceKeyString::Create(key);
Noesis::Ptr<Noesis::BaseComponent> resource;
if (resources->Find(resourceKey, resource))
{
return Noesis::Boxing::Unbox<NsString>(resource).c_str();
}
return nullptr;
}
The resource keys are already cached so you don't need to worry about the creation of these objects.
And you don't have to Release() a BaseComponent object unless you manually called AddReference().
Re: Read string out of ResourceDictionary in C++?
Posted: 24 Apr 2019, 15:19
by unvestigate
Great, thanks!
Re: Read string out of ResourceDictionary in C++?
Posted: 25 Apr 2019, 16:46
by sfernandez
Glad to help, marking this topic as solved.
Re: Read string out of ResourceDictionary in C++?
Posted: 26 Apr 2019, 23:19
by jsantos
By the way, the API for accessing ResourceDictionaties by code is, apart from a deviation from WPF, quite ugly. We have plans to improve it, if you want to track this feature, please report a ticket about it.
Re: Read string out of ResourceDictionary in C++?
Posted: 21 Oct 2020, 08:59
by unvestigate
Sorry to revive this old thread, but perhaps other people will find this useful too. It seems like the solution no longer works in Noesis 3.x. How would I go about getting a string out of the resource dictionary in the latest version of Noesis?
Thanks!
Re: Read string out of ResourceDictionary in C++?
Posted: 21 Oct 2020, 12:16
by sfernandez
We simplified the dictionary interface in 3.0 version, so it is easier now:
const char* Localization::getLocalizedString(const char* key)
{
Noesis::ResourceDictionary* resources = mSelectedLanguage->getResources();
Noesis::Ptr<Noesis::BaseComponent> resource;
if (resources->Find(key, resource))
{
return Noesis::Boxing::Unbox<String>(resource).Str();
}
return nullptr;
}
Re: Read string out of ResourceDictionary in C++?
Posted: 21 Oct 2020, 12:34
by unvestigate
Nice, thanks!