Add ability to fetch translations for a given language

This commit is contained in:
jvyden 2022-04-13 18:58:07 -04:00
parent 25731dd84b
commit a340a77d74
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
18 changed files with 86 additions and 35 deletions

View file

@ -1,17 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>LBPUnion.ProjectLighthouse.Localisation</RootNamespace>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Update="BaseLayout.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>BaseLayout.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
</Project>

View file

@ -0,0 +1,36 @@
using System.Diagnostics;
using System.Reflection;
using System.Resources;
namespace LBPUnion.ProjectLighthouse.Localization;
public static class LocalizationManager
{
private static readonly string namespaceStr = typeof(LocalizationManager).Namespace ?? "";
public static string GetLocalizedString(TranslationAreas translationArea, string language, string key)
{
#if DEBUG
Console.WriteLine($"Attempting to load '{key}' for '{language}' ");
#endif
string resourceBasename = $"{namespaceStr}.{translationArea.ToString()}.{language}";
ResourceManager resourceManager = new(resourceBasename, Assembly.GetExecutingAssembly());
string? localizedString = resourceManager.GetString(key);
if (localizedString == null)
{
#if DEBUG
if (Debugger.IsAttached) Debugger.Break();
#endif
return $"{translationArea.ToString()}.{language}.{key}";
}
return localizedString;
}
public static IEnumerable<string> GetAvailableLanguages(TranslationAreas translationArea)
{
return Assembly.GetExecutingAssembly().GetManifestResourceNames().Where(r => r.StartsWith($"{namespaceStr}.{translationArea.ToString()}"));
}
}

View file

@ -0,0 +1,14 @@
namespace LBPUnion.ProjectLighthouse.Localization;
public static class Program
{
public static void Main()
{
Console.WriteLine(LocalizationManager.GetLocalizedString(TranslationAreas.BaseLayout, "en-UD", "header_home"));
Console.WriteLine("Available languages:");
foreach (string language in LocalizationManager.GetAvailableLanguages(TranslationAreas.BaseLayout))
{
Console.WriteLine(language);
}
}
}

View file

@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>LBPUnion.ProjectLighthouse.Localization</RootNamespace>
<OutputType>Exe</OutputType>
</PropertyGroup>
</Project>

View file

@ -0,0 +1,6 @@
namespace LBPUnion.ProjectLighthouse.Localization;
public enum TranslationAreas
{
BaseLayout,
}

View file

@ -10,7 +10,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProjectLighthouse.Tests.Gam
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProjectLighthouse.Tests.WebsiteTests", "ProjectLighthouse.Tests.WebsiteTests\ProjectLighthouse.Tests.WebsiteTests.csproj", "{CF65EB5B-5364-4D2A-8639-F147A67F08E7}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProjectLighthouse.Tests.WebsiteTests", "ProjectLighthouse.Tests.WebsiteTests\ProjectLighthouse.Tests.WebsiteTests.csproj", "{CF65EB5B-5364-4D2A-8639-F147A67F08E7}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProjectLighthouse.Localisation", "ProjectLighthouse.Localisation\ProjectLighthouse.Localisation.csproj", "{CDB81465-F758-4842-B843-F77ABEE7C3BF}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProjectLighthouse.Localization", "ProjectLighthouse.Localization\ProjectLighthouse.Localization.csproj", "{CDB81465-F758-4842-B843-F77ABEE7C3BF}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution

View file

@ -1,4 +1,4 @@
namespace LBPUnion.ProjectLighthouse.Helpers; namespace LBPUnion.ProjectLighthouse.Helpers;
public static class LocalisationHelper public static class LocalizationHelper
{} {}

View file

@ -43,11 +43,11 @@
<EmbeddedResource Include="gitRemotes.txt"> <EmbeddedResource Include="gitRemotes.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource> </EmbeddedResource>
<None Remove="gitUnpushed.txt" /> <None Remove="gitUnpushed.txt"/>
<EmbeddedResource Include="gitUnpushed.txt"> <EmbeddedResource Include="gitUnpushed.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource> </EmbeddedResource>
<None Remove="chatCensoredList.txt" /> <None Remove="chatCensoredList.txt"/>
<EmbeddedResource Include="chatCensoredList.txt"> <EmbeddedResource Include="chatCensoredList.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource> </EmbeddedResource>
@ -60,7 +60,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\ProjectLighthouse.Localisation\ProjectLighthouse.Localisation.csproj"/> <ProjectReference Include="..\ProjectLighthouse.Localization\ProjectLighthouse.Localization.csproj"/>
</ItemGroup> </ItemGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent"> <Target Name="PreBuild" BeforeTargets="PreBuildEvent">

View file

@ -10,29 +10,30 @@ files: [
# Source files filter # Source files filter
# e.g. "/resources/en/*.json" # e.g. "/resources/en/*.json"
# #
"source": "/ProjectLighthouse.Localisation/*.resx", "source": "/ProjectLighthouse.Localization/*.resx",
# #
# Where translations will be placed # Where translations will be placed
# e.g. "/resources/%two_letters_code%/%original_file_name%" # e.g. "/resources/%two_letters_code%/%original_file_name%"
# #
"translation": "/ProjectLighthouse.Localisation/%file_name%.%locale%.%file_extension%", "translation": "/ProjectLighthouse.Localization/%file_name%.%locale%.%file_extension%",
# #
# Files or directories for ignore # Files or directories for ignore
# e.g. ["/**/?.txt", "/**/[0-9].txt", "/**/*\?*.txt"] # e.g. ["/**/?.txt", "/**/[0-9].txt", "/**/*\?*.txt"]
# #
#"ignore" : [], ignore:
- "/ProjectLighthouse.Localization/%file_name%.*.%file_extension%"
#
# The dest allows you to specify a file name in Crowdin #
# e.g. "/messages.json" # The dest allows you to specify a file name in Crowdin
# # e.g. "/messages.json"
#"dest" : "", #
#"dest" : "",
#
# File type #
# e.g. "json" # File type
# e.g. "json"
# #
#"type" : "", #"type" : "",