Beispiel #1
0
        /// <summary>
        /// Loads the i18n files for the specified culture and merges it all into a single <see cref="I18nDocument"/>.
        /// The files must be under the <c>i18n/lang-region</c> folder, such as <c>i18n/en-US</c> or <c>i18n/es-ES</c> and have <c>.json</c> extensions.
        /// </summary>
        /// <param name="newCulture">The culture to load.</param>
        /// <returns>An <see cref="I18nDocument"/> containing the merged contents of all valid json files.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="newCulture"/> is null.</exception>
        public static async Task <I18nDocument> LoadCultureAsync(CultureInfo newCulture)
        {
            if (newCulture is null)
            {
                throw new ArgumentNullException(nameof(newCulture));
            }

            I18nDocument document = new I18nDocument();

            string i18nLocation = Path.GetFullPath(Path.Combine(typeof(Program).Assembly.Location, "i18n", newCulture.Name));

            if (Directory.Exists(i18nLocation))
            {
                foreach (string f in Directory.EnumerateFiles(i18nLocation, "*.json", new EnumerationOptions()
                {
                    RecurseSubdirectories = true
                }))
                {
                    try
                    {
                        using FileStream fs = File.OpenRead(f);
                        document.Merge(await JsonSerializer.DeserializeAsync <I18nDocument>(fs).ConfigureAwait(false));
                    }
                    catch (JsonException) { }
                }
            }

            return(document);
        }
Beispiel #2
0
        /// <summary>
        /// Attempts to retrieve the specified i18n replacement string.
        /// </summary>
        /// <param name="category">The category to select from.</param>
        /// <param name="key">The string key to select.</param>
        /// <param name="document">The <see cref="I18nDocument"/> to use.</param>
        /// <param name="defVal">The default value to return if either the <paramref name="category"/> or <paramref name="key"/> do not exist; <c>null</c> if not provided</param>
        /// <returns>The i18n replacement string, if present; <paramref name="defVal"/> otherwise.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="document"/> is null.</exception>
        public static string Get(string category, string key, I18nDocument document, string defVal = null)
        {
            if (document is null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            if (document.I18nData.TryGetValue(category, out Dictionary <string, string> data))
            {
                if (data.TryGetValue(key, out string value))
                {
                    return(value);
                }
            }

            return(defVal);
        }
Beispiel #3
0
 /// <summary>
 /// Attempts to retrieve the specified i18n replacement string, and then formats it.
 /// </summary>
 /// <param name="category">The category to select from.</param>
 /// <param name="key">The string key to select.</param>
 /// <param name="document">The <see cref="I18nDocument"/> to use.</param>
 /// <param name="source">A type that provides the replacement values. Can be <c>Dictionary&lt;string, string&gt;</c>, <c>List&lt;string&gt;</c>, or an anonymous type (eg. <c>new { MyValue = "hello!" }</c>).</param>
 /// <param name="culture">The culture to use.</param>
 /// <param name="defVal">The default value to return if either the <paramref name="category"/> or <paramref name="key"/> do not exist; <c>null</c> if not provided</param>
 /// <returns>A copy of the i18n replacement string (or <paramref name="defVal"/> if it is not found) in which the format items have been replaced by the string representation of the corresponding objects in <paramref name="source"/>.</returns>
 /// <exception cref="ArgumentNullException"><paramref name="document"/> is null.</exception>
 public static string GetAndFormatWith(string category, string key, I18nDocument document, object source, CultureInfo culture, string defVal = null) => FormatWith(Get(category, key, document, defVal), culture, source);