首页 > 其他分享 >NopCommerce多语言的实现

NopCommerce多语言的实现

时间:2022-11-23 07:55:13浏览次数:35  
标签:语言 language 实现 resourceKey detectedLanguage NopCommerce null Id string

通过语言包xml文件导入导出语言字符串

数据库方面,以下数据表:

Language

LocaleStringResource

LocalizedProperty

接口IWorkContext真正实现类是Nop.Web.Framework.WebWorkContext,里面有一个获取语言的属性。

  1. public virtual Language WorkingLanguage
  2. {
  3. get
  4. {
  5. if (_cachedLanguage != null)
  6. return _cachedLanguage;
  7. Language detectedLanguage = null;
  8. if (_localizationSettings.SeoFriendlyUrlsForLanguagesEnabled)
  9. {
  10. //从URL中获取语言
  11. detectedLanguage = GetLanguageFromUrl();
  12. }
  13. if (detectedLanguage == null && _localizationSettings.AutomaticallyDetectLanguage)
  14. {
  15. //get language from browser settings
  16. //but we do it only once
  17. if (!this.CurrentCustomer.GetAttribute<bool>(SystemCustomerAttributeNames.LanguageAutomaticallyDetected,
  18. _genericAttributeService, _storeContext.CurrentStore.Id))
  19. {
  20. detectedLanguage = GetLanguageFromBrowserSettings();
  21. if (detectedLanguage != null)
  22. {
  23. _genericAttributeService.SaveAttribute(this.CurrentCustomer, SystemCustomerAttributeNames.LanguageAutomaticallyDetected,
  24. true, _storeContext.CurrentStore.Id);
  25. }
  26. }
  27. }
  28. if (detectedLanguage != null)
  29. {
  30. //the language is detected. now we need to save it
  31. if (this.CurrentCustomer.GetAttribute<int>(SystemCustomerAttributeNames.LanguageId,
  32. _genericAttributeService, _storeContext.CurrentStore.Id) != detectedLanguage.Id)
  33. {
  34. _genericAttributeService.SaveAttribute(this.CurrentCustomer, SystemCustomerAttributeNames.LanguageId,
  35. detectedLanguage.Id, _storeContext.CurrentStore.Id);
  36. }
  37. }
  38. var allLanguages = _languageService.GetAllLanguages(storeId: _storeContext.CurrentStore.Id);
  39. //find current customer language
  40. var languageId = this.CurrentCustomer.GetAttribute<int>(SystemCustomerAttributeNames.LanguageId,
  41. _genericAttributeService, _storeContext.CurrentStore.Id);
  42. var language = allLanguages.FirstOrDefault(x => x.Id == languageId);
  43. if (language == null)
  44. {
  45. //如果没有指定语言,默认返回系统中的第一种语言
  46. language = allLanguages.FirstOrDefault();
  47. }
  48. if (language == null)
  49. {
  50. //it not specified, then return the first found one
  51. language = _languageService.GetAllLanguages().FirstOrDefault();
  52. }
  53. //cache
  54. _cachedLanguage = language;
  55. return _cachedLanguage;
  56. }
  57. set
  58. {
  59. var languageId = value != null ? value.Id : 0;
  60. _genericAttributeService.SaveAttribute(this.CurrentCustomer,
  61. SystemCustomerAttributeNames.LanguageId,
  62. languageId, _storeContext.CurrentStore.Id);
  63. //reset cache
  64. _cachedLanguage = null;
  65. }
  66. }
  1. string result = string.Empty;
  2. if (resourceKey == null)
  3. resourceKey = string.Empty;
  4. resourceKey = resourceKey.Trim().ToLowerInvariant();
  5. if (_localizationSettings.LoadAllLocaleRecordsOnStartup)
  6. {
  7. //load all records (we know they are cached)
  8. var resources = GetAllResourceValues(languageId);
  9. if (resources.ContainsKey(resourceKey))
  10. {
  11. result = resources[resourceKey].Value;
  12. }
  13. }
  14. else
  15. {
  16. //gradual loading
  17. string key = string.Format(LOCALSTRINGRESOURCES_BY_RESOURCENAME_KEY, languageId, resourceKey);
  18. string lsr = _cacheManager.Get(key, () =>
  19. {
  20. var query = from l in _lsrRepository.Table
  21. where l.ResourceName == resourceKey
  22. && l.LanguageId == languageId
  23. select l.ResourceValue;
  24. return query.FirstOrDefault();
  25. });
  26. if (lsr != null)
  27. result = lsr;
  28. }
  29. if (String.IsNullOrEmpty(result))
  30. {
  31. if (logIfNotFound)
  32. _logger.Warning(string.Format("Resource string ({0}) is not found. Language ID = {1}", resourceKey, languageId));
  33. if (!String.IsNullOrEmpty(defaultValue))
  34. {
  35. result = defaultValue;
  36. }
  37. else
  38. {
  39. if (!returnEmptyIfNotFound)
  40. result = resourceKey;
  41. }
  42. }
  43. return result;
  44. }

 

  1. public virtual string GetResource(string resourceKey)
  2. {
  3. if (_workContext.WorkingLanguage != null)
  4. return GetResource(resourceKey, _workContext.WorkingLanguage.Id);
  5. return "";
  6. }

 

