Skip to content

Commit

Permalink
Merge pull request #38 from Axemasta/feature/binding-extensions
Browse files Browse the repository at this point in the history
Add SetTranslation Binding Extension
  • Loading branch information
Axemasta authored Jul 23, 2024
2 parents 6c165fd + e4662b1 commit 1c85e70
Show file tree
Hide file tree
Showing 37 changed files with 915 additions and 207 deletions.
2 changes: 1 addition & 1 deletion directory.build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<PropertyGroup>
<MauiVersion>8.0.21</MauiVersion>
<MauiVersion>8.0.70</MauiVersion>

<StandardTargetFramework>net8.0</StandardTargetFramework>
<IosTargetFramework>net8.0-ios</IosTargetFramework>
Expand Down
24 changes: 12 additions & 12 deletions samples/Mocale.Samples/App.xaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
<?xml version="1.0" encoding="UTF-8" ?>
<Application
x:Class="Mocale.Samples.App"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
x:Class="Mocale.Samples.App"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
5 changes: 2 additions & 3 deletions samples/Mocale.Samples/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using Mocale.Samples.Views;
namespace Mocale.Samples;

public partial class App : Application
{
public App(IntroductionPage introductionPage)
public App()
{
InitializeComponent();

MainPage = new NavigationPage(introductionPage);
MainPage = new AppShell();
}
}
19 changes: 19 additions & 0 deletions samples/Mocale.Samples/AppShell.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8" ?>
<Shell
TabBarIsVisible="False"
x:Class="Mocale.Samples.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:keys="clr-namespace:Mocale.Translations"
xmlns:mocale="http://axemasta.com/schemas/2022/mocale"
xmlns:pages="clr-namespace:Mocale.Samples.Pages"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">


<FlyoutItem FlyoutDisplayOptions="AsMultipleItems">
<ShellContent ContentTemplate="{DataTemplate pages:IntroductionPage}" Title="{mocale:Localize Key={x:Static keys:TranslationKeys.IntroductionPageTitle}}" />

<ShellContent ContentTemplate="{DataTemplate pages:BindingPage}" Title="{mocale:Localize Key={x:Static keys:TranslationKeys.BindingPageTitle}}" />

<ShellContent ContentTemplate="{DataTemplate pages:ParameterPage}" Title="{mocale:Localize Key={x:Static keys:TranslationKeys.ParameterPageTitle}}" />
</FlyoutItem>
</Shell>
9 changes: 9 additions & 0 deletions samples/Mocale.Samples/AppShell.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Mocale.Samples;

public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
}
}
37 changes: 37 additions & 0 deletions samples/Mocale.Samples/Converters/CityDescriptionConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Globalization;

namespace Mocale.Samples.Converters;

internal sealed class CityDescriptionConverter : IValueConverter
{
private readonly ITranslatorManager translatorManager = MocaleLocator.TranslatorManager;

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
{
return null;
}

if (value is not string)
{
throw new InvalidOperationException($"Value must be of type {nameof(String)}");
}

var key = $"CityDescription_{value}";

//return new Binding
//{
// Mode = BindingMode.OneWay,
// Path = $"[{key}]",
// Source = translatorManager,
//};

return translatorManager.Translate(key);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
using System.Globalization;

namespace Mocale.Samples.Converter;

internal sealed class LanguageEmojiConverter : IValueConverter
{
private readonly ITranslatorManager translatorManager;

public LanguageEmojiConverter()
{
translatorManager = MocaleLocator.TranslatorManager;
}

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var currentCulture = translatorManager.CurrentCulture;

if (currentCulture is null)
{
return null;
}

return GetFlag(currentCulture.EnglishName);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}

private static string GetFlag(string country)
{
// Adapted from
// https://itnext.io/convert-country-name-to-flag-emoji-in-c-the-net-ecosystem-115f714d3ef9
var regions = CultureInfo.GetCultures(CultureTypes.SpecificCultures).ToList();
var englishRegion = regions.FirstOrDefault(region => region.EnglishName.Contains(country));

if (englishRegion == null)
{
return "🏳";
}

var region = new RegionInfo(englishRegion.LCID);

var countryAbbrev = region.TwoLetterISORegionName;
var flag = IsoCountryCodeToFlagEmoji(countryAbbrev);
return flag;
}

private static string IsoCountryCodeToFlagEmoji(string countryCode)
{
return string.Concat(countryCode.ToUpperInvariant().Select(x => char.ConvertFromUtf32(x + 0x1F1A5)));
}
}
using System.Globalization;

