Skip to content

Commit

Permalink
Added tool to extract translation during build.
Browse files Browse the repository at this point in the history
  • Loading branch information
martinknafve committed Apr 14, 2014
1 parent 1d76122 commit 463d0f0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 176 deletions.
6 changes: 6 additions & 0 deletions hmailserver/tools/DevTools.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DatabaseUnicodeConversion",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SQLScriptCreator", "SQLScriptCreator\SQLScriptCreator.csproj", "{9068DD94-207F-4D9B-9217-110AD6A4238B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TranslationExtractor", "TranslationExtractor\TranslationExtractor.csproj", "{5BC541A7-3DAA-41DC-9C6C-2DAF41244F62}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -25,6 +27,10 @@ Global
{9068DD94-207F-4D9B-9217-110AD6A4238B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9068DD94-207F-4D9B-9217-110AD6A4238B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9068DD94-207F-4D9B-9217-110AD6A4238B}.Release|Any CPU.Build.0 = Release|Any CPU
{5BC541A7-3DAA-41DC-9C6C-2DAF41244F62}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5BC541A7-3DAA-41DC-9C6C-2DAF41244F62}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5BC541A7-3DAA-41DC-9C6C-2DAF41244F62}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5BC541A7-3DAA-41DC-9C6C-2DAF41244F62}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
189 changes: 19 additions & 170 deletions hmailserver/tools/TranslationExtractor/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,21 @@
// http://www.hmailserver.com

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Net;
using System.IO;
using System.Configuration;
using System.Text;

namespace TranslationExtractor
{
class Program
internal class Program
{
static string TranslationFolder = null;
private static string TranslationFolder;

static void Main(string[] args)
private static string TranslationScript =
"http://www.hmailserver.com/devnet/translation_getlanguage.php?language={0}";

private static void Main(string[] args)
{
string username = ConfigurationSettings.AppSettings["MySQLUsername"];
string password = ConfigurationSettings.AppSettings["MySQLPassword"];
string port = ConfigurationSettings.AppSettings["MySQLPort"];

TranslationFolder = args[0];

if (Directory.Exists(TranslationFolder) == false)
Expand All @@ -30,175 +26,28 @@ static void Main(string[] args)
return;
}

if (string.IsNullOrEmpty(username))
{
Environment.Exit(-1);
return;
}

Console.WriteLine(string.Format("Creating translation in {0}.", TranslationFolder));

Console.WriteLine("Starting extraction...");

string connectionString = string.Format("Database=hmailserver_webpage;Data Source=hmailserver.com;User Id={0};Password={1};CharSet=utf8;Port={2}", username, password, port);

using (MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(connectionString))
{
conn.Open();

List<string> languages = new List<string>();
string sql = "select languageinternalname from hm_tran_languages order by languageinternalname asc";

using (MySqlCommand command = new MySqlCommand(sql, conn))
{
using (MySqlDataReader reader = command.ExecuteReader())
{


while (reader.Read())
{
languages.Add(reader.GetString("languageinternalname"));
}

reader.Close();
}
}

int numberOfStrings = 0;
numberOfStrings = GetNumberOfStrings(conn);

List<string> languagesToExtract = new List<string>();

foreach (string language in languages)
{
if (language != "english" && language != "swedish")
{
Console.WriteLine(string.Format("Skipped language {0} (hardcoded exclusion)", language));
continue;
}

int numberOfTranslatedStrings = GetNumberOfStringsInLanguage(conn, language);

int translatedPercentage = Convert.ToInt32(((double)numberOfTranslatedStrings / (double)numberOfStrings) * (double)100);

if (translatedPercentage < 95)
{
Console.WriteLine(string.Format("Skipped language {0} ({1}%)", language, translatedPercentage));
continue;
}



languagesToExtract.Add(language);

}

string exportedLanguages = "";
foreach (string language in languagesToExtract)
{
Console.Write(string.Format("Extract language {0}...", language));

ExtractLanguage(conn, language);

if (exportedLanguages.Length > 0)
exportedLanguages += ",";

exportedLanguages += language;
DownloadTranslation("english", TranslationFolder);
DownloadTranslation("swedish", TranslationFolder);

Console.WriteLine(" done");
}

Console.WriteLine(string.Format("Extraction completed. {0} languages were extracted.", languagesToExtract.Count));
Console.WriteLine();
Console.WriteLine("Line for InnoSetup:");

string output = string.Format(
"Filename: \"{0}\"; Section: \"GUILanguages\"; Key: \"ValidLanguages\"; String: \"{1}\";",
"{app}\\Bin\\hMailServer.INI",
exportedLanguages
);

Console.WriteLine(output);
Console.WriteLine();
}

Environment.Exit(0);
}

private static void ExtractLanguage(MySqlConnection conn, string language)
private static void DownloadTranslation(string language, string targetDir)
{
var url = string.Format(TranslationScript, language);

string destinationFile = Path.Combine(TranslationFolder, language + ".ini");

if (File.Exists(destinationFile))
File.Delete(destinationFile);

string sql = string.Format("select stringid, language_{0} translation from hm_tran_strings order by stringid asc", language);

string contents = "[Strings]" + Environment.NewLine;

using (MySqlCommand command = new MySqlCommand(sql, conn))
{
using (MySqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
int stringID = reader.GetInt32("stringID");
string value = reader.GetString("translation");

string line = string.Format("String_{0}={1}", stringID, value);

contents += line + Environment.NewLine;
}

reader.Close();
}
}

File.WriteAllText(destinationFile, contents, Encoding.Unicode);
}
var request = (HttpWebRequest) WebRequest.Create(url);

private static int GetNumberOfStrings(MySqlConnection conn)
{
string sql = "select count(*) c from hm_tran_strings";

using (MySqlCommand command = new MySqlCommand(sql, conn))
using (var response = request.GetResponse())
using (var responseStream = response.GetResponseStream())
using (var streamReader = new StreamReader(responseStream))
{
using (MySqlDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
int numberOfStrings = reader.GetInt32("c");
return numberOfStrings;
}
var content = streamReader.ReadToEnd();

reader.Close();
}
}
var targetFile = Path.Combine(targetDir, string.Format("{0}.ini", language));

throw new Exception("Database connection failed");
}

private static int GetNumberOfStringsInLanguage(MySqlConnection conn, string language)
{
string sql = string.Format("select count(*) c from hm_tran_strings where language_{0} <> ''", language);

using (MySqlCommand command = new MySqlCommand(sql, conn))
{
using (MySqlDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
int numberOfStrings = reader.GetInt32("c");
return numberOfStrings;
}

reader.Close();
}
File.WriteAllText(targetFile, content, Encoding.UTF8);
}

throw new Exception("Database connection failed");
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="MySql.Data, Version=5.1.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>Libraries\MySql.Data.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
Expand Down

This file was deleted.

0 comments on commit 463d0f0

Please sign in to comment.