Skip to content

Commit

Permalink
Serval was down until langtags.json was added back. (#194)
Browse files Browse the repository at this point in the history
* Serval was down until langtags.json was added back.
Add test to force download.

* Reviwer comments
  • Loading branch information
johnml1135 authored Apr 25, 2024
1 parent 448bd1e commit 9ff51e6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 15 deletions.
29 changes: 25 additions & 4 deletions src/SIL.Machine.AspNetCore/Services/LanguageTagService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,35 @@ public LanguageTagService()
_flores200Languages = InitializeFlores200Languages();
}

private static Dictionary<string, string> InitializeDefaultScripts()
protected virtual void InitializeSldrLanguageTags()
{
Sldr.InitializeLanguageTags();
}

private Dictionary<string, string> 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<string, string>();
Dictionary<string, string> tempDefaultScripts = new();
foreach (JsonNode? entry in json!.AsArray())
{
if (entry is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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));
}

Expand All @@ -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"));
}
}

0 comments on commit 9ff51e6

Please sign in to comment.