예제 #1
0
 private object HandleLocalization(object src, ITranslatorDisabler disabler, ITranslatedCaching cache, ITranslator translator, string culture, ResultExecutingContext context)
 {
     if (src == null)
     {
         return(null);
     }
     if (src.GetType().GetTypeInfo().BaseType == typeof(ValueType))
     {
         return(src);
     }
     if (src is IDictionary)
     {
         return(src);
     }
     if (src is IEnumerable)
     {
         var model = src is IList ? src : ((dynamic)src).ToList();
         for (var i = 0; i < model.Count; i++)
         {
             var tmp = model[i];
             HandleLocalizationForObject(ref tmp, disabler, cache, translator, culture, context);
             model[i] = tmp;
         }
         return(model);
     }
     else
     {
         return(HandleLocalizationForObject(ref src, disabler, cache, translator, culture, context));
     }
 }
예제 #2
0
 private object HandleLocalization(object src, ITranslatorDisabler disabler, ITranslatedCaching cache, ITranslator translator, string culture, ResultExecutingContext context, List <int> visited)
 {
     try
     {
         if (src == null)
         {
             return(null);
         }
         if (src.GetType().GetTypeInfo().BaseType == typeof(ValueType))
         {
             return(src);
         }
         if (src is IDictionary)
         {
             return(src);
         }
         if (src is string)
         {
             return(src);
         }
         if (src is IEnumerable)
         {
             var model = src is IList ? src : ((dynamic)src).ToList();
             if (!visited.Any(x => x == src.GetHashCode()))
             {
                 for (var i = 0; i < model.Count; i++)
                 {
                     var tmp = model[i];
                     var hc  = (int)model[i].GetHashCode();
                     if (!visited.Any(x => x == hc))
                     {
                         HandleLocalizationForObject(ref tmp, disabler, cache, translator, culture, context, visited);
                         model[i] = tmp;
                     }
                 }
                 return(model);
             }
             else
             {
                 return(model);
             }
         }
         else
         {
             if (!visited.Any(x => x == src.GetHashCode()))
             {
                 visited.Add(src.GetHashCode());
                 return(HandleLocalizationForObject(ref src, disabler, cache, translator, culture, context, visited));
             }
             else
             {
                 return(src);
             }
         }
     }
     catch
     {
         return(src);
     }
 }
예제 #3
0
        private object HandleLocalizationForObject(ref object src, ITranslatorDisabler disabler, ITranslatedCaching cache, ITranslator translator, string culture, ResultExecutingContext context)
        {
            if (src == null)
            {
                return(null);
            }
            if (src.GetType().GetTypeInfo().BaseType == typeof(ValueType))
            {
                return(src);
            }
            var type       = src.GetType().GetTypeInfo();
            var properties = type.DeclaredProperties;

            foreach (var y in properties)
            {
                if (y.PropertyType.GetTypeInfo().BaseType == typeof(ValueType))
                {
                    continue;
                }
                else if (y.PropertyType == typeof(string))
                {
                    if (y.GetCustomAttribute <LocalizedAttribute>() != null)
                    {
                        var jsonStr = y.GetValue(src);
                        var json    = JsonConvert.DeserializeObject <Dictionary <string, string> >(jsonStr.ToString());
                        if (json.ContainsKey(culture))
                        {
                            y.SetValue(src, json[culture]);
                        }
                        else
                        {
                            var key = json.Keys.FirstOrDefault();
                            if (key == null)
                            {
                                y.SetValue(src, "");
                            }
                            else
                            {
                                if (disabler.IsDisabled())
                                {
                                    y.SetValue(src, json[key]);
                                }
                                (context.Result as ViewResult).ViewData["__IsTranslated"] = true;
                                var cachedString = cache.Get(json[key], culture);
                                if (cachedString == null)
                                {
                                    var translateTask = translator.TranslateAsync(key, culture, json[key]);
                                    translateTask.Wait();
                                    cache.Set(json[key], culture, translateTask.Result);
                                    y.SetValue(src, translateTask.Result);
                                }
                                else
                                {
                                    y.SetValue(src, cachedString);
                                }
                            }
                        }
                    }
                }
                else
                {
                    HandleLocalization(y.GetValue(src), disabler, cache, translator, culture, context);
                }
            }
            return(src);
        }
 public BaiduTranslator(ITranslatorDisabler disabler)
 {
     this.disabler = disabler;
 }
 public BaiduTranslator(ITranslatorDisabler disabler)
 {
     this.disabler = disabler;
 }