using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using JetBrains.ProjectModel; using JetBrains.Util; namespace AWS.Localization { /// /// Simple loader for Java .properties files with localized strings. /// Currently, this class is used to get string constants that persists in Java code in MessagesBundle.properties file. /// We would like to use same localized strings to share values between IDEA and R#. /// Note: the original .properties file is added to AWS.Localization project as a static resource. /// [SolutionComponent] public class JavaPropertiesLoader { private readonly IDictionary myLocalizedStrings = new Dictionary(); private readonly object myLock = new object(); /// /// Get value by key from "MessagesBundle.properties" Java file. /// /// When key is not found. /// Key to search for /// Value from the "MessagesBundle.properties" file associated with a provided key. public string GetLocalizedString(string key) { lock (myLock) { if (myLocalizedStrings.IsNullOrEmpty()) { using (var stream = Assembly.GetExecutingAssembly() .GetManifestResourceStream("AWS.Localization.Resources.MessagesBundle.properties")) { Load(stream); } } return myLocalizedStrings[key]; } } private void Load(Stream stream) { if (stream.Length == 0) return; if (!stream.CanRead) throw new FileLoadException("Unable to read .properties file"); using (var reader = new StreamReader(stream)) { string line; while ((line = reader.ReadLine()?.Trim()) != null) { if (line.Length == 0 || line.StartsWith("#") || !line.Contains("=")) continue; // Consider all "="'s after the first match as the part of a value string. var keyValuePair = line.Split('='); myLocalizedStrings[keyValuePair[0]] = string.Join("", keyValuePair.Skip(1)); } } } } }