接下来我们来通过一个验证提示信息类BlogPostValidator来看看这个多语言是怎么实现的?

BlogPostValidator.cs:

  1. public class BlogPostValidator : AbstractValidator<BlogPostModel>
  2. {
  3. public BlogPostValidator(ILocalizationService localizationService)
  4. {
  5. RuleFor(x => x.Title)
  6. .NotEmpty()
  7. .WithMessage(localizationService.GetResource("Admin.ContentManagement.Blog.BlogPosts.Fields.Title.Required"));
  8. RuleFor(x => x.Body)
  9. .NotEmpty()
  10. .WithMessage(localizationService.GetResource("Admin.ContentManagement.Blog.BlogPosts.Fields.Body.Required"));
  11. }
  12. }

 

标签:语言,language,实现,resourceKey,detectedLanguage,NopCommerce,null,Id,string
From: https://www.cnblogs.com/5x19/p/16917102.html

相关文章

  • CSS实现标题和图片混合
    ResultCode<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="view......
  • 当resource bundle 的多语言文件里包含引号'时
    背景项目中使用Spring的ReloadableResourceBundleMessageSource这个类来实现多语言,有一次字符串里包含引号'时,解析时出了问题,一起来看一下吧例子resources下包含三个语......
  • golang算法—— 使用两个栈实现一个队列
    前言阅读本文,假定已经了解了基本数据结构概念。队列:先入先出。栈:先进后出。分析使用两个栈串联,可以实现先进先出。但是,得注意以下两点:队列在入列时,stack2必须为空,stac......
  • go语言入门
    go目录简介/bin:包含可执行文件,如:编译器,Go工具/doc:包含文档模版/lib:包含示例程序,代码工具,本地文档等/misc:包含与支持Go编辑器有关的配置文件以及cgo的示例/os_a......
  • 在WPF中利用XPS文档实现打印功能
    摘要WPF重新规划了打印模型,将所有编码围绕System.Windows.Controls.PrintDialog,使用PrintDialog类可显示Print对话框、设置打印机首选项等等,并且可将元素、文档以及低级的......
  • VUE3 全局共享数据方案之一 (简单快速实现类似vuex)
    自定义封装单列模式!globalstate由于vue3的响应式系统本身可以脱离组件而存在,因此可以充分利用这一点,轻松制造多个全局响应式数据,并且通过和vuex一样通过某个模块指定......
  • 随机打乱数组--java实现
    参考链接听说过java.utils.Random随机数是伪随机,但是Math库还没学,所以下面代码中还是用的Randompublicstaticint[]shuffle(int[]arr){Randomr=newRandom(......
  • Java 用Lambda实现一个通用的制造者工具
    在我们日常开发中,虽然是用了lombok在实体类中已经帮我们省了get、set方法,但是在公司的项目中,还是经常会出现new一个对象然后一个个的给它set值的情况,太丑了,如下图List<St......
  • C语言中调用可执行程序的方法。
    在C/C++程序中,经常需要调用其它的程序来先成某项任务,例如其它的C/C++程序、操作系统命令或Shell脚本,C/C++提供了exec函数族和system函数来实现这个功能。一、exce函数族exec......
  • 精华推荐 | 【深入浅出RocketMQ原理及实战】「底层原理挖掘系列」透彻剖析贯穿RocketM
    RocketMQ的发展历史RocketMQ是一个统一消息引擎、轻量级数据处理平台。RocketMQ是一款阿里巴巴开源的消息中间件。2016年11月28日,阿里巴巴向广西党性培训Apache软......