Skip to content

Commit

Permalink
Fix: localize TabBar by settings #286
Browse files Browse the repository at this point in the history
Fix: localize TabBar by settings
  • Loading branch information
pfedotovsky authored Mar 28, 2020
2 parents 0f1fb7f + 708ac60 commit 65c2ffa
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 24 deletions.
25 changes: 15 additions & 10 deletions DotNetRu.Clients.UI/AppShell.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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">

<Shell.Resources>
Expand All @@ -24,30 +25,34 @@
</ResourceDictionary>
</Shell.Resources>

<TabBar Route="root">
<ui:CustomTab
<TabBar x:Name="TabBar" Route="root">
<localization:LocalizableTab
Title="{Binding Resources[News]}"
Icon="tab_feed.png"
ResourceName="News"
Route="news">
<ShellContent ContentTemplate="{DataTemplate home:NewsPage}" />
</ui:CustomTab>
<ui:CustomTab
</localization:LocalizableTab>
<localization:LocalizableTab
Title="{Binding Resources[Speakers]}"
Icon="tab_speakers.png"
ResourceName="Speakers"
Route="speakers">
<ShellContent ContentTemplate="{DataTemplate speakers:SpeakersPage}" />
</ui:CustomTab>
<ui:CustomTab
</localization:LocalizableTab>
<localization:LocalizableTab
Title="{Binding Resources[Meetups]}"
Icon="tab_events.png"
ResourceName="Meetups"
Route="meetups">
<ShellContent ContentTemplate="{DataTemplate meetups:MeetupsPage}" />
</ui:CustomTab>
<ui:CustomTab
</localization:LocalizableTab>
<localization:LocalizableTab
Title="{Binding Resources[About]}"
Icon="tab_about.png"
ResourceName="About"
Route="about">
<ShellContent ContentTemplate="{DataTemplate about:AboutPage}" />
</ui:CustomTab>
</localization:LocalizableTab>
</TabBar>
</Shell>
15 changes: 15 additions & 0 deletions DotNetRu.Clients.UI/AppShell.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
using System.Linq;
using DotNetRu.Clients.Portable.Helpers;
using DotNetRu.Clients.UI.Localization;
using DotNetRu.Utils.Helpers;
using Xamarin.Forms;
using Xamarin.Forms.Internals;

namespace DotNetRu.Clients.UI
{
Expand All @@ -15,6 +20,16 @@ public AppShell()

// Can't set BackgrounColor as StaticResource, see https://github.com/xamarin/Xamarin.Forms/issues/7055
SetBackgroundColor(this, backgroundColor);

MessagingCenter.Subscribe<LocalizedResources>(this, MessageKeys.LanguageChanged, sender => this.UpdateTabBarLocalization());
}

public void UpdateTabBarLocalization()
{
Device.BeginInvokeOnMainThread(() =>
{
this.TabBar.Items.Where(i => i is LocalizableTab).Cast<LocalizableTab>().ForEach(x => x.Update());
});
}
}
}
14 changes: 0 additions & 14 deletions DotNetRu.Clients.UI/CustomTab.cs

This file was deleted.

31 changes: 31 additions & 0 deletions DotNetRu.Clients.UI/Localization/LocalizableTab.cs
Original file line number Diff line number Diff line change
@@ -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<Page> OnPopAsync(bool animated)
{
// temporary workaround while https://github.com/xamarin/Xamarin.Forms/issues/8581 not fixed
return base.OnPopAsync(animated: false);
}
}
}

0 comments on commit 65c2ffa

Please sign in to comment.