From 35f61826895cd36f6dbf47599c5a5a96ef561e7c Mon Sep 17 00:00:00 2001 From: Chebotov Nikolay Date: Tue, 24 Mar 2020 17:19:19 +0300 Subject: [PATCH 1/2] Add fix: change TabBar localization by settings --- DotNetRu.Clients.UI/AppShell.xaml | 4 ++++ DotNetRu.Clients.UI/AppShell.xaml.cs | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/DotNetRu.Clients.UI/AppShell.xaml b/DotNetRu.Clients.UI/AppShell.xaml index bd518245..7778aef6 100644 --- a/DotNetRu.Clients.UI/AppShell.xaml +++ b/DotNetRu.Clients.UI/AppShell.xaml @@ -26,24 +26,28 @@ diff --git a/DotNetRu.Clients.UI/AppShell.xaml.cs b/DotNetRu.Clients.UI/AppShell.xaml.cs index 520020c7..90ed81ec 100644 --- a/DotNetRu.Clients.UI/AppShell.xaml.cs +++ b/DotNetRu.Clients.UI/AppShell.xaml.cs @@ -1,3 +1,6 @@ +using DotNetRu.Clients.Portable.Helpers; +using DotNetRu.Clients.Portable.ViewModel; +using DotNetRu.Utils.Helpers; using Xamarin.Forms; namespace DotNetRu.Clients.UI @@ -15,6 +18,22 @@ public AppShell() // Can't set BackgrounColor as StaticResource, see https://github.com/xamarin/Xamarin.Forms/issues/7055 SetBackgroundColor(this, backgroundColor); + + MessagingCenter.Subscribe(this, MessageKeys.LanguageChanged, sender => this.UpdateTabBarLocalization()); + } + + public void UpdateTabBarLocalization() + { + Device.BeginInvokeOnMainThread(() => + { + if (this.BindingContext is ViewModelBase viewModel) + { + this.NewsTab.Title = viewModel.Resources["News"] ?? this.NewsTab.Title; + this.SpeakersTab.Title = viewModel.Resources["Speakers"] ?? this.SpeakersTab.Title; + this.MeetupsTab.Title = viewModel.Resources["Meetups"] ?? this.MeetupsTab.Title; + this.AboutTab.Title = viewModel.Resources["About"] ?? this.AboutTab.Title; + } + }); } } } From 708ac60b5c40fc1ee77d5c11b0c194ec2cdd9cfc Mon Sep 17 00:00:00 2001 From: Chebotov Nikolay Date: Tue, 24 Mar 2020 18:28:49 +0300 Subject: [PATCH 2/2] CustomTab moved to LocalizableTab --- DotNetRu.Clients.UI/AppShell.xaml | 29 ++++++++--------- DotNetRu.Clients.UI/AppShell.xaml.cs | 12 +++---- DotNetRu.Clients.UI/CustomTab.cs | 14 --------- .../Localization/LocalizableTab.cs | 31 +++++++++++++++++++ 4 files changed, 50 insertions(+), 36 deletions(-) delete mode 100644 DotNetRu.Clients.UI/CustomTab.cs create mode 100644 DotNetRu.Clients.UI/Localization/LocalizableTab.cs diff --git a/DotNetRu.Clients.UI/AppShell.xaml b/DotNetRu.Clients.UI/AppShell.xaml index ca998287..d9bb5bd3 100644 --- a/DotNetRu.Clients.UI/AppShell.xaml +++ b/DotNetRu.Clients.UI/AppShell.xaml @@ -4,8 +4,9 @@ xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:about="clr-namespace:DotNetRu.Clients.UI.Pages.About;assembly=DotNetRu.Clients.UI" xmlns:home="clr-namespace:DotNetRu.Clients.UI.Pages.Home;assembly=DotNetRu.Clients.UI" + xmlns:localization="clr-namespace:DotNetRu.Clients.UI.Localization;assembly=DotNetRu.Clients.UI" xmlns:meetups="clr-namespace:DotNetRu.Clients.UI.Pages.Meetups;assembly=DotNetRu.Clients.UI" - xmlns:speakers="clr-namespace:DotNetRu.Clients.UI.Pages.Speakers;assembly=DotNetRu.Clients.UI" xmlns:ui="clr-namespace:DotNetRu.Clients.UI" + xmlns:speakers="clr-namespace:DotNetRu.Clients.UI.Pages.Speakers;assembly=DotNetRu.Clients.UI" FlyoutBehavior="Disabled"> @@ -24,34 +25,34 @@ - - + - - + - - + - - + - + \ No newline at end of file diff --git a/DotNetRu.Clients.UI/AppShell.xaml.cs b/DotNetRu.Clients.UI/AppShell.xaml.cs index 90ed81ec..fe5a1859 100644 --- a/DotNetRu.Clients.UI/AppShell.xaml.cs +++ b/DotNetRu.Clients.UI/AppShell.xaml.cs @@ -1,7 +1,9 @@ +using System.Linq; using DotNetRu.Clients.Portable.Helpers; -using DotNetRu.Clients.Portable.ViewModel; +using DotNetRu.Clients.UI.Localization; using DotNetRu.Utils.Helpers; using Xamarin.Forms; +using Xamarin.Forms.Internals; namespace DotNetRu.Clients.UI { @@ -26,13 +28,7 @@ public void UpdateTabBarLocalization() { Device.BeginInvokeOnMainThread(() => { - if (this.BindingContext is ViewModelBase viewModel) - { - this.NewsTab.Title = viewModel.Resources["News"] ?? this.NewsTab.Title; - this.SpeakersTab.Title = viewModel.Resources["Speakers"] ?? this.SpeakersTab.Title; - this.MeetupsTab.Title = viewModel.Resources["Meetups"] ?? this.MeetupsTab.Title; - this.AboutTab.Title = viewModel.Resources["About"] ?? this.AboutTab.Title; - } + this.TabBar.Items.Where(i => i is LocalizableTab).Cast().ForEach(x => x.Update()); }); } } diff --git a/DotNetRu.Clients.UI/CustomTab.cs b/DotNetRu.Clients.UI/CustomTab.cs deleted file mode 100644 index 6698c383..00000000 --- a/DotNetRu.Clients.UI/CustomTab.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System.Threading.Tasks; -using Xamarin.Forms; - -namespace DotNetRu.Clients.UI -{ - public class CustomTab : Tab - { - protected override Task OnPopAsync(bool animated) - { - // temporary workaround while https://github.com/xamarin/Xamarin.Forms/issues/8581 not fixed - return base.OnPopAsync(animated: false); - } - } -} diff --git a/DotNetRu.Clients.UI/Localization/LocalizableTab.cs b/DotNetRu.Clients.UI/Localization/LocalizableTab.cs new file mode 100644 index 00000000..855dbe28 --- /dev/null +++ b/DotNetRu.Clients.UI/Localization/LocalizableTab.cs @@ -0,0 +1,31 @@ +using System.Threading.Tasks; +using Xamarin.Forms; + +namespace DotNetRu.Clients.UI.Localization +{ + public class LocalizableTab : Tab + { + private string resourceName; + + public string ResourceName + { + get => this.resourceName; + set + { + this.resourceName = value; + this.Update(); + } + } + + public void Update() + { + this.Title = AppResources.ResourceManager.GetString(this.ResourceName); + } + + protected override Task OnPopAsync(bool animated) + { + // temporary workaround while https://github.com/xamarin/Xamarin.Forms/issues/8581 not fixed + return base.OnPopAsync(animated: false); + } + } +}