namespace Mocale.Samples.Converters;

internal sealed class LanguageEmojiConverter : IValueConverter
{
private readonly ITranslatorManager translatorManager;

public LanguageEmojiConverter()
{
translatorManager = MocaleLocator.TranslatorManager;
}

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var currentCulture = translatorManager.CurrentCulture;

if (currentCulture is null)
{
return null;
}

return GetFlag(currentCulture.EnglishName);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}

private static string GetFlag(string country)
{
// Adapted from
// https://itnext.io/convert-country-name-to-flag-emoji-in-c-the-net-ecosystem-115f714d3ef9
var regions = CultureInfo.GetCultures(CultureTypes.SpecificCultures).ToList();
var englishRegion = regions.FirstOrDefault(region => region.EnglishName.Contains(country));

if (englishRegion == null)
{
return "🏳";
}

var region = new RegionInfo(englishRegion.LCID);

var countryAbbrev = region.TwoLetterISORegionName;
var flag = IsoCountryCodeToFlagEmoji(countryAbbrev);
return flag;
}

private static string IsoCountryCodeToFlagEmoji(string countryCode)
{
return string.Concat(countryCode.ToUpperInvariant().Select(x => char.ConvertFromUtf32(x + 0x1F1A5)));
}
}
7 changes: 6 additions & 1 deletion samples/Mocale.Samples/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Mocale.Models;
using Mocale.Providers.GitHub.Raw;
using Mocale.Samples.ViewModels;
using Mocale.Samples.Views;
using Mocale.Samples.Pages;
namespace Mocale.Samples;

public static class MauiProgram
Expand Down Expand Up @@ -64,8 +64,13 @@ public static MauiApp CreateMauiApp()
#endif
});

builder.Services.AddTransient<BindingPage>();
builder.Services.AddTransient<IntroductionPage>();
builder.Services.AddTransient<ParameterPage>();

builder.Services.AddTransient<IntroductionPageViewModel>();
builder.Services.AddTransient<BindingViewModel>();
builder.Services.AddTransient<ParameterViewModel>();

return builder.Build();
}
Expand Down
17 changes: 5 additions & 12 deletions samples/Mocale.Samples/Mocale.Samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<RootNamespace>Mocale.Samples</RootNamespace>
<SingleProject>true</SingleProject>
<ImplicitUsings>true</ImplicitUsings>

<GenerateSatelliteAssembliesForCore>true</GenerateSatelliteAssembliesForCore>

<!-- Display name -->
Expand All @@ -29,15 +29,15 @@
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net7.0-ios|AnyCPU'">
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)'=='Debug|$(IosTargetFramework)'">
<CreatePackage>false</CreatePackage>
<CodesignProvision>Automatic</CodesignProvision>
<CodesignKey>iPhone Developer</CodesignKey>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net7.0-ios|AnyCPU'">
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)'=='Release|$(MacTargetFramework)'">
<CreatePackage>false</CreatePackage>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net7.0-maccatalyst|AnyCPU'">
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)'=='Debug|$(MacTargetFramework)'">
<CreatePackage>false</CreatePackage>
<CodesignKey>Mac Developer</CodesignKey>
<PackageSigningKey>3rd Party Mac Developer Installer</PackageSigningKey>
Expand All @@ -62,8 +62,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
<PackageReference Include="Refractored.MvvmHelpers" Version="1.6.2" />
</ItemGroup>

<ItemGroup>
Expand Down Expand Up @@ -91,13 +91,6 @@
</ItemGroup>

<ItemGroup>
<MauiXaml Update="Views\IntroductionPage.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
</ItemGroup>

<ItemGroup>
<Folder Include="Resources\Locales\" />
<EmbeddedResource Include="Resources\Locales\*.json" />
<AdditionalFiles Include="Resources\Locales\*.json" />
</ItemGroup>
Expand Down
Loading

0 comments on commit 1c85e70

Please sign in to comment.