Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

language idea #1792

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
29 changes: 29 additions & 0 deletions starsky/starsky.feature.language/CustomCultureComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace starsky.feature.language;

public class CustomCultureComparer : IComparer<string>
{
private readonly List<string> _customOrder;

public CustomCultureComparer(List<string> customOrder)
{
_customOrder = customOrder;
}

public int Compare(string? x, string? y)
{
var indexX = _customOrder.IndexOf(x);
var indexY = _customOrder.IndexOf(y);

if ( indexX == -1 )
{
indexX = int.MaxValue;
}

if ( indexY == -1 )
{
indexY = int.MaxValue;
}

return indexX.CompareTo(indexY);
}
}
27 changes: 27 additions & 0 deletions starsky/starsky.feature.language/GetAllCultures.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Collections.Concurrent;
using System.Globalization;
using System.Resources;

namespace starsky.feature.language;

public static class GetAllCultures
{
private static readonly ConcurrentDictionary<Type, List<CultureInfo>>
ResourceCultures = new();

/// <summary>
/// Return the list of cultures that is supported by a Resource Assembly (usually collection of
/// resx files).
/// </summary>
public static List<CultureInfo> CulturesOfResource<T>()
{
return ResourceCultures.GetOrAdd(typeof(T), t =>
{
var manager = new ResourceManager(t);
return CultureInfo.GetCultures(CultureTypes.AllCultures)
.Where(c => !c.Equals(CultureInfo.InvariantCulture) &&
manager.GetResourceSet(c, true, false) != null)
.ToList();
});
}
}
40 changes: 40 additions & 0 deletions starsky/starsky.feature.language/JsonToResxConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Resources.NetStandard;
using System.Text.Json;

public static class JsonToResxConverter
{
public static void ConvertJsonToResx(string jsonFilePath, string resxFilePath)
{
// Read and parse the JSON file
var jsonString = File.ReadAllText(jsonFilePath);
var translations =
JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(jsonString);

var languages = new List<string> {string.Empty, "nl", "en", "de" };

foreach ( var language in languages )
{
// Create a .resx file and write the translations
var path = resxFilePath;
if ( language != string.Empty )
{
path = resxFilePath.Replace(".resx", $".{language}.resx");
}

Console.WriteLine(path);
using ( var resxWriter = new ResXResourceWriter(path) )
{
foreach ( var entry in translations )
{
var value = entry.Value["en"];
if ( language != string.Empty )
{
value = entry.Value[language];
}

resxWriter.AddResource(entry.Key, value);
}
}
}
}
}
Loading
Loading