Skip to content

Commit f555c1c

Browse files
committed
Add project files.
1 parent 70d854e commit f555c1c

7 files changed

Lines changed: 220 additions & 0 deletions

‎FMGYAZILIM_LOGO.png‎

1.87 KB
Loading

‎FmgLib.Localization.csproj‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
8+
<Title>FmgLib.Localization</Title>
9+
<Company>Fmg Yazılım</Company>
10+
<PackageIcon>FMGYAZILIM_LOGO.png</PackageIcon>
11+
<PackageReadmeFile>README.md</PackageReadmeFile>
12+
<Authors>FmgYazılım</Authors>
13+
<Description>This package serves to integrate your system into multiple languages.</Description>
14+
<Copyright>Copyright 2023</Copyright>
15+
<PackageTags>Localization</PackageTags>
16+
<Version>1.0.3</Version>
17+
</PropertyGroup>
18+
19+
<ItemGroup>
20+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
21+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
22+
</ItemGroup>
23+
24+
<ItemGroup>
25+
<None Update="FMGYAZILIM_LOGO.png">
26+
<Pack>True</Pack>
27+
<PackagePath>\</PackagePath>
28+
</None>
29+
<None Update="README.md">
30+
<Pack>True</Pack>
31+
<PackagePath>\</PackagePath>
32+
</None>
33+
</ItemGroup>
34+
35+
</Project>

‎FmgLib.Localization.sln‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.9.34511.98
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FmgLib.Localization", "FmgLib.Localization.csproj", "{F2231A23-8F19-4520-865C-8A7FAE7C5D56}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{F2231A23-8F19-4520-865C-8A7FAE7C5D56}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{F2231A23-8F19-4520-865C-8A7FAE7C5D56}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{F2231A23-8F19-4520-865C-8A7FAE7C5D56}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{F2231A23-8F19-4520-865C-8A7FAE7C5D56}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {2A7F3BAE-C8B2-49C4-88C4-4AF175C2B3D5}
24+
EndGlobalSection
25+
EndGlobal

‎LocalizationService.cs‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using Newtonsoft.Json;
2+
3+
namespace FmgLib.Localization;
4+
5+
public static class LocalizationService
6+
{
7+
private static Dictionary<string, Dictionary<string, string>> _languagePacks;
8+
public static string Language { get; set; }
9+
10+
public static void ReadLocalizationFile(params string[] filePathes)
11+
{
12+
_languagePacks = new Dictionary<string, Dictionary<string, string>>();
13+
14+
try
15+
{
16+
List<string> pathesList = filePathes.ToList();
17+
18+
if (pathesList.Count == 0)
19+
pathesList.Add("Localization.json");
20+
21+
foreach (var filePath in pathesList)
22+
{
23+
var json = File.ReadAllText(filePath);
24+
var pack = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, string>>>(json);
25+
26+
foreach (var lang in pack)
27+
{
28+
if (!_languagePacks.ContainsKey(lang.Key))
29+
{
30+
_languagePacks[lang.Key] = new Dictionary<string, string>();
31+
}
32+
if (lang.Value != null)
33+
{
34+
foreach (var kvp in lang.Value)
35+
{
36+
_languagePacks[lang.Key][kvp.Key] = kvp.Value;
37+
}
38+
}
39+
}
40+
}
41+
}
42+
catch (Exception ex)
43+
{
44+
throw new FileLoadException($"There is an error in your language file.\n{ex.Message} \n\n\n\n The proper json format is:\n \"{{\\n \\\"wordKey\\\": {{\\n \\\"langKey1\\\": \\\"1st language translation.\\\",\\n \\\"langKey2\\\": \\\"2nd language translation.\\\"\\n }},\\n \\\"wordKey2\\\": {{\\n \\\"langKey1\\\": \\\"1st language translation.\\\",\\n \\\"langKey2\\\": \\\"2nd language translation.\\\"\\n }}\\n}}\"");
45+
}
46+
47+
}
48+
49+
public static string Translate(string key)
50+
{
51+
if (string.IsNullOrEmpty(Language))
52+
Language = Thread.CurrentThread.CurrentUICulture.Name;
53+
54+
var lang = _languagePacks.FirstOrDefault(x => x.Key == key).Value?.FirstOrDefault(y => y.Key == Language);
55+
var resultKey = lang?.Key;
56+
var resultValue = lang?.Value;
57+
if (resultKey == Language)
58+
{
59+
return resultValue ?? key;
60+
}
61+
62+
if (_languagePacks.ContainsKey("en") && _languagePacks["en"].ContainsKey(key))
63+
{
64+
return _languagePacks["en"][key];
65+
}
66+
67+
return key;
68+
}
69+
70+
public static string Translate(string key, string langKey)
71+
{
72+
var lang = _languagePacks.FirstOrDefault(x => x.Key == key).Value?.FirstOrDefault(y => y.Key == langKey);
73+
var resultKey = lang?.Key;
74+
var resultValue = lang?.Value;
75+
if (resultKey == langKey)
76+
{
77+
return resultValue ?? key;
78+
}
79+
80+
return key;
81+
}
82+
}

‎README.md‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
To access related services, simply add `using FmgLib.Localization;` statement.
2+
3+
4+
In the Program.cs file,
5+
```CSharp
6+
builder.Services.AddFmgLibLocalization();
7+
```
8+
should be added.
9+
10+
11+
In your main project you should have a language file of type json. The translation will be read from this file and imported.
12+
If you do not specify the path to the file(s) in the parameter (
13+
```CSharp
14+
builder.Services.AddFmgLibLocalization();
15+
```
16+
), will look for a json file named `Localization.json` in the home directory.
17+
18+
if you give one or more parameters like
19+
```CSharp
20+
builder.Services.AddFmgLibLocalization("Localization1.json", "Localization2.json", "/Languages/Temp1.json");
21+
```
22+
it will read json files in given file paths.
23+
24+
25+
26+
Proper json format:
27+
28+
![image](https://user-images.githubusercontent.com/73774639/225441549-4c195df7-789a-4887-87d2-d5f6f434433c.png)
29+
30+
Instead of 'keyWord' keywords, you can use any word or phrase(s) you want. You don't have any Regex limitations.
31+
32+
You can also change the 'tr-TR' and 'en-US' language keys with words or sentences as you wish. But it is recommended to use expressions such as 'en-US', 'tr-TR', 'fr-FR'.
33+
34+
35+
You can simply use
36+
```CSharp
37+
Console.WriteLine("keyWord 1".ToTranslate());
38+
```
39+
in the code.
40+
41+
If you do not specify a parameter, it will translate according to the language expression registered in Culture (language keys such as 'en-US', 'tr-TR' will appear).
42+
43+
You can easily obtain the translation by specifying the language key as
44+
```CSharp
45+
Console.WriteLine("keyWord 1".ToTranslate("tr-TR"));
46+
```
47+
.

‎ServiceRegistration.cs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
3+
namespace FmgLib.Localization;
4+
5+
public static class ServiceRegistration
6+
{
7+
public static IServiceCollection AddFmgLibLocalization(this IServiceCollection services, params string[] filePathes)
8+
{
9+
LocalizationService.ReadLocalizationFile(filePathes);
10+
11+
return services;
12+
}
13+
14+
public static void SetFmgLibLocalization(params string[] filePathes)
15+
{
16+
LocalizationService.ReadLocalizationFile(filePathes);
17+
}
18+
}

‎ToTranslateHelper.cs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace FmgLib.Localization;
2+
3+
public static class ToTranslateHelper
4+
{
5+
public static string ToTranslate(this string key)
6+
{
7+
return LocalizationService.Translate(key);
8+
}
9+
public static string ToTranslate(this string key, string langKey)
10+
{
11+
return LocalizationService.Translate(key, langKey);
12+
}
13+
}

0 commit comments

Comments
 (0)