internal JObject Convert(
            ICollection <LocalizationResource> resources,
            string language,
            FallbackLanguagesCollection fallbackCollection,
            bool camelCase)
        {
            var result = new JObject();

            foreach (var resource in resources)
            {
                // we need to process key names and supported nested classes with "+" symbols in keys -> so we replace those with dots to have proper object nesting on client side
                var key = resource.ResourceKey.Replace("+", ".");
                if (!key.Contains("."))
                {
                    continue;
                }

                var segments = key.Split(new[] { "." }, StringSplitOptions.None)
                               .Select(k => camelCase ? CamelCase(k) : k)
                               .ToList();

                // let's try to look for translation explicitly in requested language
                // if there is no translation in given language -> worth to look in fallback culture *and* invariant (if configured to do so)
                var translation = resource.Translations.GetValueWithFallback(
                    language,
                    fallbackCollection.GetFallbackLanguages(language));

                // there is nothing at the other end - so we should not generate key at all
                if (translation == null)
                {
                    continue;
                }

                Aggregate(result,
                          segments,
                          (e, segment) =>
                {
                    if (e[segment] == null)
                    {
                        e[segment] = new JObject();
                    }

                    return(e[segment] as JObject);
                },
                          (o, s) => { o[s] = translation; });
            }

            return(result);
        }
        /// <summary>
        /// Gets the JSON object from given resource class.
        /// </summary>
        /// <param name="resourceClassName">Name of the resource class.</param>
        /// <param name="languageName">Name of the language.</param>
        /// <param name="fallbackCollection">List of fallback languages collection.</param>
        /// <param name="camelCase">if set to <c>true</c> JSON properties will be in camelCase; otherwise PascalCase is used.</param>
        /// <returns>JSON object that represents resource</returns>
        public JObject GetJson(
            string resourceClassName,
            string languageName,
            FallbackLanguagesCollection fallbackCollection,
            bool camelCase = false)
        {
            var resources         = _queryExecutor.Execute(new GetAllResources.Query());
            var filteredResources = resources
                                    .Where(r => r.ResourceKey.StartsWith(resourceClassName, StringComparison.InvariantCultureIgnoreCase))
                                    .ToList();

            return(Convert(
                       filteredResources,
                       languageName,
                       fallbackCollection,
                       camelCase));
        }
예제 #3
0
 public FallbackLanguagesTestTranslationHandler(FallbackLanguagesCollection fallbackCollection)
 {
     _fallbackCollection = fallbackCollection;
 }
 /// <summary>
 /// Gets the JSON object from given resource class.
 /// </summary>
 /// <param name="resourceClassName">Name of the resource class.</param>
 /// <param name="fallbackCollection">List of fallback languages collection.</param>
 /// <param name="camelCase">if set to <c>true</c> JSON properties will be in camelCase; otherwise PascalCase is used.</param>
 /// <returns>JSON object that represents resource</returns>
 public JObject GetJson(string resourceClassName, FallbackLanguagesCollection fallbackCollection, bool camelCase = false)
 {
     return(GetJson(resourceClassName, CultureInfo.CurrentUICulture.Name, fallbackCollection, camelCase));
 }
예제 #5
0
 /// <summary>
 /// Gets the JSON object from given resource class.
 /// </summary>
 /// <param name="resourceClassName">Name of the resource class.</param>
 /// <param name="fallbackCollection">List of fallback languages collection.</param>
 /// <param name="camelCase">if set to <c>true</c> JSON properties will be in camelCase; otherwise PascalCase is used.</param>
 /// <returns>JSON object that represents resource</returns>
 public JObject GetJson(string resourceClassName, FallbackLanguagesCollection fallbackCollection, bool camelCase = false)
 {
     return(GetJson(resourceClassName, _queryExecutor.Execute(new GetCurrentUICulture.Query()).Name, fallbackCollection, camelCase));
 }