Created
August 9, 2016 16:29
-
-
Save anonymous/7743197bada899609806b3f0bc8de1f2 to your computer and use it in GitHub Desktop.
A quick & dirty port of translations_from_po() in https://github.com/uroni/urbackup_backend/blob/next/urbackupserver/www/translation_helper.py
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
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