About Monkey 2 › Forums › Monkey 2 Code Library › Localization system (allow change locale on-the-fly)
This topic contains 4 replies, has 3 voices, and was last updated by 
 Diffrenzy
 8 months, 1 week ago.
- 
		AuthorPosts
 - 
		
			
				
August 12, 2018 at 10:00 am #15288
Hi everyone.
Some time ago I published localization system on github: https://github.com/engor/m2-localization
It is easy to use lightweight system that allow you to keep all texts localized on-the-fly.
Supported formats – json and ini. And you can add own format via registering loaders.
There are few demos on github that describe different usecases.
I just pushed some improvements.
Benefits:
- All binded texts will be localized on-the-fly when locale will be changed;
 - You can register any format loader, android-strings for example via extending LocLoader class;
 - You can bring localization support into any classes via adding just one method Localize( t:String ), and you can do it via extension – so you needn’t to extend classes, and you can add this method even into Final classes! Note: extension way works with latest develop branch of monkey2, so will work out of the box starting from next release.
 - Lightweight and easy to use – there are polymorphic Localized function out of the box with different types that register you elements to be localized;
 - You can override any custom constant in realtime – load texts from file and replace any value(s) for given lang and given key.
 
Example of file with translations (json format):
Monkey1234567891011121314151617181920212223{"langs": ["ru","en"],"default": "en","ru": {"app-title": "Пример локализации","button-remove": "Удалить","button-close": "Закрыть","hello-world": "Привет, мир!","switch-lang": "Переключить язык","hint": "Нажмите Enter чтобы переключить язык"},"en": {"app-title": "Localization demo","button-remove": "Remove","button-close": "Close","hello-world": "Hello world!","switch-lang": "Switch lang","hint": "Press Enter to switch lang"}}Usage in code:
Monkey1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950Namespace jentos.locale.demo.mojox#Import "<std>"#Import "<mojo>"#Import "<mojox>"#Import "Localization"#Import "assets/"Using std..Using mojo..Using mojox..Using jentos.locale..Function Main()' load texts from json-fileLocale.Load( "asset::translation.json","en" )New AppInstanceNew MyWindowApp.Run()EndClass MyWindow Extends WindowMethod New()Super.New( "",640,480,WindowFlags.Resizable )' here we create mojox.Label with text' from localization file with key "hello"ContentView=Localized<Label>( "hello" )EndEnd' just add the only method and now' all views that extend Label give localization supportClass Label ExtensionMethod Localize( t:String )Text=tEndEndFor more complex examples look at demo projects on github.
Support is welcome.
August 12, 2018 at 11:36 pm #15292Cool.
August 13, 2018 at 12:19 pm #15293Do you have a suggestion as how to implement randomized texts?
In my own module I do:
_multitexts = New StringMap<StringStack>
Local key:= “btn_ok”
_multitexts.Add(key,New StringStack)
_multitexts.Get(key).Add(“Ok”)
_multitexts.Get(key).Add(“Fine”)
_multitexts.Get(key).Add(“Will Do”)and then the module looks to see if there is a multitext map, and if so picks random entry. before looking in the regular StringMap
In the language file, I’d like to avoid having key-01,key-02,key-03 naming, so that I kan just edit without thinking to much.
Would it be possible to allow, button-remove = Remove;Get rid of, Kill, Die!; syntax or similar, and then somefunction to get Random text, or is such functions beyond the scope of your module?
August 13, 2018 at 2:32 pm #15296I just pushed improvements to github.
Look at new demo-multivalue.monkey2.
Now we can register custom value-providers to override behaviour.
As far as there is Null value for String type is empty string and empty string can be valid value,
I created a bit monstrouse function defenition with Bool Ptr (omg!) to have two return values.
Monkey1providerFunc:String( key:String,eaten:Bool Ptr )if such provider func set eaten=True then we get/found actual value and must return it even if it’s an empty string.
For example, we want to censoring all texts by keys, for example by key “java”.
Monkey1234567891011Locale.Load( "asset::translation.ini","en" )Locale.RegisterValuesProvider( Lambda:String( key:String,eaten:Bool Ptr )If key.ToLower().Contains( "java" )eaten[0]=TrueReturn "[censored]"Endifeaten[0]=FalseReturn ""End )Now all keys containing word “java” will return “[censored]” string.
August 13, 2018 at 8:59 pm #15300That is absolutely brilliant! Thank you very much.
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.