Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added translation support #220

Merged
merged 9 commits into from
Jun 23, 2024
Merged
8 changes: 8 additions & 0 deletions Unity/Assets/Scripts/Localization.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

894 changes: 894 additions & 0 deletions Unity/Assets/Scripts/Localization/DatabaseTextConverter.cs

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions Unity/Assets/Scripts/Localization/DatabaseTextConverter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

164 changes: 164 additions & 0 deletions Unity/Assets/Scripts/Localization/PoFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Text;

public class PoFile
{
public Dictionary<string, string> translationData = new Dictionary<string, string>();

public void SaveFile(string filePath, bool isTemplate = false, bool stripEmpty = false)
{
using(StreamWriter writer = new StreamWriter(filePath, false))
{
using (var provider = CodeDomProvider.CreateProvider("CSharp"))
{
writer.WriteLine("msgid \"\"");
writer.WriteLine("msgstr \"\"");
writer.WriteLine("");
foreach(var k in translationData)
{
if(stripEmpty && string.IsNullOrEmpty(k.Value))
continue;
writer.WriteLine("msgid \""+k.Key+"\"");
string str = k.Value;
str = str.Replace("\n", "\\n").Replace("\"", "\\\"").Replace("\r","\\r");
writer.WriteLine("msgstr \""+(isTemplate ? "" : str)+"\"");
writer.WriteLine("");
}
}
}
}

public void LoadFile(string filePath, bool clear = false)
{
if(clear)
translationData.Clear();
if(!File.Exists(filePath))
{
UnityEngine.Debug.LogError("Cannont find "+filePath);
return;
}
using(StreamReader reader = new StreamReader(filePath))
{
string msgId = null;
string msgStr = null;
while(true)
{
string line = reader.ReadLine();
if(line == null || line == "")
{
if(msgStr != null && msgId != null && msgId != "")
{
//UnityEngine.Debug.LogError("A Msg "+msgId+":"+msgStr);
msgStr = msgStr.Replace("\\\"", "\"").Replace("\\n", "\n").Replace("\\r", "\r");
if(translationData.ContainsKey(msgId))
{
if(!string.IsNullOrEmpty(msgStr))
{
//UnityEngine.Debug.LogError("Replace "+msgId+":"+msgStr);
translationData[msgId] = msgStr;
}
}
else
{
//UnityEngine.Debug.LogError("Add "+msgId+":"+msgStr);
translationData.Add(msgId, msgStr);
}
}
msgId = null;
msgStr = null;
}
if(line == null)
break;
if(line.StartsWith("msgid \""))
{
msgId = line.Replace("msgid \"", "");
msgId = msgId.Remove(msgId.Length - 1, 1);
//UnityEngine.Debug.LogError("Read Id "+msgId+":"+msgStr);
}
else if(line.StartsWith("msgstr \""))
{
msgStr = line.Replace("msgstr \"", "");
msgStr = msgStr.Remove(msgStr.Length - 1, 1);
//UnityEngine.Debug.LogError("Read Msg "+msgId+":"+msgStr);
}
else if(line.StartsWith("\""))
{
string s = line.Replace("\"", "");
if(msgStr == null)
{
if(msgId != null)
msgId += s;
}
else
{
msgStr += s;
}
}
}
}
}

public void WriteToArchiveAsBankData(CBBJHPBGBAJ_Archive archive, string fileName)
{
// Generate db translated file
// Binary format : (From MessageBank : SetupFromBinary)
// 0 : int32 numStrings
// 4 - 14 : 0xff
// loop num (4 * 4 * num)
// int32 offset1
// int32 size1
// int32 offset2
// int32 size2
// loop strings
// string (offset, size) Encoding.Unicode.GetString
if(translationData.Count > 0)
{
using(MemoryStream stream = new MemoryStream())
{
using(BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write(translationData.Count);
for(int i = 4; i < 16; i++)
writer.Write((byte)0xff);
for(int i = 0; i < translationData.Count; i++)
{
int v = 0;
writer.Write(v);
writer.Write(v);
writer.Write(v);
writer.Write(v);
}
int infoOffset = 16;
int curOffset = 16 + 4 * 4 * translationData.Count;
foreach(var k in translationData)
{
byte[] strByte = Encoding.Unicode.GetBytes(k.Value);
writer.Write(strByte);
writer.Seek(infoOffset, SeekOrigin.Begin);
writer.Write(curOffset);
writer.Write(strByte.Length);
curOffset += strByte.Length;
writer.Seek(0, SeekOrigin.End);
infoOffset += 8;

strByte = Encoding.Unicode.GetBytes(k.Key);
writer.Write(strByte);
writer.Seek(infoOffset, SeekOrigin.Begin);
writer.Write(curOffset);
writer.Write(strByte.Length);
curOffset += strByte.Length;
writer.Seek(0, SeekOrigin.End);
infoOffset += 8;
}
writer.Flush();
byte[] data = stream.ToArray();
archive.KGHAJGGMPKL_Files.Add(new CBBJHPBGBAJ_Archive.JBCFNCNGLPM_File() { OPFGFINHFCE_Name = fileName, DBBGALAPFGC_Data = data });
writer.Close();
}
}
}
}
}
11 changes: 11 additions & 0 deletions Unity/Assets/Scripts/Localization/PoFile.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions Unity/Assets/Scripts/Localization/StringLiteralsConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

#if UNITY_EDITOR
using System;
using System.IO;
using System.Reflection;
using UnityEditor;
using UnityEngine;

public static class StringLiteralsConverter
{
public static string PoPath = Application.dataPath + "/../../Localization/JpLiteralStrings/po/";

[MenuItem("UMO/Localization/Export JpStringLiterals strings")]
public static void GeneratePoFile()
{
PoFile poFile = new PoFile();

Type t = Type.GetType("JpStringLiterals");

PropertyInfo[] fields = t.GetProperties(BindingFlags.Static | BindingFlags.Public);
foreach(var k in fields)
{
poFile.translationData.Add(k.Name, k.GetValue(null).ToString());
}

Directory.CreateDirectory(PoPath);
poFile.SaveFile(PoPath + "messages.pot", true);
poFile.SaveFile(PoPath + "jp.po");
}
}
#endif
11 changes: 11 additions & 0 deletions Unity/Assets/Scripts/Localization/StringLiteralsConverter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading