This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial rework of translations * implemented new repo files checking for translation files * finished modifying language parsing and loading * shortened translation method * removed old language file from scripts * made english item in initial language selection window appear first * fixed first boot not setting language properly * documented and ordered a bit * added failsafe for deleted translation files that were in use * small optimization for last change
- Loading branch information
1 parent
be36c06
commit 6906237
Showing
43 changed files
with
588 additions
and
3,072 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,6 @@ SetOutPath $INSTDIR | |
|
||
File SPCode.exe | ||
|
||
File lang_0_spcode.xml | ||
File License.txt | ||
File GPLv3.txt | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,6 @@ SetOutPath $INSTDIR | |
|
||
File SPCode.exe | ||
|
||
File lang_0_spcode.xml | ||
File License.txt | ||
File GPLv3.txt | ||
|
||
|
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,199 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.IO.Compression; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Windows; | ||
using System.Xml; | ||
using Octokit; | ||
using SPCode.Utils; | ||
using static SPCode.Utils.DefaultTranslations; | ||
|
||
namespace SPCode.Interop | ||
{ | ||
public class TranslationProvider | ||
{ | ||
public string[] AvailableLanguageIDs; | ||
public string[] AvailableLanguages; | ||
|
||
public List<string> AvailableLanguageIDs = new(); | ||
public List<string> AvailableLanguages = new(); | ||
public bool IsDefault = true; | ||
|
||
private readonly Dictionary<string, string> LangDict = new(StringComparer.OrdinalIgnoreCase); | ||
private readonly string _tempDir = Paths.GetTempDirectory(); | ||
private readonly string _translationsDir = Paths.GetTranslationsDirectory(); | ||
private static readonly Dictionary<string, string> _langDictionary = new(StringComparer.OrdinalIgnoreCase); | ||
private Release _latestVersion; | ||
|
||
public TranslationProvider() | ||
{ | ||
// Make sure translations dir exists | ||
if (!Directory.Exists(_translationsDir)) | ||
{ | ||
Directory.CreateDirectory(_translationsDir); | ||
} | ||
|
||
if (IsUpdateAvailable()) | ||
{ | ||
UpdateTranslations(); | ||
} | ||
|
||
ParseTranslationFiles(); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the translation of the specified phrase. | ||
/// </summary> | ||
/// <param name="phrase">The phrase to return translated</param> | ||
/// <returns></returns> | ||
public string Get(string phrase) | ||
public static string Translate(string phrase) | ||
{ | ||
return LangDict.ContainsKey(phrase) ? LangDict[phrase] : "<empty>"; | ||
return _langDictionary.ContainsKey(phrase) ? _langDictionary[phrase] : "<empty>"; | ||
} | ||
|
||
/// <summary> | ||
/// Loads the specified language. | ||
/// </summary> | ||
/// <param name="lang">The language to load</param> | ||
/// <param name="Initial"></param> | ||
public void LoadLanguage(string lang, bool Initial = false) | ||
/// <param name="initial">Whether this was the startup initial method call</param> | ||
public void LoadLanguage(string lang, bool initial = false) | ||
{ | ||
DefaultLangDict.Keys.ToList().ForEach(x => LangDict[x] = DefaultLangDict[x]); | ||
var languageList = new List<string>(); | ||
var languageIDList = new List<string>(); | ||
languageList.Add("English"); | ||
languageIDList.Add(""); | ||
// This is probably the first boot ever | ||
if (lang == string.Empty) | ||
{ | ||
lang = Constants.DefaultLanguageID; | ||
Program.OptionsObject.Language = lang; | ||
} | ||
lang = lang.Trim().ToLowerInvariant(); | ||
IsDefault = (string.IsNullOrEmpty(lang) || lang.ToLowerInvariant() == "en") && Initial; | ||
if (File.Exists(Constants.LanguagesFile)) | ||
IsDefault = (string.IsNullOrEmpty(lang) || lang.ToLowerInvariant() == Constants.DefaultLanguageID) && initial; | ||
var doc = new XmlDocument(); | ||
|
||
try | ||
{ | ||
try | ||
// Fill with defaults first | ||
if (initial) | ||
{ | ||
var document = new XmlDocument(); | ||
document.Load(Constants.LanguagesFile); | ||
if (document.ChildNodes.Count < 1) | ||
doc.Load(Path.Combine(_translationsDir, Constants.DefaultTranslationsFile)); | ||
foreach (XmlNode node in doc.ChildNodes[0].ChildNodes) | ||
{ | ||
throw new Exception("No Root-Node: \"translations\" found"); | ||
_langDictionary.Add(node.Name, node.InnerText); | ||
} | ||
|
||
XmlNode rootLangNode = null; | ||
foreach (XmlNode childNode in document.ChildNodes[0].ChildNodes) | ||
// Return if the attempted language to load is the default one | ||
if (lang == Constants.DefaultLanguageID) | ||
{ | ||
var lID = childNode.Name; | ||
var lNm = lID; | ||
if (childNode.Name.ToLowerInvariant() == lang) | ||
{ | ||
rootLangNode = childNode; | ||
} | ||
if (childNode.FirstChild.Name.ToLowerInvariant() == "language") | ||
{ | ||
lNm = childNode.FirstChild.InnerText; | ||
} | ||
languageList.Add(lNm); | ||
languageIDList.Add(lID); | ||
return; | ||
} | ||
if (rootLangNode != null) | ||
} | ||
|
||
var file = Path.Combine(_translationsDir, $"{lang}.xml"); | ||
if (!File.Exists(file)) | ||
{ | ||
UpdateTranslations(); | ||
LoadLanguage(lang); | ||
return; | ||
} | ||
else | ||
{ | ||
doc.Load(file); | ||
|
||
// Replace existing keys with the ones available in this file | ||
foreach (XmlNode node in doc.ChildNodes[0].ChildNodes) | ||
{ | ||
foreach (XmlNode node in rootLangNode.ChildNodes) | ||
{ | ||
if (node.NodeType == XmlNodeType.Comment) | ||
{ | ||
continue; | ||
} | ||
var nn = node.Name.ToLowerInvariant(); | ||
var nv = node.InnerText; | ||
LangDict[nn] = nv; | ||
} | ||
_langDictionary[node.Name] = node.InnerText; | ||
} | ||
} | ||
catch (Exception e) | ||
} | ||
catch (Exception) | ||
{ | ||
throw; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets all of the translation files and adds them to the global available languages list. | ||
/// </summary> | ||
public void ParseTranslationFiles() | ||
{ | ||
try | ||
{ | ||
var filesDir = Directory.GetFiles(_translationsDir).Where(x => x.EndsWith(".xml")); | ||
foreach (var file in filesDir) | ||
{ | ||
// Create wrapper | ||
var fInfo = new FileInfo(file); | ||
|
||
// Parse content in an XML object | ||
var doc = new XmlDocument(); | ||
doc.LoadXml(File.ReadAllText(fInfo.FullName)); | ||
|
||
// Get language name and ID | ||
var langName = doc.ChildNodes[0].ChildNodes | ||
.Cast<XmlNode>() | ||
.Single(x => x.Name == "language") | ||
.InnerText; | ||
var langID = fInfo.Name.Substring(0, fInfo.Name.IndexOf('.')); | ||
|
||
// Add file to the available languages lists | ||
AvailableLanguages.Add(langName); | ||
AvailableLanguageIDs.Add(langID); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
MessageBox.Show($"There was a problem while updating the translations file.\n" + | ||
$"Details: {ex.Message}"); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Downloads the latest translation files release from GitHub for SPCode to parse them. | ||
/// </summary> | ||
public void UpdateTranslations() | ||
{ | ||
// Clear temp folder before beggining | ||
DirUtils.ClearTempFolder(); | ||
|
||
// Download latest release zip file | ||
var wc = new WebClient(); | ||
var downloadedFile = Path.Combine(_tempDir, "langs.zip"); | ||
wc.Headers.Add(HttpRequestHeader.UserAgent, Constants.ProductHeaderValueName); | ||
wc.DownloadFile(_latestVersion.ZipballUrl, downloadedFile); | ||
|
||
// Decompress and replace all of its files | ||
ZipFile.ExtractToDirectory(downloadedFile, _tempDir); | ||
var filesDir = Directory.GetFiles(Directory.GetDirectories(_tempDir)[0]).Where(x => x.EndsWith(".xml")); | ||
foreach (var file in filesDir) | ||
{ | ||
// Create wrapper | ||
var fInfo = new FileInfo(file); | ||
|
||
// Replace current file with this one | ||
var destination = Path.Combine(_translationsDir, fInfo.Name); | ||
if (File.Exists(destination)) | ||
{ | ||
MessageBox.Show("An error occured while reading the language-file. Without them, the editor wont show translations." + Environment.NewLine + "Details: " + e.Message | ||
, "Error while reading configs." | ||
, MessageBoxButton.OK | ||
, MessageBoxImage.Warning); | ||
File.Delete(destination); | ||
} | ||
File.Move(fInfo.FullName, destination); | ||
} | ||
AvailableLanguages = languageList.ToArray(); | ||
AvailableLanguageIDs = languageIDList.ToArray(); | ||
|
||
// Delete all temp folder contents | ||
DirUtils.ClearTempFolder(); | ||
|
||
// Update version to options object | ||
Program.OptionsObject.TranslationsVersion = int.Parse(_latestVersion.Name); | ||
} | ||
|
||
/// <summary> | ||
/// Compares the stored version of the translations release with the one from GitHub | ||
/// </summary> | ||
/// <returns>Whether there's an update available</returns> | ||
public bool IsUpdateAvailable() | ||
{ | ||
var client = new GitHubClient(new ProductHeaderValue(Constants.ProductHeaderValueName)); | ||
var versionStored = Program.OptionsObject.TranslationsVersion; | ||
|
||
_latestVersion = client.Repository.Release.GetAll(Constants.OrgName, | ||
Constants.TranslationsRepoName).Result[0]; | ||
|
||
return versionStored < int.Parse(_latestVersion.Name); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.