Skip to content

Instantly share code, notes, and snippets.

@martijn00
Last active January 15, 2018 16:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martijn00/b13cb2481d3f3ff78ba0 to your computer and use it in GitHub Desktop.
Save martijn00/b13cb2481d3f3ff78ba0 to your computer and use it in GitHub Desktop.
MvvmCross Jsonlocalisation
public class App : MvxApplication
{
public override void Initialize()
{
TextProviderBuilder.SetDefaultLanguage(language);
TextProviderBuilder builder = new TextProviderBuilder(new AppJsonDictionaryTextProvider());
Mvx.RegisterSingleton<IMvxTextProviderBuilder>(builder);
Mvx.RegisterSingleton<IMvxTextProvider>(builder.TextProvider);
}
}
public class AppJsonDictionaryTextProvider : MvxDictionaryTextProvider, IMvxJsonDictionaryTextLoader
{
protected IMvxResourceLoader ResourceLoader
{
get
{
return Mvx.Resolve<IMvxResourceLoader>();
}
}
public AppJsonDictionaryTextProvider(bool maskErrors = true)
: base(maskErrors)
{
}
public void LoadJsonFromResource(string namespaceKey, string typeKey, string resourcePath)
{
IMvxResourceLoader resourceLoader = this.ResourceLoader;
string textResource = resourceLoader.GetTextResource(resourcePath);
if (string.IsNullOrEmpty(textResource))
{
throw new FileNotFoundException("Unable to find resource file " + resourcePath);
}
LoadJsonFromText(namespaceKey, typeKey, textResource);
}
public void LoadJsonFromText(string namespaceKey, string typeKey, string rawJson)
{
Dictionary<string, Dictionary<string, string>> dictionaries = Mvx.Resolve<IMvxJsonConverter>().DeserializeObject<Dictionary<string, Dictionary<string, string>>>(rawJson);
foreach (KeyValuePair<string, Dictionary<string, string>> currentDictionary in dictionaries)
{
foreach (KeyValuePair<string, string> current in currentDictionary.Value)
{
base.AddOrReplace(namespaceKey, currentDictionary.Key, current.Key, current.Value);
}
}
}
}
File structure:
Core
> Translation
> en
main.json
iOS
> Link folder "Translation" to root of iOS project
- Select right mouse on the "main.json" go to build action and select "Content"
Android
> Assets
> Link folder "Translation" to Assets folder
- Select right mouse on the "main.json" go to build action and select "AndroidAsset"
Plugins used:
- JsonLocalisation
- ResourceLoader
Structure of json file "main.json":
{
"LoginViewModel": {
"Title": "Login",
"TbxUsername.Hint": "Username",
"TbxPassword.Hint": "Password",
"BtnLogin.Text": "Login"
},
"SomeOtherViewModel": {
"Title": "Login",
"TbxUsername.Hint": "Username",
"TbxPassword.Hint": "Password",
"BtnLogin.Text": "Login"
}
}
public class TextProviderBuilder : MvxTextProviderBuilder
{
private static string DefaultLanguage;
public TextProviderBuilder(BmmJsonDictionaryTextProvider provider)
: base("AppNamespace", "Translation", provider, provider)
{
}
public static void SetDefaultLanguage(string language)
{
DefaultLanguage = language;
}
public static string GetDefaultLanguage()
{
return DefaultLanguage;
}
/// <summary>
/// Load a dictionary, containing the ViewModel and the file, that should be loaded for that ViewModel.
/// </summary>
protected override IDictionary<string, string> ResourceFiles
{
get
{
return new Dictionary<string, string>() { { "main", "main" } };
}
}
protected override string GetResourceFilePath(string whichLocalisationFolder, string whichFile)
{
if (string.IsNullOrEmpty(whichLocalisationFolder))
{
whichLocalisationFolder = DefaultLanguage;
}
return base.GetResourceFilePath(whichLocalisationFolder, whichFile);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment