Last active
May 16, 2017 22:29
-
-
Save omerfarukz/7f5e3f0d0bbcde2950110d5da2fb2f2f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Globalization; | |
using System.Linq; | |
namespace LocalizationDemo | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
LocalizationOptions options = new LocalizationOptions(); | |
options.ReturnNameWhenNotFound = true; | |
options.ReturnBracketsOnUsingName = true; | |
var sources = new List<ILocalizationSource> { new StringSourceInMemory() }; | |
ILocalizationManager localizationManager = new LocalizationManager(options, sources); | |
PrintLocalized(localizationManager, "name1", "en-US"); | |
PrintLocalized(localizationManager, "name1", "tr-TR"); | |
PrintLocalized(localizationManager, "name2", "en-US"); | |
PrintLocalized(localizationManager, "name2", "tr-TR"); | |
PrintLocalized(localizationManager, "name3", "tr-TR"); | |
} | |
private static void PrintLocalized(ILocalizationManager man, string name, string cultureName) | |
{ | |
ILocalizationContext context = new DefaultLocalizationContext(man); | |
context.CultureInfo = new CultureInfo(cultureName); | |
var localizationResult = context.Localize(name); | |
if (localizationResult != null) | |
{ | |
Console.WriteLine(localizationResult.Value); | |
} | |
else | |
{ | |
Console.WriteLine($"NOT FOUND: {name}"); | |
} | |
} | |
class StringSourceInMemory : ILocalizationSource | |
{ | |
private Dictionary<string, string> _values = new Dictionary<string, string>(); | |
public StringSourceInMemory() | |
{ | |
_values.Add("tr-TR#name1", "ornek"); | |
_values.Add("tr-TR#name2", "sunum"); | |
_values.Add("en-US#name1", "example"); | |
_values.Add("en-US#name2", "presentation"); | |
} | |
public bool CanLocalize(string name, CultureInfo cultureInfo) | |
{ | |
if (string.IsNullOrWhiteSpace(name)) | |
throw new ArgumentNullException(nameof(name)); | |
if (cultureInfo == null) | |
throw new ArgumentNullException(nameof(cultureInfo)); | |
string key = GetKey(name, cultureInfo); | |
return _values.ContainsKey(key); | |
} | |
public IEnumerable<ILocalizable> GetAll() | |
{ | |
return _values.Select(f => CreateLocalizableStringFrom(f.Key, f.Value)); | |
} | |
public object Localize(string name, CultureInfo cultureInfo) | |
{ | |
if (string.IsNullOrWhiteSpace(name)) | |
throw new ArgumentNullException(nameof(name)); | |
if (cultureInfo == null) | |
throw new ArgumentNullException(nameof(cultureInfo)); | |
string key = GetKey(name, cultureInfo); | |
if (!_values.ContainsKey(key)) | |
{ | |
throw new InvalidProgramException($"Name not found {name} with key {key}"); | |
} | |
return _values[key]; | |
} | |
private ILocalizable CreateLocalizableStringFrom(string name, string stringValue) | |
{ | |
if (string.IsNullOrWhiteSpace(name)) | |
throw new ArgumentNullException(nameof(name)); | |
return new LocalizableString() { Name = name, Value = stringValue }; | |
} | |
private string GetKey(string name, CultureInfo cultureInfo) | |
{ | |
if (string.IsNullOrWhiteSpace(name)) | |
throw new ArgumentNullException(nameof(name)); | |
if (cultureInfo == null) | |
throw new ArgumentNullException(nameof(cultureInfo)); | |
return $"{cultureInfo.Name}#{name}"; | |
} | |
} | |
} | |
public class LocalizationOptions | |
{ | |
public bool ReturnNameWhenNotFound { get; set; } | |
public bool ReturnBracketsOnUsingName { get; set; } | |
} | |
public interface ILocalizable | |
{ | |
string Name { get; set; } | |
object GetValue(); | |
} | |
public class LocalizableString : ILocalizable | |
{ | |
public string Name { get; set; } | |
public string Value { get; set; } | |
public object GetValue() | |
{ | |
return this.Value; | |
} | |
public static implicit operator string(LocalizableString localizableString) | |
{ | |
return (string)localizableString?.GetValue(); | |
} | |
} | |
public interface ILocalizationSource | |
{ | |
bool CanLocalize(string name, CultureInfo cultureInfo); | |
object Localize(string name, CultureInfo cultureInfo); | |
IEnumerable<ILocalizable> GetAll(); | |
} | |
public interface ILocalizationContext | |
{ | |
CultureInfo CultureInfo { get; set; } | |
LocalizationResult Localize(string name); | |
} | |
public class LocalizationResult | |
{ | |
public string Name { get; set; } | |
public CultureInfo CultureInfo { get; set; } | |
public ILocalizationSource Source { get; set; } | |
public virtual object Value { get; set; } | |
} | |
public class LocalizationResult<T> : LocalizationResult | |
{ | |
public new T Value { get; set; } | |
} | |
public class DefaultLocalizationContext : ILocalizationContext | |
{ | |
private readonly ILocalizationManager _localizationManager; | |
public CultureInfo CultureInfo { get; set; } | |
public DefaultLocalizationContext(ILocalizationManager localizationManager) | |
{ | |
_localizationManager = localizationManager; | |
} | |
public LocalizationResult Localize(string name) | |
{ | |
if (string.IsNullOrWhiteSpace(name)) | |
throw new ArgumentNullException(nameof(name)); | |
return _localizationManager.Localize(name, CultureInfo); | |
} | |
} | |
public interface ILocalizationManager | |
{ | |
LocalizationResult Localize(string name, CultureInfo cultureInfo); | |
} | |
public class LocalizationManager : ILocalizationManager | |
{ | |
private readonly IEnumerable<ILocalizationSource> _sources; | |
private readonly LocalizationOptions _options; | |
public LocalizationManager(LocalizationOptions options, IEnumerable<ILocalizationSource> sources) | |
{ | |
_options = options; | |
_sources = sources; | |
} | |
public LocalizationResult Localize(string name, CultureInfo cultureInfo) | |
{ | |
if (string.IsNullOrWhiteSpace(name)) | |
throw new ArgumentNullException(nameof(name)); | |
if (cultureInfo == null) | |
throw new ArgumentNullException(nameof(cultureInfo)); | |
LocalizationResult result = null; | |
var source = _sources.FirstOrDefault(f => f.CanLocalize(name, cultureInfo)); | |
if (source != null) | |
{ | |
var localized = source.Localize(name, cultureInfo); | |
result = new LocalizationResult() | |
{ | |
CultureInfo = cultureInfo, | |
Name = name, | |
Source = source, | |
Value = localized | |
}; | |
} | |
else | |
{ | |
if (_options.ReturnNameWhenNotFound) | |
{ | |
result = new LocalizationResult() | |
{ | |
CultureInfo = cultureInfo, | |
Name = name, | |
Source = source, | |
Value = _options.ReturnBracketsOnUsingName ? $"[{name}]" : name | |
}; | |
} | |
} | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment