Created
October 13, 2017 06:25
-
-
Save BrunoVT1992/94b899625259f65c566a047c6e5b212d to your computer and use it in GitHub Desktop.
Translations visual studio tool for managing csv text files
This file contains 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
<#@ template language="C#" hostSpecific="true" #> | |
<#@ output extension=".cs" #> | |
<#@ assembly name="System.Core"#> | |
<#@ import namespace="System" #> | |
<#@ import namespace="System.Linq" #> | |
<#@ import namespace="System.Text" #> | |
<#@ import namespace="System.IO" #> | |
<#@ import namespace="System.Collections.Generic" #> | |
<#@ import namespace="System.Globalization" #> | |
<# | |
// need to save in XLS as Windows CSV (for accents) | |
Process("YourNameSpaceHere", "YourFileNameHere.csv"); | |
#> | |
using System.Collections.Generic; | |
using System.Globalization; | |
namespace <#= NamespaceName #> | |
{ | |
public static class Translations | |
{ | |
static Dictionary<string, Dictionary<string, string>> TranslationItems { get; set; } | |
static Translations() | |
{ | |
TranslationItems = new Dictionary<string, Dictionary<string, string>>(); | |
SupportedLanguages = new List<CultureInfo> { <#= SupportedLanguages #> }; | |
Load(); | |
} | |
public static CultureInfo Culture { get; set; } | |
public static List<CultureInfo> SupportedLanguages { get; private set; } | |
public static string GetString(string key, CultureInfo culture = null) | |
{ | |
Dictionary<string, string> translations; | |
if (culture == null) | |
{ | |
culture = Culture; | |
} | |
var found = TranslationItems.TryGetValue(key, out translations); | |
if (!found) { | |
return $"[{culture}]{key} key not found"; | |
} | |
string translation; | |
found = translations.TryGetValue(culture.ToString().ToLower(), out translation); | |
if (!found) { | |
return $"[{culture}]{key} key not found"; | |
} | |
if (string.IsNullOrEmpty(translation) && translation != "|") { | |
return $"[{culture}]{key} key not found"; | |
} | |
return translation; | |
} | |
<# foreach(var key in Translations.Keys) { #> | |
/// <summary> | |
/// <#= GetTranslation(key) #> | |
/// </summary> | |
public static string <#= key #> { get { return GetString("<#= key #>", Culture); } } | |
<# } #> | |
static void Load() | |
{ | |
<# foreach(var key in Translations.Keys) { #> | |
TranslationItems.Add("<#= key #>", new Dictionary<string, string> { <#= GetTranslation(key) #> }); | |
<# } #> | |
} | |
} | |
} | |
<#+ | |
public string NamespaceName { get; set; } | |
public Dictionary<string, Dictionary<string, string>> Translations { get; set; } | |
public string SupportedLanguages { get; set; } | |
public void Process(string namespaceName, string importFile) | |
{ | |
Translations = new Dictionary<string, Dictionary<string, string>>(); | |
NamespaceName = namespaceName; | |
var filePath = Path.Combine(Path.GetDirectoryName(Host.TemplateFile), importFile); | |
var lines = File.ReadAllLines(filePath, Encoding.GetEncoding(1252)); | |
//Rewrite file so that the keys are ordered by alphabet | |
var linesList = lines.ToList(); | |
var firstLine = linesList.First(); | |
linesList.RemoveAt(0); | |
linesList = linesList.OrderByDescending(x => x).ToList(); | |
linesList.Add(firstLine); | |
linesList.Reverse(); | |
File.WriteAllLines(filePath, linesList.ToArray(), Encoding.GetEncoding(1252)); | |
lines = linesList.ToArray(); | |
// first line contains key followed by translations | |
var header = lines[0].Split(';'); | |
for (int i = 1; i <= header.Count() - 1; i++) | |
{ | |
SupportedLanguages += $"new CultureInfo(\"{header[i]}\")"; | |
if(i < header.Count() - 1) | |
SupportedLanguages += ", "; | |
} | |
for (int i = 1; i < lines.Length; i++) | |
{ | |
var data = lines[i].Split(';'); | |
var key = data[0]; | |
var translation = new Dictionary<string,string>(); | |
for (int y = 1; y < data.Length; y++) | |
{ | |
translation.Add(header[y], data[y]); | |
} | |
Translations.Add(key, translation); | |
} | |
} | |
public string GetTranslation(string translationKey) | |
{ | |
var result = string.Empty; | |
var translations = Translations[translationKey]; | |
foreach(var key in translations.Keys) | |
{ | |
bool isLastKey = key == translations.Keys.Last(); | |
result += @"{ """ + key + @""", """ + translations[key] + @""" }"; | |
if (!isLastKey) result += ", "; | |
} | |
return result; | |
} | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment