public static void Main(string[] args) { #region Snippet // Initialize service collection IServiceCollection serviceCollection = new ServiceCollection(); // Add IAssetBuilder, an instruction to construct one later serviceCollection.AddSingleton <IAssetBuilder, AssetBuilder>(); // Add IAssetSource, that will construct cache cache serviceCollection.AddSingleton <IAssetSource>(new AssetCacheSource(o => o.AddResourceCache().AddStringsCache().AddCulturesCache())); // Add IAssetSource, that adds strings Dictionary <string, string> strings = new Dictionary <string, string> { { "en:hello", "Hello World!" } }; serviceCollection.AddSingleton <IAssetSource>(new AssetInstanceSource(new StringAsset(strings, LineParameterPrinter.Default))); // Add delegate to forward IAsset request to IAssetBuilder serviceCollection.AddSingleton <IAsset>(s => s.GetService <IAssetBuilder>().Build()); // Create service scope using (ServiceProvider serviceScope = serviceCollection.BuildServiceProvider()) { // Construct new asset IAsset asset = serviceScope.GetService <IAsset>(); // Create string key ILine key = new LineRoot().Key("hello").Culture("en"); // Request string IString value = asset.GetLine(key).GetString(); // Print result Console.WriteLine(value); } #endregion Snippet }
public static void Main(string[] args) { #region Snippet // Initialize service collection IServiceCollection serviceCollection = new ServiceCollection(); // Add IAssetBuilder, ILineRoot and ICulturePolicy to service collection serviceCollection.AddLexicalLocalization( addStringLocalizerService: false, addCulturePolicyService: false, useGlobalInstance: false, addCache: true); // Add dictionary of strings Dictionary <string, string> strings = new Dictionary <string, string> { { "en:hello", "Hello World!" } }; serviceCollection.AddSingleton <IAssetSource>(new AssetInstanceSource(new StringAsset(strings, LineParameterPrinter.Default))); // Create service scope using (ServiceProvider serviceScope = serviceCollection.BuildServiceProvider()) { // Construct new asset IAsset asset = serviceScope.GetService <IAsset>(); // Create string key ILine key = new LineRoot().Key("hello").Culture("en"); // Request string IString value = asset.GetLine(key).GetString(); // Print result Console.WriteLine(value); } #endregion Snippet }
public static void Main(string[] args) { // Create dictionary of strings Dictionary <string, string> strings = new Dictionary <string, string> { { "en:hello", "Hello World!" } }; #region Snippet // Create AssetBuilder IAssetBuilder builder = new AssetBuilder(); // Add IAssetSource that adds cache builder.AddCache(); // Add IAssetSource that adds strings builder.AddStrings(strings, LineParameterPrinter.Default); #endregion Snippet // Instantiate IAsset IAsset asset = builder.Build(); // Create string key ILine key = new LineRoot().Key("hello").Culture("en"); // Request string IString value = asset.GetLine(key).GetString(); // Print result Console.WriteLine(value); }
public static void Main(string[] args) { #region Snippet // Create dictionary of strings Dictionary <string, string> strings = new Dictionary <string, string> { { "en:hello", "Hello World!" } }; // Create IAssetSource that adds cache IAssetSource assetSource_0 = new AssetCacheSource(c => c.AddResourceCache().AddStringsCache().AddCulturesCache()); // Create IAssetSource that static reference of IAsset (string dictionary) IAssetSource assetSource_1 = new AssetInstanceSource(new StringAsset(strings, LineParameterPrinter.Default)); // Create AssetBuilder IAssetBuilder builder = new AssetBuilder(assetSource_0, assetSource_1); // Instantiate IAsset IAsset asset = builder.Build(); // Create string key ILine key = new LineRoot().Key("hello").Culture("en"); // Request value IString value = asset.GetLine(key).GetString(); // Print result Console.WriteLine(value); #endregion Snippet }
/// <summary> /// Get key-matching value from associated <see cref="IAsset"/>. /// Ignores culture policy, ignores inlining, ignores formatting. /// /// <see cref="ResolveFormatString(ILine)"/> to resolve string with active culture from <see cref="ICulturePolicy"/>. /// </summary> /// <param name="key"></param> /// <returns>format string</returns> /// <exception cref="LineException">If resolving failed or resolver was not found</exception> public static IString GetAssetValue(this ILine key) { IAsset asset = key.FindAsset(); if (asset == null) { throw new LineException(key, "String resolver was not found."); } ILine line = asset.GetLine(key); if (line == null) { throw new LineException(key, "String was not found."); } return(line.GetString()); }
/// <summary> /// Try get key-matching value from associated <see cref="IAsset"/>. /// Ignores culture policy, ignores inlining, ignores formatting. /// /// <see cref="ResolveFormatString(ILine)"/> to resolve string with active culture from <see cref="ICulturePolicy"/>. /// </summary> /// <param name="key"></param> /// <param name="result"></param> /// <returns>format string, or null if format string was not found, or if resolver was not found</returns> public static bool TryGetAssetValue(this ILine key, out IString result) { IAsset asset = key.FindAsset(); if (asset == null) { result = null; return(false); } ILine line = asset.GetLine(key); if (line == null) { result = null; return(false); } IString str = line.GetString(); result = str; return(str.Text != null); }
/// <summary> /// Resolve keys into a line. Searches from asset, inlines and key itself. /// /// If resolving causes an exception, it is caught, logged and status in <paramref name="features"/> is updated. /// </summary> /// <param name="key"></param> /// <param name="features"></param> /// <param name="culture">The culture that matched</param> /// <returns>matching line or null</returns> ILine ResolveKeyToLine(ILine key, ref LineFeatures features, ref CultureInfo culture) { try { // Tmp variable ILine line = null; // Explicit culture request if (culture != null) { // 0 - main preference, 1+ - fallback preferences int preferenceIndex = 0; for (CultureInfo ci = culture, prev = null; ci != null && ci != prev; prev = ci, ci = ci.Parent) { ILine key_with_culture = preferenceIndex == 0 ? key : key.Prune(NoCultureQualifier.Default).Culture(ci); // Should invariant culture be tested here?? foreach (var stage in ResolveSequence) { switch (stage) { // Try asset case ResolveSource.Asset: for (int i = 0; i < features.Assets.Count; i++) { try { IAsset asset = features.Assets[i]; if ((line = asset.GetLine(key_with_culture)) != null) { // Up status with source info features.Status.UpResolve(LineStatus.ResolveOkFromAsset); // Up status with culture info LineStatus cultureStatus = preferenceIndex == 0 // Request matched with first preference ? (ci.Name == "" ? LineStatus.CultureOkRequestMatchedInvariantCulture : ci.IsNeutralCulture ? LineStatus.CultureOkRequestMatchedLanguage : LineStatus.CultureOkRequestMatchedLanguageAndRegion) // Request matched with a fallback preference : (ci.Name == "" ? Localization.LineStatus.CultureErrorRequestMatchedInvariantCulture : ci.IsNeutralCulture ? LineStatus.CultureWarningRequestMatchedLanguage : Localization.LineStatus.CultureWarningRequestMatchedLanguageAndRegion); features.Status.UpCulture(cultureStatus); features.ScanFeatures(line); return(line); } } catch (Exception e) { features.Status.UpResolve(LineStatus.ResolveErrorAssetException); features.Log(e); } } break; // Try inlines case ResolveSource.Inline: for (int i = 0; i < features.Inlines.Count; i++) { try { if (features.Inlines[i].TryGetValue(key_with_culture, out line)) { // Up status with source info features.Status.UpResolve(LineStatus.ResolveOkFromInline); // Up status with culture info LineStatus cultureStatus = preferenceIndex == 0 // Request matched with first preference ? (ci.Name == "" ? LineStatus.CultureOkRequestMatchedInvariantCulture : ci.IsNeutralCulture ? LineStatus.CultureOkRequestMatchedLanguage : LineStatus.CultureOkRequestMatchedLanguageAndRegion) // Request matched with a fallback preference : (ci.Name == "" ? Localization.LineStatus.CultureErrorRequestMatchedInvariantCulture : ci.IsNeutralCulture ? LineStatus.CultureWarningRequestMatchedLanguage : Localization.LineStatus.CultureWarningRequestMatchedLanguageAndRegion); features.Status.UpCulture(cultureStatus); features.ScanFeatures(line); return(line); } } catch (Exception e) { features.Status.UpResolve(LineStatus.ResolveErrorInlinesException); features.Log(e); } } break; case ResolveSource.Line: // Key has explicit culture and value, use the value if (preferenceIndex == 0 && (features.String != null || features.StringText != null)) { // Up status with source info features.Status.UpResolve(LineStatus.CultureWarningRequestMatchedInvariantCulture); // Up status with culture info LineStatus cultureStatus = culture == null || culture.Name == "" ? LineStatus.CultureOkRequestMatchedInvariantCulture : LineStatus.CultureWarningRequestMatchedInvariantCulture; features.Status.UpCulture(cultureStatus); if (culture == null) { culture = CultureInfo.InvariantCulture; } return(key); } break; } } preferenceIndex++; } // No matching value was found for the requested key and the explicit culture in the key. features.Status.UpCulture(LineStatus.CultureFailedRequestNoMatch); } // Try with cultures from the culture policy CultureInfo[] cultures = features.CulturePolicy?.Cultures; if (culture == null && cultures != null) { try { for (int preferenceIndex = 0; preferenceIndex < cultures.Length; preferenceIndex++) { CultureInfo ci = cultures[preferenceIndex]; // Has already been tested above? Skip if (ci == culture) { continue; } ILine key_with_culture = key.Culture(ci); foreach (var stage in ResolveSequence) { switch (stage) { // Try asset case ResolveSource.Asset: for (int i = 0; i < features.Assets.Count; i++) { try { IAsset asset = features.Assets[i]; if ((line = asset.GetLine(key_with_culture)) != null) { culture = ci; // Up status with source info features.Status.UpResolve(LineStatus.ResolveOkFromAsset); // Up status with culture info LineStatus cultureStatus = preferenceIndex == 0 // Request matched with first preference ? (ci.Name == "" ? LineStatus.CultureOkCulturePolicyMatchedInvariantCulture : ci.IsNeutralCulture ? LineStatus.CultureOkCulturePolicyMatchedLanguage : LineStatus.CultureOkCulturePolicyMatchedLanguageAndRegion) // Request matched with a fallback preference : (ci.Name == "" ? Localization.LineStatus.CultureErrorCulturePolicyMatchedInvariantCulture : ci.IsNeutralCulture ? LineStatus.CultureWarningCulturePolicyMatchedLanguage : Localization.LineStatus.CultureWarningCulturePolicyMatchedLanguageAndRegion); features.Status.UpCulture(cultureStatus); features.ScanFeatures(line); return(line); } } catch (Exception e) { features.Status.UpResolve(LineStatus.ResolveErrorAssetException); features.Log(e); } } break; // Try inlines case ResolveSource.Inline: for (int i = 0; i < features.Inlines.Count; i++) { try { if (features.Inlines[i].TryGetValue(key_with_culture, out line)) { culture = ci; // Up status with source info features.Status.UpResolve(LineStatus.ResolveOkFromInline); // Up status with culture info LineStatus cultureStatus = preferenceIndex == 0 // Request matched with first preference ? (ci.Name == "" ? LineStatus.CultureOkCulturePolicyMatchedInvariantCulture : ci.IsNeutralCulture ? LineStatus.CultureOkCulturePolicyMatchedLanguage : LineStatus.CultureOkCulturePolicyMatchedLanguageAndRegion) // Request matched with a fallback preference : (ci.Name == "" ? Localization.LineStatus.CultureErrorCulturePolicyMatchedInvariantCulture : ci.IsNeutralCulture ? LineStatus.CultureWarningCulturePolicyMatchedLanguage : Localization.LineStatus.CultureWarningCulturePolicyMatchedLanguageAndRegion); features.Status.UpCulture(cultureStatus); features.ScanFeatures(line); return(line); } } catch (Exception e) { features.Status.UpResolve(LineStatus.ResolveErrorInlinesException); features.Log(e); } } break; case ResolveSource.Line: if ((features.String != null || features.StringText != null) && ci.Equals(features.Culture)) { culture = ci; // Up status with source info features.Status.UpResolve(LineStatus.ResolveOkFromLine); // Up status with culture info LineStatus cultureStatus = preferenceIndex == 0 // Request matched with first preference ? (ci.Name == "" ? LineStatus.CultureOkCulturePolicyMatchedInvariantCulture : ci.IsNeutralCulture ? LineStatus.CultureOkCulturePolicyMatchedLanguage : LineStatus.CultureOkCulturePolicyMatchedLanguageAndRegion) // Request matched with a fallback preference : (ci.Name == "" ? Localization.LineStatus.CultureErrorCulturePolicyMatchedInvariantCulture : ci.IsNeutralCulture ? LineStatus.CultureWarningCulturePolicyMatchedLanguage : Localization.LineStatus.CultureWarningCulturePolicyMatchedLanguageAndRegion); features.Status.UpCulture(cultureStatus); return(key); } break; } } } } catch (Exception e) { features.Status.UpResolve(LineStatus.CultureErrorCulturePolicyException); features.Log(e); } } // No request culture - try this _after_ culture policy if (culture == null) { foreach (var stage in ResolveSequence) { switch (stage) { // Try asset case ResolveSource.Asset: for (int i = 0; i < features.Assets.Count; i++) { try { IAsset asset = features.Assets[i]; if ((line = asset.GetLine(key)) != null) { // Up status with source info features.Status.UpResolve(LineStatus.ResolveOkFromAsset); line.TryGetCultureInfo(out culture); if (culture == null) { culture = CultureInfo.InvariantCulture; } // Up status with culture info LineStatus cultureStatus = culture.Name == "" ? LineStatus.CultureOkMatchedInvariantCulture : culture.IsNeutralCulture ? LineStatus.CultureOkMatchedLanguage : LineStatus.CultureOkMatchedLanguageAndRegion; features.Status.UpCulture(cultureStatus); features.ScanFeatures(line); return(line); } } catch (Exception e) { features.Status.UpResolve(LineStatus.ResolveErrorAssetException); features.Log(e); } } break; // Try inlines case ResolveSource.Inline: for (int i = 0; i < features.Inlines.Count; i++) { try { if (features.Inlines[i].TryGetValue(key, out line)) { // Up status with source info features.Status.UpResolve(LineStatus.ResolveOkFromInline); line.TryGetCultureInfo(out culture); if (culture == null) { culture = CultureInfo.InvariantCulture; } // Up status with culture info LineStatus cultureStatus = culture.Name == "" ? LineStatus.CultureOkMatchedInvariantCulture : culture.IsNeutralCulture ? LineStatus.CultureOkMatchedLanguage : LineStatus.CultureOkMatchedLanguageAndRegion; features.Status.UpCulture(cultureStatus); features.ScanFeatures(line); return(line); } } catch (Exception e) { features.Status.UpResolve(LineStatus.ResolveErrorInlinesException); features.Log(e); } } break; case ResolveSource.Line: // Key has explicit culture and value, use the value if (features.String != null || features.StringText != null) { // Up status with source info features.Status.UpResolve(LineStatus.ResolveOkFromLine); if (culture == null) { culture = CultureInfo.InvariantCulture; } LineStatus cultureStatus = LineStatus.CultureOkMatchedInvariantCulture; features.Status.UpCulture(cultureStatus); return(key); } break; } } } // No matching value was found for the requested key and the explicit culture in the key. features.Status.UpCulture(cultures != null ? LineStatus.CultureFailedCulturePolicyNoMatch : LineStatus.CultureFailedRequestNoMatch); // No match features.Status.UpResolve(LineStatus.ResolveErrorNoMatch); culture = null; return(null); } catch (Exception e) { // Uncaptured error features.Status.UpResolve(LineStatus.ResolveError); features.Log(e); culture = null; return(null); } }