Пример #1
0
        public virtual async Task <string> ExportResourcesToXmlAsync(TblLanguages language)
        {
            var sb           = new StringBuilder();
            var stringWriter = new StringWriter(sb);
            var xmlWriter    = new XmlTextWriter(stringWriter);

            xmlWriter.WriteStartDocument();
            xmlWriter.WriteStartElement("Language");
            xmlWriter.WriteAttributeString("Name", language.LanguageName);
            xmlWriter.WriteAttributeString("ISO", language.IsoCode);

            var resources = (await GetAsEnumerableAsync()).Where(p => p.LanguageId == language.Id);

            foreach (var resource in resources)
            {
                xmlWriter.WriteStartElement("LocaleResource");
                xmlWriter.WriteAttributeString("Name", resource.ResourceName);
                xmlWriter.WriteElementString("Value", null, resource.ResourceValue);
                xmlWriter.WriteEndElement();
            }

            xmlWriter.WriteEndElement();
            xmlWriter.WriteEndDocument();
            xmlWriter.Close();
            return(stringWriter.ToString().PrettyXml());
        }
Пример #2
0
        public virtual async Task UpdateAsync(TblLanguages record)
        {
            if (await _dbContext.Languages
                .DeferredAny(p => p.IsoCode.Trim() == record.IsoCode.Trim() && p.Id != record.Id)
                .FromCacheAsync(CacheTags.Language))
            {
                throw new Exception($"The \"{record.IsoCode}\" ISO code already exist.");
            }

            var oldRecord = await FindByIdAsync(record.Id);

            if (oldRecord != null)
            {
                if (oldRecord.IsDefault && !record.Published)
                {
                    throw new Exception("System default language cannot be at unpublished status.");
                }

                if (record.Icon == null || record.Icon.Length == 0)
                {
                    record.Icon = oldRecord.Icon;
                }
                _dbContext.Languages.AddOrUpdate(record);
                await _dbContext.SaveChangesAsync();

                QueryCacheManager.ExpireTag(CacheTags.Language);

                _eventPublisher.EntityUpdated(record, oldRecord);
            }
        }
        public virtual async Task <LanguageModel> PrepareLanguageModelAsync([CanBeNull] TblLanguages language)
        {
            var result = language == null ? new LanguageModel() : language.Adapt <LanguageModel>();

            result.CurrenciesList = await _currencyService.GetAsSelectListAsync();

            return(result);
        }
Пример #4
0
        public virtual async Task <int> AddAsync(TblLanguages record)
        {
            if (await _dbContext.Languages.DeferredAny(p => p.IsoCode.Trim() == record.IsoCode.Trim())
                .FromCacheAsync(CacheTags.Language))
            {
                throw new Exception($"The \"{record.IsoCode}\" ISO code already exist.");
            }

            _dbContext.Languages.Add(record);
            await _dbContext.SaveChangesAsync();

            QueryCacheManager.ExpireTag(CacheTags.Language);

            _eventPublisher.EntityInserted(record);

            return(record.Id);
        }
        public virtual void ImportResourcesFromXmlAsync(TblLanguages language, string xml, AppDbContext db)
        {
            var xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(xml);

            var nodes = xmlDoc.SelectNodes(@"//Language/LocaleResource");

            if (nodes == null)
            {
                return;
            }
            foreach (XmlNode node in nodes)
            {
                if (node.Attributes != null)
                {
                    string name      = node.Attributes["Name"]?.InnerText.Trim();
                    string value     = "";
                    var    valueNode = node.SelectSingleNode("Value");
                    if (valueNode != null)
                    {
                        value = valueNode.InnerText;
                    }

                    if (String.IsNullOrEmpty(name))
                    {
                        continue;
                    }


                    db.LocalizedStrings.Add(new TblLocalizedStrings()
                    {
                        LanguageId    = language.Id,
                        ResourceName  = name,
                        ResourceValue = value
                    });
                }
            }

            db.SaveChanges();
        }
