- unvestigate
- Posts: 32
- Joined:
Read string out of ResourceDictionary in C++?
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?
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?
- unvestigate
- Posts: 32
- Joined:
Re: Read string out of ResourceDictionary in C++?
I managed to do it like this. Is this the correct way to handle it?
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?
Code: Select all
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());
}
- unvestigate
- Posts: 32
- Joined:
Re: Read string out of ResourceDictionary in C++?
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.
-
sfernandez
Site Admin
- Posts: 2991
- Joined:
Re: Read string out of ResourceDictionary in C++?
To get a string resource from a dictionary you can do it like this:
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().
Code: Select all
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;
}
And you don't have to Release() a BaseComponent object unless you manually called AddReference().
-
sfernandez
Site Admin
- Posts: 2991
- Joined:
Re: Read string out of ResourceDictionary in C++?
Glad to help, marking this topic as solved.
Re: Read string out of ResourceDictionary in C++?
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.
- unvestigate
- Posts: 32
- Joined:
Re: Read string out of ResourceDictionary in C++?
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!
Thanks!
-
sfernandez
Site Admin
- Posts: 2991
- Joined:
Re: Read string out of ResourceDictionary in C++?
We simplified the dictionary interface in 3.0 version, so it is easier now:
Code: Select all
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;
}
Who is online
Users browsing this forum: Ahrefs [Bot] and 8 guests