mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-19 14:38:18 +00:00
changed language file format to json
This commit is contained in:
parent
f50b5d6059
commit
4337eb80f3
|
@ -1 +1 @@
|
|||
Subproject commit 928f0f47c0ffb1f6fea9d13254da42c250a825af
|
||||
Subproject commit 503ac9b16b645b00095dbbbbc32440c46a588a42
|
|
@ -1 +1 @@
|
|||
Subproject commit 64baa0e11c7f9a1390279394ca3536277afcd61b
|
||||
Subproject commit eff8f067d4f0bf123fb21a18258ba2d241559d82
|
|
@ -4,7 +4,7 @@ if match != None:
|
|||
os.chdir(match.group(1))
|
||||
|
||||
source_exts = [".c", ".cpp", ".h"]
|
||||
content_author = "##### authors #####\n#originally created by:\n#\n#modified by:\n#\n##### /authors #####\n".encode()
|
||||
content_author = ""
|
||||
|
||||
def parse_source():
|
||||
stringtable = {}
|
||||
|
@ -37,16 +37,16 @@ def load_languagefile(filename):
|
|||
global content_author
|
||||
|
||||
for i in range(0, len(lines)-1):
|
||||
l = lines[i].strip()
|
||||
if authorpart == 0 and l == "##### authors #####".encode():
|
||||
if authorpart == 0 and "\"authors\":".encode() in lines[i]:
|
||||
authorpart = 1
|
||||
content_author = l + "\n".encode()
|
||||
content_author = lines[i]
|
||||
elif authorpart == 1:
|
||||
if l == "##### /authors #####".encode():
|
||||
if "\"translated strings\":".encode() in lines[i]:
|
||||
authorpart = 2
|
||||
content_author += l + "\n".encode()
|
||||
elif len(l) and not l[0:1] == "=".encode() and not l[0:1] == "#".encode():
|
||||
stringtable[l] = lines[i+1][3:].rstrip()
|
||||
else:
|
||||
content_author += lines[i]
|
||||
elif "\"or\":".encode() in lines[i]:
|
||||
stringtable[lines[i].strip()[7:-2]] = lines[i+1].strip()[7:-1]
|
||||
|
||||
return stringtable
|
||||
|
||||
|
@ -63,25 +63,35 @@ def generate_languagefile(outputfilename, srctable, loctable):
|
|||
srctable_keys.sort()
|
||||
|
||||
content = content_author
|
||||
content += "\n##### translated strings #####\n\n".encode()
|
||||
|
||||
content += "\"translated strings\": [\n".encode()
|
||||
for k in srctable_keys:
|
||||
if k in loctable and len(loctable[k]):
|
||||
content += k + "\n== ".encode() + loctable[k] + "\n\n".encode()
|
||||
if not num_items == 0:
|
||||
content += ",\n".encode()
|
||||
content += "\t{\n\t\t\"or\": \"".encode() + k + "\",\n\t\t\"tr\": \"".encode() + loctable[k] + "\"\n\t}".encode()
|
||||
num_items += 1
|
||||
content += "],\n".encode()
|
||||
|
||||
content += "##### needs translation #####\n\n".encode()
|
||||
content += "\"needs translation\": [\n".encode()
|
||||
for k in srctable_keys:
|
||||
if not k in loctable or len(loctable[k]) == 0:
|
||||
content += k + "\n== \n\n".encode()
|
||||
if not new_items == 0:
|
||||
content += ",\n".encode()
|
||||
content += "\t{\n\t\t\"or\": \"".encode() + k + "\",\n\t\t\"tr\": \"\"\n\t}".encode()
|
||||
num_items += 1
|
||||
new_items += 1
|
||||
content += "],\n".encode()
|
||||
|
||||
content += "##### old translations #####\n\n".encode()
|
||||
content += "\"old translations\": [\n".encode()
|
||||
for k in loctable:
|
||||
if not k in srctable:
|
||||
content += k + "\n== ".encode() + loctable[k] + "\n\n".encode()
|
||||
if not old_items == 0:
|
||||
content += ",\n".encode()
|
||||
content += "\t{\n\t\t\"or\": \"".encode() + k + "\",\n\t\t\"tr\": \"".encode() + loctable[k] + "\"\n\t}".encode()
|
||||
num_items += 1
|
||||
old_items += 1
|
||||
content += "]\n}\n".encode()
|
||||
|
||||
f.write(content)
|
||||
f.close()
|
||||
|
@ -92,9 +102,7 @@ srctable = parse_source()
|
|||
print("%-40s %8s %8s %8s" % ("filename", "total", "new", "old"))
|
||||
|
||||
for filename in os.listdir("data/languages"):
|
||||
if not ".txt" in filename:
|
||||
continue
|
||||
if filename == "index.txt" or filename == "license.txt" or filename == "readme.txt":
|
||||
if not filename[-5:] == ".json" or filename == "index.json":
|
||||
continue
|
||||
|
||||
filename = "data/languages/" + filename
|
||||
|
|
|
@ -508,7 +508,7 @@ void LoadLanguageIndexfile(IStorage *pStorage, IConsole *pConsole, sorted_array<
|
|||
for(int i = 0; i < rStart.u.array.length; ++i)
|
||||
{
|
||||
char aFileName[128];
|
||||
str_format(aFileName, sizeof(aFileName), "languages/%s.txt", (const char *)rStart[i]["file"]);
|
||||
str_format(aFileName, sizeof(aFileName), "languages/%s.json", (const char *)rStart[i]["file"]);
|
||||
pLanguages->add(CLanguage((const char *)rStart[i]["name"], aFileName, (long)rStart[i]["code"]));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
#include "localization.h"
|
||||
#include <base/tl/algorithm.h>
|
||||
|
||||
#include <engine/shared/linereader.h>
|
||||
#include <engine/external/json-parser/json.h>
|
||||
#include <engine/console.h>
|
||||
#include <engine/storage.h>
|
||||
|
||||
|
@ -54,47 +54,43 @@ bool CLocalizationDatabase::Load(const char *pFilename, IStorage *pStorage, ICon
|
|||
return true;
|
||||
}
|
||||
|
||||
IOHANDLE IoHandle = pStorage->OpenFile(pFilename, IOFLAG_READ, IStorage::TYPE_ALL);
|
||||
if(!IoHandle)
|
||||
// read file data into buffer
|
||||
IOHANDLE File = pStorage->OpenFile(pFilename, IOFLAG_READ, IStorage::TYPE_ALL);
|
||||
if(!File)
|
||||
return false;
|
||||
int FileSize = (int)io_length(File);
|
||||
char *pFileData = (char *)mem_alloc(FileSize+1, 1);
|
||||
io_read(File, pFileData, FileSize);
|
||||
pFileData[FileSize] = 0;
|
||||
io_close(File);
|
||||
|
||||
char aBuf[256];
|
||||
// init
|
||||
char aBuf[64];
|
||||
str_format(aBuf, sizeof(aBuf), "loaded '%s'", pFilename);
|
||||
pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "localization", aBuf);
|
||||
m_Strings.clear();
|
||||
|
||||
char aOrigin[512];
|
||||
CLineReader LineReader;
|
||||
LineReader.Init(IoHandle);
|
||||
char *pLine;
|
||||
while((pLine = LineReader.Get()))
|
||||
// parse json data
|
||||
json_settings JsonSettings;
|
||||
mem_zero(&JsonSettings, sizeof(JsonSettings));
|
||||
char aError[256];
|
||||
json_value *pJsonData = json_parse_ex(&JsonSettings, pFileData, aError);
|
||||
if(pJsonData == 0)
|
||||
{
|
||||
if(!str_length(pLine))
|
||||
continue;
|
||||
|
||||
if(pLine[0] == '#') // skip comments
|
||||
continue;
|
||||
|
||||
str_copy(aOrigin, pLine, sizeof(aOrigin));
|
||||
char *pReplacement = LineReader.Get();
|
||||
if(!pReplacement)
|
||||
{
|
||||
pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "localization", "unexpected end of file");
|
||||
break;
|
||||
}
|
||||
|
||||
if(pReplacement[0] != '=' || pReplacement[1] != '=' || pReplacement[2] != ' ')
|
||||
{
|
||||
str_format(aBuf, sizeof(aBuf), "malform replacement line for '%s'", aOrigin);
|
||||
pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "localization", aBuf);
|
||||
continue;
|
||||
}
|
||||
|
||||
pReplacement += 3;
|
||||
AddString(aOrigin, pReplacement);
|
||||
pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "localizations", aError);
|
||||
return false;
|
||||
}
|
||||
io_close(IoHandle);
|
||||
|
||||
// extract data
|
||||
const json_value &rStart = (*pJsonData)["translated strings"];
|
||||
if(rStart.type == json_array)
|
||||
{
|
||||
for(int i = 0; i < rStart.u.array.length; ++i)
|
||||
AddString((const char *)rStart[i]["or"], (const char *)rStart[i]["tr"]);
|
||||
}
|
||||
|
||||
// clean up
|
||||
json_value_free(pJsonData);
|
||||
m_CurrentVersion = ++m_VersionCounter;
|
||||
return true;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue