| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | |
|
| | 5 | | namespace Itinero.Instructions; |
| | 6 | |
|
| | 7 | | /// <summary> |
| | 8 | | /// Extensions on top of a generated route and base instructions object. |
| | 9 | | /// </summary> |
| | 10 | | // ReSharper disable once InconsistentNaming |
| | 11 | | public static class IRouteAndBaseInstructionsExtensions |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Generates text for all the base instructions for the given language code. |
| | 15 | | /// </summary> |
| | 16 | | /// <param name="routeAndBaseInstructions">The route and base instructions.</param> |
| | 17 | | /// <param name="languageCode">The language code.</param> |
| | 18 | | public static IRouteAndInstructions ForLanguage(this IRouteAndBaseInstructions routeAndBaseInstructions, |
| | 19 | | string languageCode = "en") |
| 2 | 20 | | { |
| 2 | 21 | | if (!routeAndBaseInstructions.Languages.TryGetValue(languageCode, out var languageGenerator)) |
| 0 | 22 | | { |
| 0 | 23 | | throw new ArgumentOutOfRangeException($"Language {languageCode} not configured"); |
| | 24 | | } |
| | 25 | |
|
| 2 | 26 | | var instructions = routeAndBaseInstructions.BaseInstructions.Select(baseInstruction => |
| 6 | 27 | | new Instruction(baseInstruction, new Dictionary<string, string>() { { languageCode, languageGenerator.ToText |
| | 28 | |
|
| 2 | 29 | | return new RouteAndInstructions(routeAndBaseInstructions.Route, instructions); |
| 2 | 30 | | } |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Generates text for all the base instructions for the given language code. |
| | 34 | | /// </summary> |
| | 35 | | /// <param name="routeAndBaseInstructions">The route and base instructions.</param> |
| | 36 | | /// <param name="languageCodes">The language codes.</param> |
| | 37 | | public static IRouteAndInstructions ForLanguage(this IRouteAndBaseInstructions routeAndBaseInstructions, |
| | 38 | | IEnumerable<string> languageCodes) |
| 0 | 39 | | { |
| 0 | 40 | | var languageGenerators = languageCodes.Select(languageCode => |
| 0 | 41 | | { |
| 0 | 42 | | if (!routeAndBaseInstructions.Languages.TryGetValue(languageCode, out var languageGenerator)) |
| 0 | 43 | | { |
| 0 | 44 | | throw new ArgumentOutOfRangeException($"Language {languageCode} not configured"); |
| 0 | 45 | | } |
| 0 | 46 | |
|
| 0 | 47 | | return (languageCode, languageGenerator); |
| 0 | 48 | | }); |
| | 49 | |
|
| 0 | 50 | | var instructions = routeAndBaseInstructions.BaseInstructions.Select(baseInstruction => |
| 0 | 51 | | { |
| 0 | 52 | | var text = new Dictionary<string, string>(); |
| 0 | 53 | | foreach (var (languageCode, languageGenerator) in languageGenerators) |
| 0 | 54 | | { |
| 0 | 55 | | text[languageCode] = languageGenerator.ToText(baseInstruction); |
| 0 | 56 | | } |
| 0 | 57 | |
|
| 0 | 58 | | return new Instruction(baseInstruction, text); |
| 0 | 59 | | }).ToList(); |
| | 60 | |
|
| 0 | 61 | | return new RouteAndInstructions(routeAndBaseInstructions.Route, instructions); |
| 0 | 62 | | } |
| | 63 | | } |