Loading the strings initially
A commenter just asked, ‘Docs say “The TextService supports runtime dynamic reloading of strings, so if these are reloaded, in say, a different language, all your IHaveUpdateableText instances will be reformatted and updated immediately.”
How do I reload strings dynamically?’
Good question. Let’s start by seeing how the strings are loaded in the first place.
The strings are kept – initially – in the configuration.yml file. It’s convenient and straightforward, and the section looks something like this:
strings:
home_title: "Home Section"
media_title: "Media Section"
news_title: "News Section"
The configuration is loaded into the ConfigurationService by the D_Services command in org.birdbase.framework.controller.core.
Once the configuration is loaded successfully, we parse the YML into a dictionary and deliver it to various objects:
(ConfigurationService)
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
| private function handleComplete( e:Event ) : void
{
try
{
var d:Dictionary = YAML.decode( e.target.data ) as Dictionary;
setConfiguration( d )
setStrings( d );
setPreferences( d );
setNavigation( d );
eventDispatcher.dispatchEvent( new StateEvent( StateEvent.ACTION, BootManagement.CONFIGURING_SERVICES_COMPLETE ) );
}
catch( e:Error )
{
fatal( e.message );
eventDispatcher.dispatchEvent( new StateEvent( StateEvent.ACTION, BootManagement.CONFIGURING_SERVICES_FAILED ) );
}
} |
Line 104 above calls the setStrings function which basically sends the strings chunk of the parsed YML to the TextService:
78
| textService.strings = d.strings; |
The TextService then updates any updateable components that are registered. And that’s it.
So, the way to update the strings dynamically is to simply set them on the TextService instance. But how do we load a new file in?
Loading a new YML file of strings
The new YML should look exactly like the strings section in the configuration.yml, although the values will be different:
strings:
home_title: "Accueil"
media_title: "Medias"
news_title: "Nouvelles"
So we ought to have an URLLoader to load this for us.
[Inject]
public var textService:ITextService;
var loader:URLLoader = new URLLoader();
loader.addEventListener( Event.COMPLETE, handleComplete );
loader.load( new URLRequest( "<path/to/my/new/strings/file.yml>" ) );
function handleComplete( e:Event ):void
{
var d:Dictionary = YAML.decode( e.target.data ) as Dictionary;
textService.strings = d.strings;
}
And the TextService will update all your registered components.