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);
        }
Example #2
0
 public string Execute(GetTranslation.Query query)
 {
     return(_resources[query.Key].Translations.GetValueWithFallback(
                query.Language,
                _fallbackCollection.GetFallbackLanguages(query.Language)));
 }