Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Add LocalizedString class for automatic ViewModel string updates #750

Merged
merged 23 commits into from
Jan 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
af6aaf1
Add LocalizedString
maxkoshevoi Jan 11, 2021
4805bf1
Update test name
maxkoshevoi Jan 11, 2021
b359d3f
Remove from NETSTANDARD1_0
maxkoshevoi Jan 11, 2021
ef7c9e0
LangVersion latest
maxkoshevoi Jan 11, 2021
e3707cb
Revert "LangVersion latest"
maxkoshevoi Jan 11, 2021
9c99f7d
Not using linq discard
maxkoshevoi Jan 11, 2021
9201937
Fix test
maxkoshevoi Jan 11, 2021
77c8a7b
Fix test. Add constructor with localizationManager
maxkoshevoi Jan 11, 2021
cf7ac1c
Add new feature to sample app
maxkoshevoi Jan 13, 2021
a67f822
Add Dispose() to unsubscribe from localizationManager.PropertyChanged
maxkoshevoi Jan 13, 2021
40a9390
Fix Sample localization resources generation
maxkoshevoi Jan 14, 2021
50f1779
Update sample app to change AppResources.Culture
maxkoshevoi Jan 14, 2021
1b508cf
Update LocalizedString to use ObservableObject
maxkoshevoi Jan 14, 2021
0017038
Return unsubscribing from localizationManager.PropertyChanged
maxkoshevoi Jan 14, 2021
e2b916c
Add WeakSubscribe
maxkoshevoi Jan 15, 2021
7575f02
Refactor LocalizedString to use WeakSubscribe
maxkoshevoi Jan 15, 2021
e8d769f
Add test to ensure instance of LocalizedString is disposed
maxkoshevoi Jan 15, 2021
29ef8d1
Dont create instance of LocalizationResourceManager
maxkoshevoi Jan 16, 2021
04be0c8
Add null checks to WeakSubscribe
maxkoshevoi Jan 16, 2021
02e9940
Invoke action with less code
maxkoshevoi Jan 16, 2021
c8eae99
Replace spaces with tabs
maxkoshevoi Jan 16, 2021
96b2769
Revert "Use Single for getting culture"
maxkoshevoi Jan 17, 2021
e4434f0
added one more unit test
pictos Jan 17, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions samples/XCT.Sample/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public partial class App : Forms.Application
public App()
{
On<Windows>().SetImageDirectory("Assets");

LocalizationResourceManager.Current.PropertyChanged += (sender, e) => AppResources.Culture = LocalizationResourceManager.Current.CurrentCulture;
LocalizationResourceManager.Current.Init(AppResources.ResourceManager);

InitializeComponent();
Expand Down
2 changes: 2 additions & 0 deletions samples/XCT.Sample/Pages/SettingPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@

<Button Text="{xct:Translate Save}"
Command="{Binding ChangeLanguageCommand, Mode=OneTime}"/>

<Label HorizontalTextAlignment="Center" Text="{Binding AppVersion.Localized}"/>
</StackLayout>
</pages:BasePage>
72 changes: 53 additions & 19 deletions samples/XCT.Sample/Resx/AppResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 0 additions & 74 deletions samples/XCT.Sample/Resx/AppResources.es.Designer.cs

This file was deleted.

5 changes: 4 additions & 1 deletion samples/XCT.Sample/Resx/AppResources.es.resx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Expand Down Expand Up @@ -129,4 +129,7 @@
<data name="Save" xml:space="preserve">
<value>Guardar</value>
</data>
<data name="Version" xml:space="preserve">
<value>Versión de la aplicación: {0}</value>
</data>
</root>
125 changes: 64 additions & 61 deletions samples/XCT.Sample/Resx/AppResources.resx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
<!--
Microsoft ResX Schema

Version 2.0
Expand Down Expand Up @@ -59,74 +59,77 @@
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="ChangeLanguage" xml:space="preserve">
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="ChangeLanguage" xml:space="preserve">
<value>Change language below, hit Save and see the texts in this page change. This will not affect the rest of the application, it just serves as a demo.</value>
</data>
<data name="English" xml:space="preserve">
<data name="English" xml:space="preserve">
<value>English</value>
</data>
<data name="Spanish" xml:space="preserve">
<data name="Spanish" xml:space="preserve">
<value>Spanish</value>
</data>
<data name="Save" xml:space="preserve">
<data name="Save" xml:space="preserve">
<value>Save</value>
</data>
<data name="Version" xml:space="preserve">
<value>App version: {0}</value>
</data>
</root>
3 changes: 3 additions & 0 deletions samples/XCT.Sample/ViewModels/SettingViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Xamarin.CommunityToolkit.Helpers;
using Xamarin.CommunityToolkit.Sample.Models;
using Xamarin.CommunityToolkit.Sample.Resx;
using Xamarin.Essentials;
using Xamarin.Forms;

namespace Xamarin.CommunityToolkit.Sample.ViewModels
Expand All @@ -29,6 +30,8 @@ public Language SelectedLanguage
set => SetProperty(ref selectedLanguage, value);
}

public LocalizedString AppVersion { get; } = new LocalizedString(() => string.Format(AppResources.Version, AppInfo.VersionString));

ICommand changeLanguageCommand;

public ICommand ChangeLanguageCommand => changeLanguageCommand ??= new Command(() =>
Expand Down
Loading