Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pomianowski committed May 31, 2024
1 parent febee26 commit b00fcc6
Show file tree
Hide file tree
Showing 25 changed files with 160 additions and 1,055 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs

# Rider
.idea

# Mono auto generated files
mono_crash.*

Expand Down
2 changes: 1 addition & 1 deletion src/Lepo.i18n.DependencyInjection/StaticStringLocalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ILocalizationCultureManager cultureManager
/// </summary>
/// <param name="name">The name of the localized string.</param>
/// <returns>The localized string.</returns>
public LocalizedString this[string name] => this[name, Array.Empty<object>()];
public LocalizedString this[string name] => this[name, []];

/// <summary>
/// Gets the localized string for the specified name and format arguments.
Expand Down
54 changes: 41 additions & 13 deletions src/Lepo.i18n.Json/LocalizationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,29 @@ public static class LocalizationBuilderExtensions
Converters = { new TranslationsContainerConverter() }
};

public static LocalizationBuilder FromJsonString(
this LocalizationBuilder builder,
string jsonString,
CultureInfo culture
)
{
return builder.FromJsonString(jsonString, default, culture);
}

public static LocalizationBuilder FromJsonString(
this LocalizationBuilder builder,
string jsonString,
string? baseName,
CultureInfo culture
)
{
builder.AddLocalization(
new LocalizationSet(baseName, culture, ComputeLocalizationPairs(jsonString))
);

return builder;
}

/// <summary>
/// Loads localization data from a JSON file in the calling assembly.
/// </summary>
Expand Down Expand Up @@ -63,11 +86,24 @@ CultureInfo culture

string? contents = EmbeddedResourceReader.ReadToEnd(path, assembly);

builder.AddLocalization(
new LocalizationSet(
Path.GetFileNameWithoutExtension(path).Trim().ToLowerInvariant(),
culture,
ComputeLocalizationPairs(contents)
)
);

return builder;
}

private static IEnumerable<KeyValuePair<string, string?>> ComputeLocalizationPairs(
string? contents
)
{
if (contents is null)
{
throw new LocalizationBuilderException(
$"Resource {path} not found in assembly {assembly.FullName}."
);
throw new ArgumentNullException(nameof(contents));
}

Version schemaVersion =
Expand Down Expand Up @@ -101,21 +137,13 @@ CultureInfo culture
if (localizedStrings.ContainsKey(localizedString.Name))
{
throw new LocalizationBuilderException(
$"The {path} file contains duplicate \"{localizedString.Name}\" keys."
$"The contents of the JSON file contains duplicate \"{localizedString.Name}\" keys."
);
}

localizedStrings.Add(localizedString.Name, localizedString.Value);
}

builder.AddLocalization(
new LocalizationSet(
Path.GetFileNameWithoutExtension(path).Trim().ToLowerInvariant(),
culture,
localizedStrings!
)
);

return builder;
return localizedStrings!;
}
}
2 changes: 1 addition & 1 deletion src/Lepo.i18n.Wpf/StringLocalizerExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public StringLocalizerExtension(string? text, string? textNamespace)
?.GetLocalizationSet(
LocalizationProviderFactory.GetInstance()?.GetCulture()
?? CultureInfo.CurrentUICulture,
Namespace ?? default
Namespace?.ToLowerInvariant() ?? default
);

if (localizationSet is null)
Expand Down
12 changes: 8 additions & 4 deletions src/Lepo.i18n/LocalizationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,9 @@ CultureInfo culture
.Where(x => x.Key is string)
.ToDictionary(x => (string)x.Key!, x => (string?)x.Value);

builder.AddLocalization(new LocalizationSet(baseName, culture, localizations));

Thread.CurrentThread.CurrentCulture = cultureToRestore;
Thread.CurrentThread.CurrentUICulture = cultureToRestore;
builder.AddLocalization(
new LocalizationSet(baseName.ToLowerInvariant(), culture, localizations)
);

return builder;
}
Expand All @@ -194,5 +193,10 @@ CultureInfo culture
ex
);
}
finally
{
Thread.CurrentThread.CurrentCulture = cultureToRestore;
Thread.CurrentThread.CurrentUICulture = cultureToRestore;
}
}
}
19 changes: 18 additions & 1 deletion src/Lepo.i18n/LocalizationSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,21 @@ public record LocalizationSet(
string? Name,
CultureInfo Culture,
IEnumerable<KeyValuePair<string, string?>> Strings
);
)
{
public string? this[string key]
{
get
{
foreach (KeyValuePair<string, string?> localizationString in Strings)
{
if (localizationString.Key == key)
{
return localizationString.Value;
}
}

return key;
}
}
}
1 change: 1 addition & 0 deletions src/Lepo.i18n/Translator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static class Translator
[Obsolete("This method is obsolete and should not be used.")]
public static string String(string value)
{
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
if (value is null)
{
return string.Empty;
Expand Down
4 changes: 3 additions & 1 deletion src/Lepo.i18n/Yaml/LocalizationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ KeyValuePair<string, IDictionary<string, string>> localizedStrings in deserializ
? baseNamespace
: localizedStrings.Key;

builder.AddLocalization(new LocalizationSet(name, culture, localizedStrings.Value!));
builder.AddLocalization(
new LocalizationSet(name.ToLowerInvariant(), culture, localizedStrings.Value!)
);
}

return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
<None Remove="Resources\Translations-pl-PL.yaml" />
<None Remove="Resources\Translations-en-US.yaml" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Resources\Translations-pl-PL.yaml" />
<EmbeddedResource Include="Resources\Translations-en-US.yaml" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" />
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit b00fcc6

Please sign in to comment.