Skip to content

Instantly share code, notes, and snippets.

Created August 9, 2016 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/7743197bada899609806b3f0bc8de1f2 to your computer and use it in GitHub Desktop.
Save anonymous/7743197bada899609806b3f0bc8de1f2 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using SecondLanguage;
namespace ConsoleApplication1
{
public static class Program
{
private const string RootDirectory = @"C:\Temp\UrBackup";
private const string TranslationDirectory = @"translations\urbackup.webinterface";
private const string JSFilePath = @"js\translation.js";
private static readonly IDictionary<string, string> LanguageMap = new Dictionary<string, string>()
{
{ "fa_IR", "fa" }
};
public static void Main(string[] args)
{
Console.WindowWidth = 120;
using (var writer = new StreamWriter(Path.Combine(RootDirectory, JSFilePath), false, Encoding.UTF8))
{
writer.Write("if(!window.translations) translations=new Object();\n");
foreach (var file in Directory.EnumerateFiles(Path.Combine(RootDirectory, TranslationDirectory), "*.po"))
{
var lang = Regex.Match(Path.GetFileName(file), @"(?<lang>[A-Za-z_]*).*\.po").Groups["lang"].ToString();
if (!string.IsNullOrEmpty(lang))
{
var po = new GettextPOTranslation();
po.Load(file);
var keys = po.GetGettextKeys().Where(x => !string.IsNullOrEmpty(x.Key.ID) && !string.IsNullOrEmpty(x.Value.FirstOrDefault())).ToList();
if (keys.Any(x => !string.IsNullOrEmpty(x.Value.First())))
{
Console.WriteLine($"{lang}:\t{keys.Count} keys \t{file}");
writer.WriteLine(
$"translations.{(LanguageMap.ContainsKey(lang) ? LanguageMap[lang] : lang)} = {{\n"
+ string.Join(
",\n",
keys.Select(x => $@"""{x.Key.ID.Escape()}"": ""{po.GetString(x.Key.ID).Escape()}"""))
+ "\n}");
}
}
}
}
Console.WriteLine("\nPress any key to exit.");
Console.ReadKey();
}
private static string Escape(this string s) => s.Replace(@"""", @"\""").Replace("\n", @"\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment