PageRenderTime 0ms CodeModel.GetById 3ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/Multilingual/Multilingual.Example/Multilingual.Example/PageModels/LanguagePageModel.cs

https://bitbucket.org/ThunderSL/sl.xamarin.multilingual
C# | 74 lines | 65 code | 7 blank | 2 comment | 3 complexity | bf816e58042b3908c56fde8446b74fe8 MD5 | raw file
 1using System;
 2using System.Collections.Generic;
 3using System.Globalization;
 4using System.IO;
 5using System.Linq;
 6using System.Reflection;
 7using System.Text;
 8using System.Threading.Tasks;
 9using System.Windows.Input;
10using System.Xml;
11using System.Xml.Linq;
12using System.Xml.Serialization;
13using Multilingual.Example.Models;
14using Multilingual.Example.Pages;
15using Multilingual.Example.Resources.Languages;
16using Newtonsoft.Json;
17using Newtonsoft.Json.Linq;
18using Xamarin.Forms;
19using Xamarin.Forms.Internals;
20
21namespace Multilingual.Example.PageModels
22{
23    public class LanguagePageModel
24    {
25        public List<Language> Languages { get; set; }
26        public int SelectedIndex { get; set; }
27
28        public LanguagePageModel()
29        {
30            ReadLanguagesResources();
31        }
32
33        private void ReadLanguagesResources()
34        {
35            using (var stream = this.GetType().Assembly.GetManifestResourceStream("Multilingual.Example.Resources.Languages.Languages.json"))
36            {
37                using (var reader = new StreamReader(stream))
38                {
39                    //Set languages picker
40                    var json = JObject.Parse(reader.ReadToEnd());
41                    Languages = JsonConvert.DeserializeObject<List<Language>>(JsonConvert.SerializeObject(json["languages"]));
42
43                    //Get current culture info
44                    var info = Plugin.Multilingual.Current.CurrentCultureInfo;
45
46                    for (var i=0; i<Languages.Count ;i++)
47                    {
48                        if (Languages[i].Abbreviation == info.TwoLetterISOLanguageName)
49                            SelectedIndex = i;
50                    }
51                }
52            }
53        }
54
55        public ICommand AcceptCommand
56        {
57            get
58            {
59                return new Command(() =>
60                {
61                    var test = Languages[SelectedIndex].Abbreviation;
62                    Plugin.Multilingual.Current.CurrentCultureInfo = new CultureInfo(test);
63                    AppResources.Culture = Plugin.Multilingual.Current.CurrentCultureInfo;
64
65                    var page = new NavigationPage(new HomePage())
66                    {
67                        BarBackgroundColor = Color.FromHex("#212121")
68                    };
69                    Application.Current.MainPage = page;
70                });
71            }
72        }
73    }
74}