|
| 1 | +// This Source Code Form is subject to the terms of the MIT License. |
| 2 | +// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT. |
| 3 | +// Copyright (C) Leszek Pomianowski and Lepo.i18n Contributors. |
| 4 | +// All Rights Reserved. |
| 5 | + |
| 6 | +using Lepo.i18n.IO; |
| 7 | +using Lepo.i18n.Json.Converters; |
| 8 | +using Lepo.i18n.Json.Models; |
| 9 | +using Lepo.i18n.Json.Models.v1; |
| 10 | + |
| 11 | +namespace Lepo.i18n.Json; |
| 12 | + |
| 13 | +/// <summary> |
| 14 | +/// Provides extension methods for the <see cref="LocalizationBuilder"/> class. |
| 15 | +/// </summary> |
| 16 | +public static class LocalizationBuilderExtensions |
| 17 | +{ |
| 18 | + private static readonly JsonSerializerOptions DefaultJsonSerializerOptions = |
| 19 | + new() |
| 20 | + { |
| 21 | + PropertyNameCaseInsensitive = true, |
| 22 | + AllowTrailingCommas = true, |
| 23 | + Converters = { new TranslationsContainerConverter() } |
| 24 | + }; |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Loads localization data from a JSON file in the calling assembly. |
| 28 | + /// </summary> |
| 29 | + /// <param name="builder">The <see cref="LocalizationBuilder"/> to add the localization data to.</param> |
| 30 | + /// <param name="path">The path to the JSON file.</param> |
| 31 | + /// <param name="culture">The culture of the localization data.</param> |
| 32 | + /// <returns>The updated <see cref="LocalizationBuilder"/>.</returns> |
| 33 | + public static LocalizationBuilder FromJson( |
| 34 | + this LocalizationBuilder builder, |
| 35 | + string path, |
| 36 | + CultureInfo culture |
| 37 | + ) |
| 38 | + { |
| 39 | + return builder.FromJson(Assembly.GetCallingAssembly(), path, culture); |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Loads localization data from a JSON file in the specified assembly. |
| 44 | + /// </summary> |
| 45 | + /// <param name="builder">The <see cref="LocalizationBuilder"/> to add the localization data to.</param> |
| 46 | + /// <param name="assembly">The assembly that contains the JSON file.</param> |
| 47 | + /// <param name="path">The path to the JSON file.</param> |
| 48 | + /// <param name="culture">The culture of the localization data.</param> |
| 49 | + /// <returns>The updated <see cref="LocalizationBuilder"/>.</returns> |
| 50 | + public static LocalizationBuilder FromJson( |
| 51 | + this LocalizationBuilder builder, |
| 52 | + Assembly assembly, |
| 53 | + string path, |
| 54 | + CultureInfo culture |
| 55 | + ) |
| 56 | + { |
| 57 | + if (!path.EndsWith(".json")) |
| 58 | + { |
| 59 | + throw new ArgumentException( |
| 60 | + $"Parameter {nameof(path)} in {nameof(FromJson)} must be path to the JSON file." |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + string? contents = EmbeddedResourceReader.ReadToEnd(path, assembly); |
| 65 | + |
| 66 | + if (contents is null) |
| 67 | + { |
| 68 | + throw new LocalizationBuilderException( |
| 69 | + $"Resource {path} not found in assembly {assembly.FullName}." |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + Version schemaVersion = |
| 74 | + new( |
| 75 | + JsonSerializer |
| 76 | + .Deserialize<ITranslationsContainer>(contents, DefaultJsonSerializerOptions) |
| 77 | + ?.Version ?? "1.0.0" |
| 78 | + ); |
| 79 | + |
| 80 | + if (!schemaVersion.Major.Equals(1)) |
| 81 | + { |
| 82 | + throw new LocalizationBuilderException( |
| 83 | + $"Localization file with schema version \"{schemaVersion.ToString() ?? "unknown"}\" is not supported." |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + TranslationFile? translationFile = JsonSerializer.Deserialize<TranslationFile>( |
| 88 | + contents, |
| 89 | + DefaultJsonSerializerOptions |
| 90 | + ); |
| 91 | + |
| 92 | + if (translationFile is null) |
| 93 | + { |
| 94 | + throw new LocalizationBuilderException("Unable to extract data from json file."); |
| 95 | + } |
| 96 | + |
| 97 | + Dictionary<string, string> localizedStrings = new(); |
| 98 | + |
| 99 | + foreach (TranslationEntity localizedString in translationFile.Strings) |
| 100 | + { |
| 101 | + if (localizedStrings.ContainsKey(localizedString.Name)) |
| 102 | + { |
| 103 | + throw new LocalizationBuilderException( |
| 104 | + $"The {path} file contains duplicate \"{localizedString.Name}\" keys." |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + localizedStrings.Add(localizedString.Name, localizedString.Value); |
| 109 | + } |
| 110 | + |
| 111 | + builder.AddLocalization( |
| 112 | + new LocalizationSet( |
| 113 | + Path.GetFileNameWithoutExtension(path).Trim().ToLowerInvariant(), |
| 114 | + culture, |
| 115 | + localizedStrings! |
| 116 | + ) |
| 117 | + ); |
| 118 | + |
| 119 | + return builder; |
| 120 | + } |
| 121 | +} |
0 commit comments