Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
itssimple committed Oct 6, 2023
2 parents 4def5f8 + cd58c8b commit 9d55fca
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
22 changes: 16 additions & 6 deletions MN.L10n/FileProviders/FileDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class FileDataProvider : IL10nDataProvider
private string PhraseFile { get; set; }
private string LanguagesFile { get; set; }
private string LanguageFile { get; set; }
public bool SaveChangesToDisk { get; set; } = true;

public FileDataProvider(string path, string l10nFileName = "phrases.json", string l10nPhraseFileNameFormat = "language-{0}.json", string l10nLanguagesFileName = "languages.json")
{
FilePath = path;
Expand Down Expand Up @@ -61,7 +63,7 @@ public L10n LoadLanguages()
languages.Add(new L10nLanguageItem { LanguageId = locLang });
}

File.WriteAllText(langPath, JsonConvert.SerializeObject(languages, SerializerOptions));
WriteToDiskIfAllowed(langPath, JsonConvert.SerializeObject(languages, SerializerOptions));
}
}

Expand All @@ -80,7 +82,7 @@ public L10n LoadLanguages()
{
Languages = languages
};
File.WriteAllText(phrasePath, JsonConvert.SerializeObject(l10n, SerializerOptions));
WriteToDiskIfAllowed(phrasePath, JsonConvert.SerializeObject(l10n, SerializerOptions));

return l10n;
}
Expand Down Expand Up @@ -109,7 +111,7 @@ public void LoadTranslations(L10n l10n)
PluralizationRules = new List<string> { "0", "1" },
PluralRule = "n != 1"
};
File.WriteAllText(langFileName, JsonConvert.SerializeObject(nLang, SerializerOptions));
WriteToDiskIfAllowed(langFileName, JsonConvert.SerializeObject(nLang, SerializerOptions));
l10n.LanguagePhrases.TryAdd(lang, nLang);
}

Expand All @@ -129,7 +131,7 @@ public void LoadTranslations(L10n l10n)
public bool SaveL10n(L10n l10n)
{
var l10nFileContents = JsonConvert.SerializeObject(l10n, SerializerOptions);
File.WriteAllText(Path.Combine(FilePath, PhraseFile), l10nFileContents);
WriteToDiskIfAllowed(Path.Combine(FilePath, PhraseFile), l10nFileContents);

SaveTranslation(l10n);

Expand Down Expand Up @@ -204,7 +206,7 @@ public async Task<bool> LoadTranslationFromSources(L10n l10n, bool removeAllPhra
}

var langFileName = Path.Combine(FilePath, string.Format(LanguageFile, lang.LanguageId));
File.WriteAllText(langFileName, JsonConvert.SerializeObject(l10nLang, SerializerOptions));
WriteToDiskIfAllowed(langFileName, JsonConvert.SerializeObject(l10nLang, SerializerOptions));

if (token.IsCancellationRequested)
{
Expand All @@ -225,10 +227,18 @@ public bool SaveTranslation(L10n l10n)
var l10nLang = l10n.LanguagePhrases[lang.LanguageId];

var langFileName = Path.Combine(FilePath, string.Format(LanguageFile, lang.LanguageId));
File.WriteAllText(langFileName, JsonConvert.SerializeObject(l10nLang, SerializerOptions));
WriteToDiskIfAllowed(langFileName, JsonConvert.SerializeObject(l10nLang, SerializerOptions));
}

return true;
}

private void WriteToDiskIfAllowed(string pathAndFilename, string contents)
{
if (SaveChangesToDisk)
{
File.WriteAllText(pathAndFilename, contents);
}
}
}
}
4 changes: 2 additions & 2 deletions MN.L10n/MN.L10n.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ Translation package</Description>
<PackageProjectUrl>https://github.com/MultinetInteractive/MN.L10n</PackageProjectUrl>
<RepositoryType>git</RepositoryType>
<Copyright>© 20XX MultiNet Interactive AB</Copyright>
<Version>3.0.0</Version>
<Version>3.0.1</Version>
<AutoIncrementPackageRevision>True</AutoIncrementPackageRevision>
<PackageReleaseNotes>Now includes analyzer</PackageReleaseNotes>
<ApplicationIcon />
<OutputType>Library</OutputType>
<StartupObject></StartupObject>
<AssemblyVersion>3.0.0</AssemblyVersion>
<AssemblyVersion>3.0.1</AssemblyVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down

0 comments on commit 9d55fca

Please sign in to comment.