/// <summary> /// Try get file format by extension. /// </summary> /// <param name="map"></param> /// <param name="extension">extension to search, case insensitive</param> /// <returns>file format or null</returns> public static ILineFileFormat TryGet(this IReadOnlyDictionary <string, ILineFileFormat> map, string extension) { ILineFileFormat result = null; map.TryGetValue(extension, out result); return(result); }
/// <summary> /// Read strings from <paramref name="srcText"/> source. /// </summary> /// <param name="fileFormat"></param> /// <param name="srcText"></param> /// <param name="lineFormat"></param> /// <returns></returns> public static IEnumerable <KeyValuePair <string, IString> > ReadStringLines(this ILineFileFormat fileFormat, TextReader srcText, ILineFormat lineFormat = default) { if (fileFormat is ILineStringTextReader r5) { return(r5.ReadStringLines(srcText, lineFormat)); } if (fileFormat is ILineStringStreamReader r6) { return(r6.ReadStringLines(srcText.ReadStream(), lineFormat)); } if (fileFormat is ILineTextReader r1) { return(r1.ReadLines(srcText, lineFormat).ToStringLines(lineFormat)); } if (fileFormat is ILineStreamReader r3) { return(r3.ReadLines(srcText.ReadStream(), lineFormat).ToStringLines(lineFormat)); } if (fileFormat is ILineTreeTextReader r2) { return(r2.ReadLineTree(srcText, lineFormat).ToStringLines(lineFormat)); } if (fileFormat is ILineTreeStreamReader r4) { return(r4.ReadLineTree(srcText.ReadStream(), lineFormat).ToStringLines(lineFormat)); } throw new FileLoadException($"Cannot read localization with {fileFormat.GetType().FullName}"); }
/// <summary> /// Read lines from <paramref name="stream"/> source. /// </summary> /// <param name="fileFormat"></param> /// <param name="stream"></param> /// <param name="lineFormat"></param> /// <returns></returns> public static IEnumerable <ILine> ReadLines(this ILineFileFormat fileFormat, Stream stream, ILineFormat lineFormat = default) { if (fileFormat is ILineStreamReader r3) { return(r3.ReadLines(stream, lineFormat)); } if (fileFormat is ILineTextReader r1) { using (var txt = stream.ReadText()) return(r1.ReadLines(txt, lineFormat)); } if (fileFormat is ILineTreeStreamReader r4) { return(r4.ReadLineTree(stream, lineFormat).ToLines()); } if (fileFormat is ILineTreeTextReader r2) { using (var txt = stream.ReadText()) return(r2.ReadLineTree(txt, lineFormat).ToLines()); } if (fileFormat is ILineStringStreamReader r6) { return(r6.ReadStringLines(stream, lineFormat).ToLines(lineFormat)); } if (fileFormat is ILineStringTextReader r5) { using (var txt = stream.ReadText()) return(r5.ReadStringLines(txt, lineFormat).ToLines(lineFormat)); } throw new FileLoadException($"Cannot read localization with {fileFormat.GetType().FullName}"); }
/// <summary> /// Create (abstract) embedded reader. /// </summary> /// <param name="fileFormat"></param> /// <param name="assembly"></param> /// <param name="resourceName"></param> /// <param name="lineFormat"></param> /// <param name="throwIfNotFound"></param> public LineEmbeddedSource(ILineFileFormat fileFormat, Assembly assembly, string resourceName, ILineFormat lineFormat, bool throwIfNotFound) : base(assembly, resourceName, throwIfNotFound) { this.FileFormat = fileFormat ?? throw new ArgumentNullException(nameof(fileFormat)); this.LineFormat = lineFormat; this.Assembly = assembly ?? throw new ArgumentNullException(nameof(assembly)); this.ResourceName = resourceName ?? throw new ArgumentNullException(nameof(resourceName)); this.ThrowIfNotFound = throwIfNotFound; }
/// <summary> /// Add <paramref name="fileFormat"/>. /// </summary> /// <param name="map"></param> /// <param name="fileFormat"></param> /// <returns></returns> public static IDictionary <string, ILineFileFormat> Add(this IDictionary <string, ILineFileFormat> map, ILineFileFormat fileFormat) { string ext = fileFormat.Extension; if (ext == null) { throw new ArgumentNullException("Extension"); } if (map.ContainsKey(ext)) { throw new InvalidOperationException($"Already contains file format for {ext}."); } map[ext] = fileFormat; return(map); }
/// <summary> /// Create localization asset source that reads file <paramref name="filename"/>. /// </summary> /// <param name="fileFormat"></param> /// <param name="path">(optional) root folder</param> /// <param name="filename">non-rooted relative path, or rooted full path</param> /// <param name="lineFormat">(optional) possibly needed for string and line conversions. Used also for choosing whether to instantiate parameter into hint or key</param> /// <param name="throwIfNotFound">if file is not found and value is true, <see cref="FileNotFoundException"/> is thrown, otherwise zero elements are returned</param> /// <returns>asset source</returns> public static LineFileSource FileAssetSource(this ILineFileFormat fileFormat, string path, string filename, ILineFormat lineFormat = default, bool throwIfNotFound = true) { if (fileFormat is ILineTreeTextReader || fileFormat is ILineTreeStreamReader) { return(new LineTreeFileSource(fileFormat, path, filename, lineFormat, throwIfNotFound)); } else if (fileFormat is ILineTextReader || fileFormat is ILineStreamReader) { return(new KeyLineFileSource(fileFormat, path, filename, lineFormat, throwIfNotFound)); } else if (fileFormat is ILineStringTextReader || fileFormat is ILineStringStreamReader) { return(new StringLineFileSource(fileFormat, path, filename, lineFormat, throwIfNotFound)); } throw new ArgumentException($"Cannot create asset for {fileFormat}."); }
/// <summary> /// Create localization asset source that reads FileProvider resource at <paramref name="filepath"/>. /// </summary> /// <param name="fileFormat"></param> /// <param name="fileProvider"></param> /// <param name="filepath"></param> /// <param name="lineFormat">(optional) possibly needed for string and line conversions. Used also for choosing whether to instantiate parameter into hint or key</param> /// <param name="throwIfNotFound">if file is not found and value is true, <see cref="FileNotFoundException"/> is thrown, otherwise zero elements are returned</param> /// <returns>asset</returns> public static IAsset FileProviderAsset(this ILineFileFormat fileFormat, IFileProvider fileProvider, string filepath, ILineFormat lineFormat = default, bool throwIfNotFound = true) { if (fileFormat is ILineTreeTextReader || fileFormat is ILineTreeStreamReader) { return(new StringAsset().Add(fileFormat.FileProviderReaderAsLineTree(fileProvider, filepath, lineFormat, throwIfNotFound), lineFormat).Load()); } else if (fileFormat is ILineTextReader || fileFormat is ILineStreamReader) { return(new StringAsset().Add(fileFormat.FileProviderReader(fileProvider, filepath, lineFormat, throwIfNotFound), lineFormat).Load()); } else if (fileFormat is ILineStringTextReader || fileFormat is ILineStringStreamReader) { return(new StringAsset().Add(fileFormat.FileProviderReaderAsStringLines(fileProvider, filepath, lineFormat, throwIfNotFound), lineFormat).Load()); } throw new ArgumentException($"Cannot create asset for {fileFormat}."); }
/// <summary> /// Create localization asset source that reads embedded resource <paramref name="resourceName"/>. /// </summary> /// <param name="fileFormat"></param> /// <param name="assembly"></param> /// <param name="resourceName"></param> /// <param name="lineFormat">(optional) </param> /// <param name="throwIfNotFound">if file is not found and value is true, <see cref="FileNotFoundException"/> is thrown, otherwise zero elements are returned</param> /// <returns>asset source</returns> public static LineEmbeddedSource EmbeddedAssetSource(this ILineFileFormat fileFormat, Assembly assembly, string resourceName, ILineFormat lineFormat = default, bool throwIfNotFound = true) { if (fileFormat is ILineTreeTextReader || fileFormat is ILineTreeStreamReader) { return(new LineEmbeddedLineTreeSource(fileFormat, assembly, resourceName, lineFormat, throwIfNotFound)); } else if (fileFormat is ILineTextReader || fileFormat is ILineStreamReader) { return(new LineEmbeddedKeyLinesSource(fileFormat, assembly, resourceName, lineFormat, throwIfNotFound)); } else if (fileFormat is ILineStringTextReader || fileFormat is ILineStringStreamReader) { return(new LineEmbeddedStringLinesSource(fileFormat, assembly, resourceName, lineFormat, throwIfNotFound)); } throw new ArgumentException($"Cannot create asset for {fileFormat}."); }
/// <summary> /// Create localization asset from embedded resource<paramref name="resourceName"/>. /// /// File is reloaded if <see cref="IAssetExtensions.Reload(IAsset)"/> is called. /// </summary> /// <param name="fileFormat"></param> /// <param name="assembly"></param> /// <param name="resourceName"></param> /// <param name="lineFormat">(optional) </param> /// <param name="throwIfNotFound">if file is not found and value is true, <see cref="FileNotFoundException"/> is thrown, otherwise zero elements are returned</param> /// <returns>reloadable localization asset</returns> public static IAsset EmbeddedAsset(this ILineFileFormat fileFormat, Assembly assembly, string resourceName, ILineFormat lineFormat = default, bool throwIfNotFound = true) { if (fileFormat is ILineTreeTextReader || fileFormat is ILineTreeStreamReader) { return(new StringAsset().Add(fileFormat.EmbeddedReaderAsLineTree(assembly, resourceName, lineFormat, throwIfNotFound), lineFormat).Load()); } else if (fileFormat is ILineTextReader || fileFormat is ILineStreamReader) { return(new StringAsset().Add(fileFormat.EmbeddedReader(assembly, resourceName, lineFormat, throwIfNotFound), lineFormat).Load()); } else if (fileFormat is ILineStringTextReader || fileFormat is ILineStringStreamReader) { return(new StringAsset().Add(fileFormat.EmbeddedReaderAsStringLines(assembly, resourceName, lineFormat, throwIfNotFound), lineFormat).Load()); } throw new ArgumentException($"Cannot create asset for {fileFormat}."); }
/// <summary> /// Read localization lines from <see cref="Stream"/> into most suitable asset implementation. /// /// File cannot be reloaded. /// </summary> /// <param name="fileFormat"></param> /// <param name="stream"></param> /// <param name="lineFormat">(optional) possibly needed for string and line conversions. Used also for choosing whether to instantiate parameter into hint or key</param> /// <returns>localization asset</returns> public static IAsset StreamAsset(this ILineFileFormat fileFormat, Stream stream, ILineFormat lineFormat = default) { if (fileFormat is ILineTreeTextReader || fileFormat is ILineTreeStreamReader) { return(new StringAsset().Add(new ILineTree[] { fileFormat.ReadLineTree(stream, lineFormat) }, lineFormat).Load()); } else if (fileFormat is ILineTextReader || fileFormat is ILineStreamReader) { return(new StringAsset().Add(fileFormat.ReadLines(stream, lineFormat), lineFormat).Load()); } else if (fileFormat is ILineStringTextReader || fileFormat is ILineStringStreamReader) { return(new StringAsset().Add(fileFormat.ReadStringLines(stream, lineFormat), lineFormat).Load()); } throw new ArgumentException($"Cannot create asset for {fileFormat}."); }
/// <summary> /// Read lines from <paramref name="srcFilename"/> file source. /// </summary> /// <param name="fileFormat"></param> /// <param name="srcFilename"></param> /// <param name="lineFormat">(optional) possibly needed for string and line conversions. Used also for choosing whether to instantiate parameter into hint or key</param> /// <param name="throwIfNotFound">if file is not found and value is true, <see cref="FileNotFoundException"/> is thrown, otherwise zero elements are returned</param> /// <returns>enumerable of lines</returns> /// <exception cref="FileNotFoundException">thrown if file was not found and <paramref name="throwIfNotFound"/> is true</exception> /// <exception cref="IOException">on io error</exception> public static IEnumerable <ILine> ReadLines(this ILineFileFormat fileFormat, string srcFilename, ILineFormat lineFormat = default, bool throwIfNotFound = true) { if (!throwIfNotFound && !File.Exists(srcFilename)) { return(no_keylines); } try { if (fileFormat is ILineTextReader r1) { return(r1.ReadLines(srcFilename.ReadText(), lineFormat)); } if (fileFormat is ILineStreamReader r3) { return(r3.ReadLines(srcFilename.ReadStream(), lineFormat)); } if (fileFormat is ILineTreeTextReader r2) { return(r2.ReadLineTree(srcFilename.ReadText(), lineFormat).ToLines()); } if (fileFormat is ILineTreeStreamReader r4) { return(r4.ReadLineTree(srcFilename.ReadStream(), lineFormat).ToLines()); } if (fileFormat is ILineStringTextReader r5) { return(r5.ReadStringLines(srcFilename.ReadText(), lineFormat).ToLines(lineFormat)); } if (fileFormat is ILineStringStreamReader r6) { return(r6.ReadStringLines(srcFilename.ReadStream(), lineFormat).ToLines(lineFormat)); } } catch (FileNotFoundException) when(!throwIfNotFound) { return(no_keylines); } throw new FileLoadException($"Cannot read localization with {fileFormat.GetType().FullName}"); }
/// <summary> /// Read lines from text. /// </summary> /// <param name="fileFormat"></param> /// <param name="srcText"></param> /// <param name="lineFormat"></param> /// <returns>lines</returns> public static IEnumerable <ILine> ReadString(this ILineFileFormat fileFormat, string srcText, ILineFormat lineFormat = default) => ReadLines(fileFormat, new StringReader(srcText), lineFormat);
/// <summary> /// Create a reader that opens embedded <paramref name="resourceName"/> from <paramref name="assembly"/> on <see cref="IEnumerable.GetEnumerator"/>. /// </summary> /// <param name="fileFormat"></param> /// <param name="assembly"></param> /// <param name="resourceName"></param> /// <param name="lineFormat"></param> /// <param name="throwIfNotFound">if file is not found and value is true, <see cref="FileNotFoundException"/> is thrown, otherwise zero elements are returned</param> /// <returns>lines</returns> public static LineEmbeddedStringLinesSource EmbeddedReaderAsStringLines(this ILineFileFormat fileFormat, Assembly assembly, string resourceName, ILineFormat lineFormat = default, bool throwIfNotFound = true) => new LineEmbeddedStringLinesSource(fileFormat, assembly, resourceName, lineFormat, throwIfNotFound);
/// <summary> /// Create a reader that opens <paramref name="filepath"/> from <paramref name="fileProvider"/> on <see cref="IEnumerable.GetEnumerator"/>. /// </summary> /// <param name="fileFormat"></param> /// <param name="fileProvider"></param> /// <param name="filepath"></param> /// <param name="lineFormat">(optional) possibly needed for string and line conversions. Used also for choosing whether to instantiate parameter into hint or key</param> /// <param name="throwIfNotFound">if file is not found and value is true, <see cref="FileNotFoundException"/> is thrown, otherwise zero elements are returned</param> /// <returns>lines</returns> public static StringLineFileProviderSource FileProviderReaderAsStringLines(this ILineFileFormat fileFormat, IFileProvider fileProvider, string filepath, ILineFormat lineFormat = default, bool throwIfNotFound = true) => new StringLineFileProviderSource(fileFormat, fileProvider, filepath, lineFormat, throwIfNotFound);
/// <summary> /// Create a reader that opens <paramref name="filename"/> on <see cref="IEnumerable.GetEnumerator"/>. /// </summary> /// <param name="fileFormat"></param> /// <param name="filename"></param> /// <param name="lineFormat">(optional) possibly needed for string and line conversions. Used also for choosing whether to instantiate parameter into hint or key</param> /// <param name="throwIfNotFound">if file is not found and value is true, <see cref="FileNotFoundException"/> is thrown, otherwise zero elements are returned</param> /// <returns>lines</returns> public static StringLineFileSource FileReaderAsStringLines(this ILineFileFormat fileFormat, string filename, ILineFormat lineFormat = default, bool throwIfNotFound = true) => new StringLineFileSource(fileFormat, null, filename, lineFormat, throwIfNotFound);
/// <summary> /// Add localization file source. /// </summary> /// <param name="assetBuilder"></param> /// <param name="filename"></param> /// <param name="lineFormat">(optional)</param> /// <param name="fileFormat">(optional) overriding file format to use in case format cannot be infered from file extension</param> /// <returns></returns> /// <exception cref="ArgumentException">thrown if fileformat was not found</exception> public static IAssetBuilder AddLocalizationFile(this IAssetBuilder assetBuilder, string filename, ILineFormat lineFormat = default, ILineFileFormat fileFormat = null) { if (fileFormat == null) { fileFormat = LineReaderMap.Default.GetFormatByFilename(filename); } assetBuilder.AddSource(fileFormat.FileAssetSource(filename, lineFormat)); return(assetBuilder); }
/// <summary> /// Create localization file source that reads as string lines. /// </summary> /// <param name="fileFormat"></param> /// <param name="path">(optional) root folder</param> /// <param name="filename">non-rooted relative path, or rooted full path</param> /// <param name="lineFormat"></param> /// <param name="throwIfNotFound"></param> public StringLineFileSource(ILineFileFormat fileFormat, string path, string filename, ILineFormat lineFormat, bool throwIfNotFound) : base(fileFormat, path, filename, lineFormat, throwIfNotFound) { }
/// <summary> /// Create abstract file source. /// </summary> /// <param name="fileFormat"></param> /// <param name="path"></param> /// <param name="filePattern"></param> /// <param name="lineFormat"></param> public LineFilePatternSource(ILineFileFormat fileFormat, string path, ILinePattern filePattern, ILineFormat lineFormat) : base(path, filePattern) { this.FileFormat = fileFormat ?? throw new ArgumentNullException(nameof(fileFormat)); this.LineFormat = lineFormat; }
/// <summary> /// Create (abstract) source to localization file in a file provider. /// </summary> /// <param name="fileFormat"></param> /// <param name="fileProvider"></param> /// <param name="filepath"></param> /// <param name="lineFormat"></param> /// <param name="throwIfNotFound"></param> public LineFileProviderSource(ILineFileFormat fileFormat, IFileProvider fileProvider, string filepath, ILineFormat lineFormat, bool throwIfNotFound) : base(fileProvider, filepath, throwIfNotFound) { this.FileFormat = fileFormat ?? throw new ArgumentNullException(nameof(fileFormat)); this.LineFormat = lineFormat; }
/// <summary> /// Create embedded localization reader that reads as string lines. /// </summary> /// <param name="fileFormat"></param> /// <param name="assembly"></param> /// <param name="resourceName"></param> /// <param name="lineFormat"></param> /// <param name="throwIfNotFound">if true, throws <see cref="FileNotFoundException"/></param> public LineEmbeddedStringLinesSource(ILineFileFormat fileFormat, Assembly assembly, string resourceName, ILineFormat lineFormat, bool throwIfNotFound) : base(fileFormat, assembly, resourceName, lineFormat, throwIfNotFound) { }
/// <summary> /// Create source to localization file in a <paramref name="fileProvider"/>. /// </summary> /// <param name="fileFormat"></param> /// <param name="fileProvider"></param> /// <param name="filepath"></param> /// <param name="lineFormat"></param> /// <param name="throwIfNotFound"></param> public StringLineFileProviderSource(ILineFileFormat fileFormat, IFileProvider fileProvider, string filepath, ILineFormat lineFormat, bool throwIfNotFound) : base(fileFormat, fileProvider, filepath, lineFormat, throwIfNotFound) { }
/// <summary> /// Read tree from text. /// </summary> /// <param name="fileFormat"></param> /// <param name="srcText"></param> /// <param name="lineFormat"></param> /// <returns>tree</returns> public static ILineTree ReadStringAsLineTree(this ILineFileFormat fileFormat, string srcText, ILineFormat lineFormat = default) => ReadLineTree(fileFormat, new StringReader(srcText), lineFormat);
/// <summary> /// Create abstract file source. /// </summary> /// <param name="fileFormat"></param> /// <param name="path"></param> /// <param name="filename"></param> /// <param name="lineFormat"></param> /// <param name="throwIfNotFound"></param> public LineFileSource(ILineFileFormat fileFormat, string path, string filename, ILineFormat lineFormat, bool throwIfNotFound) : base(path, filename, throwIfNotFound) { this.FileFormat = fileFormat ?? throw new ArgumentNullException(nameof(fileFormat)); this.LineFormat = lineFormat; }
/// <summary> /// Read key-values as strings from text. /// </summary> /// <param name="fileFormat"></param> /// <param name="srcText"></param> /// <param name="lineFormat"></param> /// <returns>lines</returns> public static IEnumerable <KeyValuePair <string, IString> > ReadStringAsStringLines(this ILineFileFormat fileFormat, string srcText, ILineFormat lineFormat = default) => ReadStringLines(fileFormat, new StringReader(srcText), lineFormat);
public static void Main(string[] args) { { #region Snippet_0a ILineFileFormat format = LineReaderMap.Default["ini"]; #endregion Snippet_0a } { #region Snippet_0b ILineFileFormat format = IniLinesReader.Default; #endregion Snippet_0b ILineFileFormat format2a = LineReaderMap.Default["json"]; ILineFileFormat format2b = JsonLinesReader.Default; ILineFileFormat format3a = LineReaderMap.Default["xml"]; ILineFileFormat format3b = XmlLinesReader.Default; ILineFileFormat format4a = LineReaderMap.Default["resx"]; ILineFileFormat format4b = ResxLinesReader.Default; ILineFileFormat format5a = LineReaderMap.Default["resources"]; ILineFileFormat format5b = ResourcesLineReader.Default; } { #region Snippet_1a IEnumerable <ILine> key_lines = LineReaderMap.Default.ReadLines( filename: "localization.ini", throwIfNotFound: true); #endregion Snippet_1a } { #region Snippet_1b IEnumerable <KeyValuePair <string, IString> > string_lines = LineReaderMap.Default.ReadStringLines( filename: "localization.ini", lineFormat: LineFormat.Parameters, throwIfNotFound: true); #endregion Snippet_1b } { #region Snippet_1c ILineTree tree = LineReaderMap.Default.ReadLineTree( filename: "localization.ini", throwIfNotFound: true); #endregion Snippet_1c } { #region Snippet_2a IEnumerable <ILine> key_lines_reader = LineReaderMap.Default.FileReader( filename: "localization.ini", throwIfNotFound: true); #endregion Snippet_2a } { #region Snippet_2b IEnumerable <KeyValuePair <string, IString> > string_lines_reader = LineReaderMap.Default.FileReaderAsStringLines( filename: "localization.ini", lineFormat: LineFormat.Parameters, throwIfNotFound: true); #endregion Snippet_2b var lines = string_lines_reader.ToArray(); } { #region Snippet_2c IEnumerable <ILineTree> tree_reader = LineReaderMap.Default.FileReaderAsLineTree( filename: "localization.ini", throwIfNotFound: true); #endregion Snippet_2c var lines = tree_reader.ToArray(); } { #region Snippet_3a Assembly asm = typeof(LocalizationReader_Examples).Assembly; IEnumerable <ILine> key_lines_reader = LineReaderMap.Default.EmbeddedReader( assembly: asm, resourceName: "docs.localization.ini", throwIfNotFound: true); #endregion Snippet_3a var lines = key_lines_reader.ToArray(); } { Assembly asm = typeof(LocalizationReader_Examples).Assembly; #region Snippet_3b IEnumerable <KeyValuePair <string, IString> > string_lines_reader = LineReaderMap.Default.EmbeddedReaderAsStringLines( assembly: asm, resourceName: "docs.localization.ini", lineFormat: LineFormat.Parameters, throwIfNotFound: true); #endregion Snippet_3b var lines = string_lines_reader.ToArray(); } { Assembly asm = typeof(LocalizationReader_Examples).Assembly; #region Snippet_3c IEnumerable <ILineTree> tree_reader = LineReaderMap.Default.EmbeddedReaderAsLineTree( assembly: asm, resourceName: "docs.localization.ini", throwIfNotFound: true); #endregion Snippet_3c var lines = tree_reader.ToArray(); } { #region Snippet_4a IFileProvider fileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory()); IEnumerable <ILine> key_lines_reader = LineReaderMap.Default.FileProviderReader( fileProvider: fileProvider, filepath: "localization.ini", throwIfNotFound: true); #endregion Snippet_4a var lines = key_lines_reader.ToArray(); } { #region Snippet_4b IFileProvider fileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory()); IEnumerable <KeyValuePair <string, IString> > string_lines_reader = LineReaderMap.Default.FileProviderReaderAsStringLines( fileProvider: fileProvider, filepath: "localization.ini", throwIfNotFound: true); #endregion Snippet_4b var lines = string_lines_reader.ToArray(); } { #region Snippet_4c IFileProvider fileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory()); IEnumerable <ILineTree> tree_reader = LineReaderMap.Default.FileProviderReaderAsLineTree( fileProvider: fileProvider, filepath: "localization.ini", throwIfNotFound: true); #endregion Snippet_4c var lines = tree_reader.ToArray(); } { #region Snippet_5a using (Stream s = new FileStream("localization.ini", FileMode.Open, FileAccess.Read)) { IEnumerable <ILine> key_lines = IniLinesReader.Default.ReadLines(s); } #endregion Snippet_5a } { #region Snippet_5b using (Stream s = new FileStream("localization.ini", FileMode.Open, FileAccess.Read)) { IEnumerable <KeyValuePair <string, IString> > string_lines = IniLinesReader.Default.ReadStringLines( stream: s, lineFormat: LineFormat.Parameters); } #endregion Snippet_5b } { #region Snippet_5c using (Stream s = new FileStream("localization.ini", FileMode.Open, FileAccess.Read)) { ILineTree tree = IniLinesReader.Default.ReadLineTree(s); } #endregion Snippet_5c } { #region Snippet_6a string text = "Culture:en:Type:MyController:Key:Hello = Hello World!\n"; using (TextReader tr = new StringReader(text)) { IEnumerable <ILine> key_lines = IniLinesReader.Default.ReadLines(tr); } #endregion Snippet_6a } { string text = "Culture:en:Type:MyController:Key:Hello = Hello World!\n"; #region Snippet_6b using (TextReader tr = new StringReader(text)) { IEnumerable <KeyValuePair <string, IString> > string_lines = IniLinesReader.Default.ReadStringLines( srcText: tr, lineFormat: LineFormat.Parameters); } #endregion Snippet_6b } { string text = "Culture:en:Type:MyController:Key:Hello = Hello World!\n"; #region Snippet_6c using (TextReader tr = new StringReader(text)) { ILineTree tree = IniLinesReader.Default.ReadLineTree(tr); } #endregion Snippet_6c } { #region Snippet_7a string text = "Culture:en:Type:MyController:Key:Hello = Hello World!\n"; IEnumerable <ILine> key_lines = IniLinesReader.Default.ReadString( srcText: text); #endregion Snippet_7a } { string text = "Culture:en:Type:MyController:Key:Hello = Hello World!\n"; #region Snippet_7b IEnumerable <KeyValuePair <string, IString> > string_lines = IniLinesReader.Default.ReadStringAsStringLines( srcText: text, lineFormat: LineFormat.Parameters); #endregion Snippet_7b } { string text = "Culture:en:Type:MyController:Key:Hello = Hello World!\n"; #region Snippet_7c ILineTree tree = IniLinesReader.Default.ReadStringAsLineTree( srcText: text); #endregion Snippet_7c } { #region Snippet_10a IAsset asset = IniLinesReader.Default.FileAsset( filename: "localization.ini", throwIfNotFound: true); #endregion Snippet_10a } { #region Snippet_10b Assembly asm = typeof(LocalizationReader_Examples).Assembly; IAsset asset = IniLinesReader.Default.EmbeddedAsset( assembly: asm, resourceName: "docs.localization.ini", throwIfNotFound: true); #endregion Snippet_10b } { #region Snippet_10c IFileProvider fileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory()); IAsset asset = IniLinesReader.Default.FileProviderAsset( fileProvider: fileProvider, filepath: "localization.ini", throwIfNotFound: true); #endregion Snippet_10c } { Assembly asm = typeof(LocalizationReader_Examples).Assembly; IFileProvider fileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory()); #region Snippet_10a2 IAsset asset = LineReaderMap.Default.FileAsset( filename: "localization.ini", throwIfNotFound: true); #endregion Snippet_10a2 #region Snippet_10b2 asset = LineReaderMap.Default.EmbeddedAsset( assembly: asm, resourceName: "docs.localization.ini", throwIfNotFound: true); #endregion Snippet_10b2 #region Snippet_10c2 asset = LineReaderMap.Default.FileProviderAsset( fileProvider: fileProvider, filepath: "localization.ini", throwIfNotFound: true); #endregion Snippet_10c2 } { #region Snippet_11a IAssetSource assetSource = IniLinesReader.Default.FileAssetSource( filename: "localization.ini", throwIfNotFound: true); IAssetBuilder assetBuilder = new AssetBuilder().AddSource(assetSource); IAsset asset = assetBuilder.Build(); #endregion Snippet_11a } { #region Snippet_11b Assembly asm = typeof(LocalizationReader_Examples).Assembly; IAssetSource assetSource = IniLinesReader.Default.EmbeddedAssetSource( assembly: asm, resourceName: "docs.localization.ini", throwIfNotFound: true); #endregion Snippet_11b } { #region Snippet_11c IFileProvider fileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory()); IAssetSource assetSource = IniLinesReader.Default.FileProviderAssetSource( fileProvider: fileProvider, filepath: "localization.ini", throwIfNotFound: true); #endregion Snippet_11c } { #region Snippet_11a2 IAssetSource assetSource = LineReaderMap.Default.FileAssetSource( filename: "localization.ini", throwIfNotFound: true); #endregion Snippet_11a2 } { Assembly asm = typeof(LocalizationReader_Examples).Assembly; #region Snippet_11b2 IAssetSource assetSource = LineReaderMap.Default.EmbeddedAssetSource( assembly: asm, resourceName: "docs.localization.ini", throwIfNotFound: true); #endregion Snippet_11b2 } { IFileProvider fileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory()); #region Snippet_11c2 IAssetSource assetSource = LineReaderMap.Default.FileProviderAssetSource( fileProvider: fileProvider, filepath: "localization.ini", throwIfNotFound: true); #endregion Snippet_11c2 } { #region Snippet_7 #endregion Snippet_7 } { #region Snippet_8 #endregion Snippet_8 } { #region Snippet_9 #endregion Snippet_9 } { #region Snippet_12 #endregion Snippet_12 } { #region Snippet_13 #endregion Snippet_13 } { #region Snippet_14 #endregion Snippet_14 } { #region Snippet_15 #endregion Snippet_15 } { #region Snippet_16 #endregion Snippet_16 } { #region Snippet_17 #endregion Snippet_17 } { #region Snippet_18 #endregion Snippet_18 } { #region Snippet_30a // Create writer ILineReader format = new ExtFileFormatReader(); // Clone formats LineFileFormatMap formats = LineReaderMap.Default.Clone(); // Add to clone formats.Add(format); // Or if in deploying application project, format can be added to the global singleton (LineReaderMap.Default as IDictionary <string, ILineFileFormat>).Add(format); #endregion Snippet_30a } }
/// <summary> /// /// </summary> /// <param name="fileFormat"></param> /// <param name="streamSource"></param> /// <param name="lineFormat"></param> public StreamProviderAssetSource(ILineFileFormat fileFormat, Func <Stream> streamSource, ILineFormat lineFormat) { this.FileFormat = fileFormat ?? throw new ArgumentNullException(nameof(fileFormat)); this.streamSource = streamSource ?? throw new ArgumentNullException(nameof(streamSource)); this.NamePolicy = lineFormat; }
/// <summary> /// Read localization strings from <see cref="Stream"/> into most suitable asset implementation. /// /// File cannot be reloaded. /// </summary> /// <param name="fileFormat"></param> /// <param name="streamSource"></param> /// <param name="lineFormat">(optional) possibly needed for string and line conversions. Used also for choosing whether to instantiate parameter into hint or key</param> /// <returns>localization asset</returns> public static IAssetSource StreamAssetSource(this ILineFileFormat fileFormat, Func <Stream> streamSource, ILineFormat lineFormat = default) => new StreamProviderAssetSource(fileFormat, streamSource, lineFormat);
/// <summary> /// Add localization file source. /// </summary> /// <param name="assetBuilder"></param> /// <param name="resourceName"></param> /// <param name="lineFormat">(optional)</param> /// <param name="fileFormat">(optional) overriding file format to use in case format cannot be infered from file extension</param> /// <returns></returns> /// <exception cref="ArgumentException">thrown if fileformat was not found</exception> public static IAssetBuilder AddEmbeddedLocalizationFile(this IAssetBuilder assetBuilder, Assembly assembly, string resourceName, ILineFormat lineFormat = default, ILineFileFormat fileFormat = null) { if (fileFormat == null) { fileFormat = LineReaderMap.Default.GetFormatByFilename(resourceName); } assetBuilder.AddSource(fileFormat.EmbeddedAssetSource(assembly, resourceName, lineFormat)); return(assetBuilder); }