Skip to content

Commit 4a35d0a

Browse files
authored
Static localization example (#34)
* Update README.md * Revert "Update README.md" This reverts commit 9916be6. * made static localization of two words (new connection and disconnect) IMPORTANT it works only in bin directory for some reasons right now * a little fix of appsettings.json Now translating works in visual studio also * a few fixes * left corrections are done. But app doesnt work
1 parent f294557 commit 4a35d0a

10 files changed

Lines changed: 154 additions & 32 deletions

File tree

SpoClient.Localization/Resources/Strings.Designer.cs

Lines changed: 36 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SpoClient.Localization/Resources/Strings.ja.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@
117117
<resheader name="writer">
118118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120+
<data name="Disconnect" xml:space="preserve">
121+
<value>接続切断</value>
122+
</data>
120123
<data name="Edit" xml:space="preserve">
121124
<value>編集</value>
122125
</data>
@@ -129,6 +132,9 @@
129132
<data name="LanguageMenuHeader" xml:space="preserve">
130133
<value>言語</value>
131134
</data>
135+
<data name="NewConnection" xml:space="preserve">
136+
<value>新規接続</value>
137+
</data>
132138
<data name="SystemLanguageMenuHeader" xml:space="preserve">
133139
<value>システム</value>
134140
</data>

SpoClient.Localization/Resources/Strings.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@
117117
<resheader name="writer">
118118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120+
<data name="Disconnect" xml:space="preserve">
121+
<value>Disconnect</value>
122+
</data>
120123
<data name="Edit" xml:space="preserve">
121124
<value>Edit</value>
122125
</data>
@@ -129,6 +132,9 @@
129132
<data name="LanguageMenuHeader" xml:space="preserve">
130133
<value>Language</value>
131134
</data>
135+
<data name="NewConnection" xml:space="preserve">
136+
<value>New connection</value>
137+
</data>
132138
<data name="SystemLanguageMenuHeader" xml:space="preserve">
133139
<value>System</value>
134140
</data>

SpoClient.Localization/SpoClient.Localization.csproj

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,13 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<Compile Update="Resources\Strings.Designer.cs">
11-
<DesignTime>True</DesignTime>
12-
<AutoGen>True</AutoGen>
13-
<DependentUpon>Strings.resx</DependentUpon>
14-
</Compile>
15-
</ItemGroup>
16-
17-
<ItemGroup>
10+
<EmbeddedResource Update="Resources\Strings.ja.resx">
11+
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
12+
</EmbeddedResource>
1813
<EmbeddedResource Update="Resources\Strings.resx">
1914
<Generator>ResXFileCodeGenerator</Generator>
2015
<LastGenOutput>Strings.Designer.cs</LastGenOutput>
16+
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
2117
</EmbeddedResource>
2218
</ItemGroup>
2319

spoclient/App.axaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Globalization;
12
using Avalonia;
23
using Avalonia.Controls;
34
using Avalonia.Markup.Xaml;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.IO;
3+
using Newtonsoft.Json;
4+
5+
public static class StaticLocalizationSettings
6+
{
7+
private static readonly string _settingsFilePath = "appsettings.json";
8+
9+
public static string GetCulture()
10+
{
11+
if (File.Exists(_settingsFilePath))
12+
{
13+
var json = File.ReadAllText(_settingsFilePath);
14+
var settings = JsonConvert.DeserializeObject<Settings>(json);
15+
return settings.Localization.Culture;
16+
}
17+
return "en-US"; // Default culture
18+
}
19+
20+
public static void SetCulture(string culture)
21+
{
22+
23+
try
24+
{
25+
var settings = new Settings { Localization = new LocalizationSettings { Culture = culture } };
26+
var json = JsonConvert.SerializeObject(settings, Formatting.Indented);
27+
if (File.Exists(_settingsFilePath))
28+
{
29+
File.Delete(_settingsFilePath);
30+
}
31+
32+
File.WriteAllText(_settingsFilePath, json);
33+
}
34+
catch (IOException ex)
35+
{
36+
Console.WriteLine($"Error writtig: {ex.Message}");
37+
}
38+
catch (UnauthorizedAccessException ex)
39+
{
40+
Console.WriteLine($"Error of acces: {ex.Message}");
41+
}
42+
}
43+
private class Settings
44+
{
45+
public LocalizationSettings Localization { get; set; }
46+
}
47+
private class LocalizationSettings
48+
{
49+
public string Culture { get; set; }
50+
}
51+
}
52+
53+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Avalonia.Markup.Xaml;
2+
using LocalizationManager;
3+
using System;
4+
using Prism.Ioc;
5+
namespace SpoClient.Localization
6+
{
7+
8+
9+
public class TranslateExtension : MarkupExtension
10+
{
11+
public string Key { get; set; }
12+
13+
public TranslateExtension()
14+
{
15+
}
16+
17+
public TranslateExtension(string key)
18+
{
19+
Key = key;
20+
}
21+
22+
public override object ProvideValue(IServiceProvider serviceProvider)
23+
{
24+
var localizationManager = ContainerLocator.Container.Resolve<ILocalizationManager>();
25+
return localizationManager.GetValue(Key, "UI");
26+
}
27+
}
28+
29+
}

spoclient/ViewModels/MainViewViewModel.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
using LocalizationManager.Avalonia;
1313
using System.Globalization;
1414
using SpoClient.Setting.Models;
15+
using SpoClient.Localization;
16+
using System.Configuration;
1517

1618
namespace spoclient.ViewModels
1719
{
@@ -83,6 +85,7 @@ public int? SelectedIndex
8385
/// </summary>
8486
private readonly IContainer container;
8587

88+
private readonly IAppSettings appSettings;
8689

8790
/// <summary>
8891
/// タブとビューモデルのマッチャー
@@ -95,12 +98,12 @@ public int? SelectedIndex
9598
/// </summary>
9699
/// <param name="dialogService">ダイアログサービス</param>
97100
/// <param name="container">コンテナサービス</param>
98-
public MainViewViewModel(IDialogService dialogService, ILocalizationManager localization, IContainer container)
101+
public MainViewViewModel(IDialogService dialogService, ILocalizationManager localization, IContainer container, IAppSettings appSettings)
99102
{
100103
this.dialogService = dialogService;
101104
this.localization = localization;
102105
this.container = container;
103-
106+
this.appSettings = appSettings;
104107
NavigationFactory = new NavigationFactory(this);
105108

106109
// タブとビューモデルのマッチャーを設定
@@ -138,9 +141,14 @@ public MainViewViewModel(IDialogService dialogService, ILocalizationManager loca
138141
public DelegateCommand ChangeLanguageEnglish => new(() =>
139142
{
140143
localization.CurrentCulture = new CultureInfo("en-US");
144+
appSettings[AppSettingKeys.Culture] = "en-US";
141145
});
142146

143-
147+
public DelegateCommand ChangeLanguageJapanese => new(() =>
148+
{
149+
localization.CurrentCulture = new CultureInfo("ja-JP");
150+
appSettings[AppSettingKeys.Culture] = "ja-JP";
151+
});
144152
/// <summary>
145153
/// タブを閉じる
146154
/// </summary>

spoclient/Views/MainView.axaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
xmlns:pages="using:spoclient.Pages"
99
xmlns:models="using:spoclient.Models"
1010
xmlns:vm="using:spoclient.ViewModels"
11+
xmlns:l10n="clr-namespace:SpoClient.Localization"
1112
mc:Ignorable="d" d:DesignWidth="600" d:DesignHeight="450"
1213
x:Class="spoclient.Views.MainView"
1314
x:DataType="vm:MainViewViewModel">
@@ -60,7 +61,7 @@
6061
<MenuItem Header="{Binding SystemLanguageMenuHeader, FallbackValue=System}" />
6162
<Separator />
6263
<MenuItem Header="English" Command="{Binding ChangeLanguageEnglish}" />
63-
<MenuItem Header="日本語" />
64+
<MenuItem Header="日本語" Command="{Binding ChangeLanguageJapanese}" />
6465
</MenuItem>
6566
</MenuItem>
6667
<MenuItem Header="{Binding HelpMenuHeader, FallbackValue=Help}">
@@ -81,8 +82,8 @@
8182
IsSettingsVisible="True">
8283

8384
<ui:NavigationView.MenuItems>
84-
<ui:NavigationViewItem Content="新規接続" Tag="NewConnection" IconSource="CloudFilled" />
85-
<ui:NavigationViewItem Content="接続切断" Tag="Disconnect" IconSource="CloudOffline" />
85+
<ui:NavigationViewItem Content="{l10n:Translate NewConnection}" Tag="NewConnection" IconSource="CloudFilled" />
86+
<ui:NavigationViewItem Content="{l10n:Translate Disconnect}" Tag="Disconnect" IconSource="CloudOffline" />
8687

8788
<ui:NavigationViewItem Content="パスワード変更" Tag="Passwd" IconSource="Key" />
8889

spoclient/spoclient.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<PackageReference Include="MessageBox.Avalonia" Version="3.2.0" />
4848
<PackageReference Include="MessageBox.Avalonia.Markdown" Version="3.2.0" />
4949
<PackageReference Include="Microlithix.AnsiParser" Version="1.0.0" />
50+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
5051
<PackageReference Include="Prism.Avalonia" Version="8.1.97.11073" />
5152
<PackageReference Include="Prism.DryIoc.Avalonia" Version="8.1.97.11073" />
5253
<PackageReference Include="SSH.NET" Version="2024.2.0" />
@@ -59,4 +60,7 @@
5960
<ProjectReference Include="..\SpoClient.Setting\SpoClient.Setting.csproj" />
6061
<ProjectReference Include="..\VtNetCore.Avalonia\VtNetCore.Avalonia.csproj" />
6162
</ItemGroup>
63+
<ItemGroup>
64+
<InternalsVisibleTo Include="..\SpoClient.Localization\SpoClient.Localization.csproj" />
65+
</ItemGroup>
6266
</Project>

0 commit comments

Comments
 (0)