diff --git a/src/SIL.Machine.AspNetCore/Services/LanguageTagService.cs b/src/SIL.Machine.AspNetCore/Services/LanguageTagService.cs index bc2d64982..72dca5135 100644 --- a/src/SIL.Machine.AspNetCore/Services/LanguageTagService.cs +++ b/src/SIL.Machine.AspNetCore/Services/LanguageTagService.cs @@ -29,14 +29,35 @@ public LanguageTagService() _flores200Languages = InitializeFlores200Languages(); } - private static Dictionary InitializeDefaultScripts() + protected virtual void InitializeSldrLanguageTags() { Sldr.InitializeLanguageTags(); + } + + private Dictionary InitializeDefaultScripts() + { + InitializeSldrLanguageTags(); var cachedAllTagsPath = Path.Combine(Sldr.SldrCachePath, "langtags.json"); - using var stream = new FileStream(cachedAllTagsPath, FileMode.Open); + JsonNode? json; + + if (!File.Exists(cachedAllTagsPath)) + { + using HttpClient client = new(); + using HttpResponseMessage response = client.Send( + new HttpRequestMessage( + HttpMethod.Get, + "https://raw.githubusercontent.com/silnrsi/langtags/master/pub/langtags.json" + ) + ); + response.EnsureSuccessStatusCode(); + using Stream responseStream = response.Content.ReadAsStream(); + using FileStream fileStream = new(cachedAllTagsPath, FileMode.Create); + responseStream.CopyTo(fileStream); + } + using FileStream stream = new(cachedAllTagsPath, FileMode.Open); + json = JsonNode.Parse(stream); - var json = JsonNode.Parse(stream); - var tempDefaultScripts = new Dictionary(); + Dictionary tempDefaultScripts = new(); foreach (JsonNode? entry in json!.AsArray()) { if (entry is null) diff --git a/tests/SIL.Machine.AspNetCore.Tests/Services/LanguageTagServiceTests.cs b/tests/SIL.Machine.AspNetCore.Tests/Services/LanguageTagServiceTests.cs index ba664cb27..593ba8524 100644 --- a/tests/SIL.Machine.AspNetCore.Tests/Services/LanguageTagServiceTests.cs +++ b/tests/SIL.Machine.AspNetCore.Tests/Services/LanguageTagServiceTests.cs @@ -3,15 +3,6 @@ [TestFixture] public class LanguageTagServiceTests { - private readonly LanguageTagService _languageTagService; - - public LanguageTagServiceTests() - { - if (!Sldr.IsInitialized) - Sldr.Initialize(); - _languageTagService = new LanguageTagService(); - } - [Test] [TestCase("es", "spa_Latn", Description = "Iso639_1Code")] [TestCase("hne", "hne_Deva", Description = "Iso639_3Code")] @@ -30,7 +21,9 @@ public LanguageTagServiceTests() [TestCase("kor_Kore", "kor_Hang", Description = "KoreanScriptCorrection")] public void ConvertToFlores200CodeTest(string language, string internalCodeTruth) { - _languageTagService.ConvertToFlores200Code(language, out string internalCode); + if (!Sldr.IsInitialized) + Sldr.Initialize(); + new LanguageTagService().ConvertToFlores200Code(language, out string internalCode); Assert.That(internalCode, Is.EqualTo(internalCodeTruth)); } @@ -41,11 +34,36 @@ public void ConvertToFlores200CodeTest(string language, string internalCodeTruth [TestCase("xyz", "xyz", false)] public void GetLanguageInfoAsync(string languageCode, string? resolvedLanguageCode, bool nativeLanguageSupport) { - bool isNative = _languageTagService.ConvertToFlores200Code(languageCode, out string internalCode); + if (!Sldr.IsInitialized) + Sldr.Initialize(); + bool isNative = new LanguageTagService().ConvertToFlores200Code(languageCode, out string internalCode); Assert.Multiple(() => { Assert.That(internalCode, Is.EqualTo(resolvedLanguageCode)); Assert.That(isNative, Is.EqualTo(nativeLanguageSupport)); }); } + + public class TestLanguageTagService : LanguageTagService + { + // Don't call Sldr initialize to call + protected override void InitializeSldrLanguageTags() + { + // remove langtags.json to force download + var cachedAllTagsPath = Path.Combine(Sldr.SldrCachePath, "langtags.json"); + if (File.Exists(cachedAllTagsPath)) + File.Delete(cachedAllTagsPath); + Directory.CreateDirectory(Sldr.SldrCachePath); + } + } + + [Test] + public void BackupLangtagsJsonTest() + { + if (!Sldr.IsInitialized) + Sldr.Initialize(); + var service = new TestLanguageTagService(); + service.ConvertToFlores200Code("en", out string internalCode); + Assert.That(internalCode, Is.EqualTo("eng_Latn")); + } }