Пример #6
0
        protected virtual TblLanguages GetLanguageFromUrl()
        {
            if (HttpContext?.Request?.Url == null)
            {
                return(null);
            }

            var          currentPath = HttpContext?.Request.Url.PathAndQuery.TrimStart('/').Split(new[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
            TblLanguages language    = null;

            if (currentPath.Length >= 1 && currentPath[0].Length == 2)
            {
                var languageIso = currentPath[0];
                language = _languagesService.FindByIso(languageIso.Trim().ToLower());
            }

            if (language != null && language.Published)
            {
                return(language);
            }

            return(null);
        }
Пример #7
0
 public DefaultLanguageChangeEvent(TblLanguages record)
 {
     Record = record;
 }
        public virtual void Initialize(AppDbContext db)
        {
            //Default Currency
            TblCurrencies usDollar = null, irRial = null;

            if (!db.Currencies.Any(p => p.IsMainCurrency))
            {
                usDollar = new TblCurrencies()
                {
                    IsoCode        = "USD",
                    DisplayOrder   = 0,
                    IsMainCurrency = true,
                    Published      = true,
                    CurrencyName   = "USD (US Dollar)",
                    ExchangeRate   = 0,
                    DisplayFormat  = "${0:N2}",
                    ShortName      = "USD"
                };

                irRial = new TblCurrencies()
                {
                    IsoCode        = "IRR",
                    DisplayOrder   = 1,
                    IsMainCurrency = false,
                    Published      = true,
                    CurrencyName   = "IRR (Iranian Rial)",
                    ExchangeRate   = 38000,
                    DisplayFormat  = "{0:#,###} ریال",
                    ShortName      = "Rial"
                };

                db.Currencies.AddOrUpdate(usDollar);
                db.Currencies.AddOrUpdate(irRial);

                db.SaveChanges();
            }
            else
            {
                usDollar = db.Currencies.FirstOrDefault(p => p.IsoCode.ToUpper().Trim() == "USD");
                irRial   = db.Currencies.FirstOrDefault(p => p.IsoCode.ToUpper().Trim() == "IRR");
            }

            //Default Language
            TblLanguages enLang, faLang;

            if (!db.Languages.Any(p => p.IsDefault))
            {
                using (var stream = new MemoryStream())
                {
                    Resources.English_16px.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                    enLang = new TblLanguages()
                    {
                        IsoCode           = "en",
                        IsRtl             = false,
                        DisplayOrder      = 0,
                        IsDefault         = true,
                        LanguageName      = "English",
                        Published         = true,
                        Icon              = stream.ToArray(),
                        DefaultCurrency   = usDollar,
                        DefaultCurrencyId = usDollar?.Id
                    };

                    stream.Seek(0, SeekOrigin.Begin);
                    Resources.Iran_16px.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                    faLang = new TblLanguages()
                    {
                        IsoCode           = "fa",
                        IsRtl             = true,
                        DisplayOrder      = 1,
                        IsDefault         = false,
                        LanguageName      = "فارسی",
                        Published         = true,
                        Icon              = stream.ToArray(),
                        DefaultCurrency   = irRial,
                        DefaultCurrencyId = irRial?.Id
                    };
                }

                db.Languages.AddOrUpdate(enLang);
                db.Languages.AddOrUpdate(faLang);
                db.SaveChanges();

                if (irRial != null)
                {
                    db.LocalizedProperty.AddOrUpdate(new TblLocalizedProperty()
                    {
                        EntityId       = irRial.Id,
                        LocaleKey      = "CurrencyName",
                        LocaleKeyGroup = "Tbl_Currencies",
                        LocaleValue    = "ریال ایران",
                        LanguageId     = faLang.Id
                    });
                    db.LocalizedProperty.AddOrUpdate(new TblLocalizedProperty()
                    {
                        EntityId       = irRial.Id,
                        LocaleKey      = "ShortName",
                        LocaleKeyGroup = "Tbl_Currencies",
                        LocaleValue    = "ریال",
                        LanguageId     = faLang.Id
                    });
                    db.SaveChanges();
                }

                if (usDollar != null)
                {
                    db.LocalizedProperty.AddOrUpdate(new TblLocalizedProperty()
                    {
                        EntityId       = usDollar.Id,
                        LocaleKey      = "CurrencyName",
                        LocaleKeyGroup = "Tbl_Currencies",
                        LocaleValue    = "دلار آمریکا",
                        LanguageId     = faLang.Id
                    });
                    db.LocalizedProperty.AddOrUpdate(new TblLocalizedProperty()
                    {
                        EntityId       = usDollar.Id,
                        LocaleKey      = "ShortName",
                        LocaleKeyGroup = "Tbl_Currencies",
                        LocaleValue    = "دلار",
                        LanguageId     = faLang.Id
                    });
                    db.SaveChanges();
                }
            }
            else
            {
                faLang = db.Languages.FirstOrDefault(p => p.IsoCode.Trim() == "fa");
            }

            //Countries List
            if (!db.Countries.Any())
            {
                foreach (var country in Countries)
                {
                    var dbCountry = new TblCountries()
                    {
                        CountryName = country.Key
                    };
                    db.Countries.AddOrUpdate(dbCountry);
                    db.SaveChanges();

                    if (faLang != null)
                    {
                        db.LocalizedProperty.AddOrUpdate(new TblLocalizedProperty()
                        {
                            EntityId       = dbCountry.Id,
                            LanguageId     = faLang.Id,
                            LocaleKeyGroup = "Tbl_Countries",
                            LocaleKey      = "CountryName",
                            LocaleValue    = country.Value
                        });
                    }
                }
            }

            db.SaveChanges();

            ImportLocalizedStrings(db);
        }
Пример #9
0
        protected virtual Document MapPost(TblPosts post, TblLanguages language, PostType?postType)
        {
            var title        = post.GetLocalized(p => p.Title, language.Id);
            var descriptions = post.Descriptions.Where(p => p.AddToSearchEngineIndexes).Aggregate("",
                                                                                                  (current, description) =>
                                                                                                  current + description.GetLocalized(p => p.HtmlDescription, language.Id)?.ConvertHtmlToText() +
                                                                                                  Environment.NewLine + Environment.NewLine);
            var tags = post.Tags.Aggregate("",
                                           (current, tag) => current + tag.GetLocalized(p => p.Tag, language.Id) + ", ");
            var categories = post.Categories.Aggregate("",
                                                       (current, cat) => current + (cat.Id + " , "));
            var keyWords = post.GetLocalized(p => p.MetaKeyWords, language.Id);

            var document = new Document();


            document.Add(new Field("ID", post.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            document.Add(new Field("PostType", postType?.ToString() ?? "", Field.Store.YES, Field.Index.NOT_ANALYZED));
            document.Add(new Field("LanguageId", language.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            document.Add(new Field("LanguageCode", language.IsoCode, Field.Store.YES, Field.Index.NOT_ANALYZED));
            document.Add(new Field("PublishDate", DateTools.DateToString(post.PublishDate, DateTools.Resolution.SECOND),
                                   Field.Store.YES, Field.Index.ANALYZED));
            document.Add(new Field("LastUpDate",
                                   DateTools.DateToString(post.LastUpDate ?? post.PublishDate, DateTools.Resolution.SECOND),
                                   Field.Store.YES, Field.Index.ANALYZED));
            var numberOfViewField = new NumericField("NumberOfVisit", Field.Store.YES, true);

            numberOfViewField.SetIntValue(post.NumberOfViews);
            document.Add(numberOfViewField);


            if (!string.IsNullOrEmpty(title))
            {
                var titleField = new Field("Title",
                                           title,
                                           Field.Store.YES,
                                           Field.Index.ANALYZED,
                                           Field.TermVector.WITH_POSITIONS_OFFSETS)
                {
                    Boost = 100
                };
                document.Add(titleField);
            }


            if (!string.IsNullOrWhiteSpace(descriptions.Replace(Environment.NewLine, "")))
            {
                var descriptionField = new Field("Description",
                                                 descriptions,
                                                 Field.Store.YES,
                                                 Field.Index.ANALYZED,
                                                 Field.TermVector.WITH_POSITIONS_OFFSETS)
                {
                    Boost = 50
                };
                document.Add(descriptionField);
            }


            if (!string.IsNullOrWhiteSpace(tags.Replace(", ", "")))
            {
                var tagField = new Field("Tags",
                                         tags,
                                         Field.Store.YES,
                                         Field.Index.ANALYZED,
                                         Field.TermVector.WITH_POSITIONS_OFFSETS)
                {
                    Boost = 50
                };
                document.Add(tagField);
            }


            var categoriesField = new Field("Categories",
                                            categories,
                                            Field.Store.YES,
                                            Field.Index.ANALYZED);

            document.Add(categoriesField);


            if (!string.IsNullOrEmpty(keyWords))
            {
                var keywordsField = new Field("Keywords",
                                              keyWords,
                                              Field.Store.YES,
                                              Field.Index.ANALYZED,
                                              Field.TermVector.WITH_POSITIONS_OFFSETS)
                {
                    Boost = 50
                };
                document.Add(keywordsField);
            }

            return(document);
        }