Skip to content

Commit 0b6b7ca

Browse files
committed
📄 Update copyright year #10
1 parent 9408239 commit 0b6b7ca

7 files changed

+201
-206
lines changed

LICENSE.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
MIT License
22
===========
33

4-
Copyright © 2024, Svante Seleborg
5-
---------------------------------
4+
Copyright © 2022-2025 Svante Seleborg
5+
-------------------------------------
66

77
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
88
associated documentation files (the “Software”), to deal in the Software without restriction,

src/Xecrets.Localization/ITranslationsProvider.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#region Copyright and License
22

33
/*
4-
* Xecrets Ez - Copyright © 2022-2024, Svante Seleborg, All Rights Reserved.
4+
* Xecrets Ez - Copyright © 2022-2025 Svante Seleborg, All Rights Reserved.
55
*
66
* This code file is part of Xecrets Ez - A cross platform desktop application
77
* for encryption, decryption and other file operations based on Xecrets Cli.
@@ -34,17 +34,16 @@
3434

3535
using Karambolo.PO;
3636

37-
namespace Xecrets.Localization
37+
namespace Xecrets.Localization;
38+
39+
/// <summary>
40+
/// Provides translations from embedded .po files.
41+
/// </summary>
42+
public interface ITranslationsProvider
3843
{
3944
/// <summary>
40-
/// Provides translations from embedded .po files.
45+
/// Gets the catalogs of translations.
4146
/// </summary>
42-
public interface ITranslationsProvider
43-
{
44-
/// <summary>
45-
/// Gets the catalogs of translations.
46-
/// </summary>
47-
/// <returns></returns>
48-
IReadOnlyDictionary<string, POCatalog> GetCatalogs();
49-
}
47+
/// <returns></returns>
48+
IReadOnlyDictionary<string, POCatalog> GetCatalogs();
5049
}
Lines changed: 100 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#region Copyright and License
22

33
/*
4-
* Xecrets Ez - Copyright © 2022-2024, Svante Seleborg, All Rights Reserved.
4+
* Xecrets Ez - Copyright © 2022-2025 Svante Seleborg, All Rights Reserved.
55
*
66
* This code file is part of Xecrets Ez - A cross platform desktop application
77
* for encryption, decryption and other file operations based on Xecrets Cli.
@@ -38,138 +38,137 @@
3838

3939
using System.Globalization;
4040

41-
namespace Xecrets.Localization
41+
namespace Xecrets.Localization;
42+
43+
/// <summary>
44+
/// Localizes strings using a <see cref="ITranslationsProvider"/>.
45+
/// </summary>
46+
/// <param name="translationsProvider"></param>
47+
/// <param name="location"></param>
48+
/// <param name="culture"></param>
49+
public sealed class POStringLocalizer(ITranslationsProvider translationsProvider, string location, CultureInfo? culture = null) : IStringLocalizer
4250
{
51+
private readonly CultureInfo _culture = culture ?? CultureInfo.CurrentUICulture;
52+
4353
/// <summary>
44-
/// Localizes strings using a <see cref="ITranslationsProvider"/>.
54+
/// Translates a string by name, falling back to the name if no translation is found.
4555
/// </summary>
46-
/// <param name="translationsProvider"></param>
47-
/// <param name="location"></param>
48-
/// <param name="culture"></param>
49-
public sealed class POStringLocalizer(ITranslationsProvider translationsProvider, string location, CultureInfo? culture = null) : IStringLocalizer
56+
/// <param name="name">The string to translate, typically the english original.</param>
57+
/// <returns>A translated string.</returns>
58+
public LocalizedString this[string name]
5059
{
51-
private readonly CultureInfo _culture = culture ?? CultureInfo.CurrentUICulture;
52-
53-
/// <summary>
54-
/// Translates a string by name, falling back to the name if no translation is found.
55-
/// </summary>
56-
/// <param name="name">The string to translate, typically the english original.</param>
57-
/// <returns>A translated string.</returns>
58-
public LocalizedString this[string name]
60+
get
5961
{
60-
get
61-
{
62-
bool resourceNotFound = !TryGetTranslation(name, out var searchedLocation, out var value);
63-
return new LocalizedString(name, value!, resourceNotFound, searchedLocation);
64-
}
62+
bool resourceNotFound = !TryGetTranslation(name, out var searchedLocation, out var value);
63+
return new LocalizedString(name, value!, resourceNotFound, searchedLocation);
6564
}
65+
}
6666

67-
/// <summary>
68-
/// Translates a string by name, falling back to the name if no translation is found.
69-
/// Also formats the string with the provided arguments.
70-
/// </summary>
71-
/// <param name="name">The string to translate, typically the english original.</param>
72-
/// <param name="arguments">Arguments passed to string.Format()</param>
73-
/// <returns>A translated string.</returns>
74-
public LocalizedString this[string name, params object[] arguments]
67+
/// <summary>
68+
/// Translates a string by name, falling back to the name if no translation is found.
69+
/// Also formats the string with the provided arguments.
70+
/// </summary>
71+
/// <param name="name">The string to translate, typically the english original.</param>
72+
/// <param name="arguments">Arguments passed to string.Format()</param>
73+
/// <returns>A translated string.</returns>
74+
public LocalizedString this[string name, params object[] arguments]
75+
{
76+
get
7577
{
76-
get
77-
{
78-
bool resourceNotFound = !TryGetTranslation(name, out string searchedLocation, out string value);
79-
return new LocalizedString(name, string.Format(value!, arguments), resourceNotFound, searchedLocation);
80-
}
78+
bool resourceNotFound = !TryGetTranslation(name, out string searchedLocation, out string value);
79+
return new LocalizedString(name, string.Format(value!, arguments), resourceNotFound, searchedLocation);
8180
}
81+
}
8282

83-
/// <summary>
84-
/// Try to get a translation for a given name.
85-
/// </summary>
86-
/// <param name="name">The name of the string to get.</param>
87-
/// <param name="searchedLocation">Where we're looking for the translation.</param>
88-
/// <param name="value">The translated value, or falls back to name.</param>
89-
/// <returns>true if a translation was found.</returns>
90-
public bool TryGetTranslation(string name, out string searchedLocation, out string value)
83+
/// <summary>
84+
/// Try to get a translation for a given name.
85+
/// </summary>
86+
/// <param name="name">The name of the string to get.</param>
87+
/// <param name="searchedLocation">Where we're looking for the translation.</param>
88+
/// <param name="value">The translated value, or falls back to name.</param>
89+
/// <returns>true if a translation was found.</returns>
90+
public bool TryGetTranslation(string name, out string searchedLocation, out string value)
91+
{
92+
bool wasFound = TryInternal(location, name, out searchedLocation, out value);
93+
value = value.Replace(@"\n", Environment.NewLine);
94+
return wasFound;
95+
}
96+
97+
private bool TryInternal(string location, string name, out string searchedLocation, out string value)
98+
{
99+
searchedLocation = location;
100+
101+
IReadOnlyDictionary<string, POCatalog> catalogs = translationsProvider.GetCatalogs();
102+
POCatalog? catalog = GetCatalog(catalogs);
103+
if (catalog == null)
91104
{
92-
bool wasFound = TryInternal(location, name, out searchedLocation, out value);
93-
value = value.Replace(@"\n", Environment.NewLine);
94-
return wasFound;
105+
value = name;
106+
return false;
95107
}
96108

97-
private bool TryInternal(string location, string name, out string searchedLocation, out string value)
109+
POKey key = new(name.Replace("\r\n", "\n"));
110+
string? translation = catalog.GetTranslation(key);
111+
if (translation == null)
98112
{
99-
searchedLocation = location;
113+
catalogs.TryGetValue("en-US", out POCatalog? englishCatalog);
114+
translation = englishCatalog?.GetTranslation(key) ?? key.Id;
115+
}
100116

101-
IReadOnlyDictionary<string, POCatalog> catalogs = translationsProvider.GetCatalogs();
102-
POCatalog? catalog = GetCatalog(catalogs);
103-
if (catalog == null)
117+
value = translation;
118+
return value != key.Id;
119+
}
120+
121+
private POCatalog? GetCatalog(IReadOnlyDictionary<string, POCatalog> catalogs)
122+
{
123+
CultureInfo culture = _culture;
124+
while (true)
125+
{
126+
if (catalogs.TryGetValue(culture.Name, out POCatalog? catalog))
104127
{
105-
value = name;
106-
return false;
128+
return catalog;
107129
}
108130

109-
POKey key = new(name.Replace("\r\n", "\n"));
110-
string? translation = catalog.GetTranslation(key);
111-
if (translation == null)
131+
CultureInfo parentCulture = culture.Parent;
132+
if (culture == parentCulture)
112133
{
113134
catalogs.TryGetValue("en-US", out POCatalog? englishCatalog);
114-
translation = englishCatalog?.GetTranslation(key) ?? key.Id;
135+
return englishCatalog;
115136
}
116137

117-
value = translation;
118-
return value != key.Id;
119-
}
120-
121-
private POCatalog? GetCatalog(IReadOnlyDictionary<string, POCatalog> catalogs)
122-
{
123-
CultureInfo culture = _culture;
124-
while (true)
125-
{
126-
if (catalogs.TryGetValue(culture.Name, out POCatalog? catalog))
127-
{
128-
return catalog;
129-
}
130-
131-
CultureInfo parentCulture = culture.Parent;
132-
if (culture == parentCulture)
133-
{
134-
catalogs.TryGetValue("en-US", out POCatalog? englishCatalog);
135-
return englishCatalog;
136-
}
137-
138-
culture = parentCulture;
139-
}
138+
culture = parentCulture;
140139
}
140+
}
141141

142-
/// <summary>
143-
/// Get all strings for the current culture.
144-
/// </summary>
145-
/// <param name="includeParentCultures"></param>
146-
/// <returns></returns>
147-
public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures)
142+
/// <summary>
143+
/// Get all strings for the current culture.
144+
/// </summary>
145+
/// <param name="includeParentCultures"></param>
146+
/// <returns></returns>
147+
public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures)
148+
{
149+
IReadOnlyDictionary<string, POCatalog> catalogs = translationsProvider.GetCatalogs();
150+
CultureInfo culture = _culture;
151+
do
148152
{
149-
IReadOnlyDictionary<string, POCatalog> catalogs = translationsProvider.GetCatalogs();
150-
CultureInfo culture = _culture;
151-
do
153+
if (catalogs.TryGetValue(culture.Name, out var catalog))
152154
{
153-
if (catalogs.TryGetValue(culture.Name, out var catalog))
155+
foreach (IPOEntry? entry in catalog)
154156
{
155-
foreach (IPOEntry? entry in catalog)
157+
if (entry.Count > 0)
156158
{
157-
if (entry.Count > 0)
158-
{
159-
yield return new LocalizedString(entry.Key.Id, entry[0], resourceNotFound: false, location);
160-
}
159+
yield return new LocalizedString(entry.Key.Id, entry[0], resourceNotFound: false, location);
161160
}
162161
}
162+
}
163163

164-
CultureInfo parentCulture = culture.Parent;
165-
if (culture == parentCulture)
166-
{
167-
break;
168-
}
169-
170-
culture = parentCulture;
164+
CultureInfo parentCulture = culture.Parent;
165+
if (culture == parentCulture)
166+
{
167+
break;
171168
}
172-
while (includeParentCultures);
169+
170+
culture = parentCulture;
173171
}
172+
while (includeParentCultures);
174173
}
175174
}

src/Xecrets.Localization/POStringLocalizerFactory.cs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#region Copyright and License
22

33
/*
4-
* Xecrets Ez - Copyright © 2022-2024, Svante Seleborg, All Rights Reserved.
4+
* Xecrets Ez - Copyright © 2022-2025 Svante Seleborg, All Rights Reserved.
55
*
66
* This code file is part of Xecrets Ez - A cross platform desktop application
77
* for encryption, decryption and other file operations based on Xecrets Cli.
@@ -34,31 +34,30 @@
3434

3535
using Microsoft.Extensions.Localization;
3636

37-
namespace Xecrets.Localization
37+
namespace Xecrets.Localization;
38+
39+
/// <summary>
40+
/// Factory for creating <see cref="POStringLocalizer"/> instances.
41+
/// </summary>
42+
/// <param name="translationsProvider">An implementation of <see cref="ITranslationsProvider"/></param>
43+
public sealed class POStringLocalizerFactory(ITranslationsProvider translationsProvider) : IStringLocalizerFactory
3844
{
3945
/// <summary>
40-
/// Factory for creating <see cref="POStringLocalizer"/> instances.
46+
/// Creates an instance of <see cref="POStringLocalizer"/>.
4147
/// </summary>
42-
/// <param name="translationsProvider">An implementation of <see cref="ITranslationsProvider"/></param>
43-
public sealed class POStringLocalizerFactory(ITranslationsProvider translationsProvider) : IStringLocalizerFactory
44-
{
45-
/// <summary>
46-
/// Creates an instance of <see cref="POStringLocalizer"/>.
47-
/// </summary>
48-
/// <param name="baseName"></param>
49-
/// <param name="location"></param>
50-
/// <returns></returns>
51-
public IStringLocalizer Create(string baseName, string location) => new POStringLocalizer(translationsProvider, location);
48+
/// <param name="baseName"></param>
49+
/// <param name="location"></param>
50+
/// <returns></returns>
51+
public IStringLocalizer Create(string baseName, string location) => new POStringLocalizer(translationsProvider, location);
5252

53-
/// <summary>
54-
/// Creates an instance of <see cref="POStringLocalizer"/>. Not implemented.
55-
/// </summary>
56-
/// <param name="resourceSource"></param>
57-
/// <returns></returns>
58-
/// <exception cref="NotImplementedException"></exception>
59-
public IStringLocalizer Create(Type resourceSource)
60-
{
61-
throw new NotImplementedException();
62-
}
53+
/// <summary>
54+
/// Creates an instance of <see cref="POStringLocalizer"/>. Not implemented.
55+
/// </summary>
56+
/// <param name="resourceSource"></param>
57+
/// <returns></returns>
58+
/// <exception cref="NotImplementedException"></exception>
59+
public IStringLocalizer Create(Type resourceSource)
60+
{
61+
throw new NotImplementedException();
6362
}
6463
}

0 commit comments

Comments
 (0)