unvestigate
Topic Author
Posts: 32
Joined: 17 Jan 2018, 09:55

Read string out of ResourceDictionary in C++?

24 Apr 2019, 13:18

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?
 
unvestigate
Topic Author
Posts: 32
Joined: 17 Jan 2018, 09:55

Re: Read string out of ResourceDictionary in C++?

24 Apr 2019, 13:48

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?
 
unvestigate
Topic Author
Posts: 32
Joined: 17 Jan 2018, 09:55

Re: Read string out of ResourceDictionary in C++?

24 Apr 2019, 13:50

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.
 
User avatar
sfernandez
Site Admin
Posts: 2991
Joined: 22 Dec 2011, 19:20

Re: Read string out of ResourceDictionary in C++?

24 Apr 2019, 15:13

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().
 
unvestigate
Topic Author
Posts: 32
Joined: 17 Jan 2018, 09:55

Re: Read string out of ResourceDictionary in C++?

24 Apr 2019, 15:19

Great, thanks!
 
User avatar
sfernandez
Site Admin
Posts: 2991
Joined: 22 Dec 2011, 19:20

Re: Read string out of ResourceDictionary in C++?

25 Apr 2019, 16:46

Glad to help, marking this topic as solved.
 
User avatar
jsantos
Site Admin
Posts: 3918
Joined: 20 Jan 2012, 17:18
Contact:

Re: Read string out of ResourceDictionary in C++?

26 Apr 2019, 23:19

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
Topic Author
Posts: 32
Joined: 17 Jan 2018, 09:55

Re: Read string out of ResourceDictionary in C++?

21 Oct 2020, 08:59

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!
 
User avatar
sfernandez
Site Admin
Posts: 2991
Joined: 22 Dec 2011, 19:20

Re: Read string out of ResourceDictionary in C++?

21 Oct 2020, 12:16

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;
}
 
unvestigate
Topic Author
Posts: 32
Joined: 17 Jan 2018, 09:55

Re: Read string out of ResourceDictionary in C++?

21 Oct 2020, 12:34

Nice, thanks!

Who is online

Users browsing this forum: Ahrefs [Bot] and 8 guests