A Preliminary Study on the multi-language and a preliminary discussion on the language
In the past few days, there have been a lot of free time. We have studied the many languages of Abp, which are interface-based dependency injection. Many ideas and methods are good, and we need to deeply understand and apply them, transform into your own ideas and apply them to practice.
This article introduces the open-source Demo ModuleZeroSampleProject. multiple languages are divided into two parts: backend CS code and front-end JS.
Resource file definition is divided into Web terminal and embedded DLL Layer
For Dll-layer-based resource files, you must set the file attribute to embedded. Otherwise, the resource files cannot be found after the release.
Web-based Initialization Method
//Add/remove localization sources here Configuration.Localization.Sources.Add( new XmlLocalizationSource( ModuleZeroSampleProjectConsts.LocalizationSourceName, HttpContext.Current.Server.MapPath("~/Localization/ModuleZeroSampleProject") ) );
The initialization method of the embedded Dll is the namespace, and Localization. Sources. AbpXmlSource is the file path.
Configuration.Localization.Sources.Add( new DictionaryBasedLocalizationSource( AbpConsts.LocalizationSourceName, new XmlEmbeddedFileLocalizationDictionaryProvider( Assembly.GetExecutingAssembly(), "Abp.Localization.Sources.AbpXmlSource" )));
Backend CS is divided into three parts, corresponding to Controller, Application, and Core
The first step of multi-language usage is initialization. Only the resource file name can be properly parsed and used,
Cotroller initialization method, create Cotroller base class ModuleZeroSampleProjectControllerBase, add the resource path in the constructor
Public abstract class ModuleZeroSampleProjectControllerBase: AbpController {protected ModuleZeroSampleProjectControllerBase () {LocalizationSourceName = ModuleZeroSampleProjectConsts. LocalizationSourceName ;}}View Code
Application, Core initialization method, create Service base class, all methods inherit this base class, resource file attributes must be set embedded
Public class MarketApplicationServiceBase: ApplicationService {protected MarketApplicationServiceBase () {LocalizationSourceName = MarketCoreConsts. LocalizationSourceName ;}}View Code
The front-end JS is partially initialized. If it is empty, we need to reference the relevant methods of Resource Initialization defined in the abp. js.
Abp. localization = abp. localization | {}; abp. localization. localize = function (key, sourceName) {sourceName = sourceName | abp. localization. defaultSourceName; var source = abp. localization. values [sourceName]; if (! Source) {abp. log. warn ('could not find localization source: '+ sourceName); return key;} var value = source [key]; if (value = undefined) {return key ;} var copiedArguments = Array. prototype. slice. call (arguments, 0); copiedArguments. splice (1, 1); copiedArguments [0] = value; return abp. utils. formatString. apply (this, copiedArguments);}; abp. localization. getSource = function (sourceName) {return function (key) {var copiedArguments = Array. prototype. slice. call (arguments, 0); copiedArguments. splice (1, 0, sourceName); return abp. localization. localize. apply (this, copiedArguments) ;};}; abp. localization. isCurrentCulture = function (name) {return abp. localization. currentCulture & abp. localization. currentCulture. name & abp. localization. currentCulture. name. indexOf (name) = 0;}; abp. localization. defaultSourceName = undefined; abp. localization. abpWeb = abp. localization. getSource ('abweb ');View Code
How can I read resource files?
<Script> $ (function () {var appLocalizationSource = abp. localization. getSource ('market '); // xml file name function localize () {return appLocalizationSource. apply (this, arguments) ;}; alert (localize ('Role _ isdefault') // fields defined in xml}) </script>
All the resource files loaded on the front end are dynamically generated by JS. <script src = "~ /AbpScripts/GetScripts? V = @ (Abp. Timing. Clock. Now. Ticks) "type =" text/javascript "> </script>
Referencing the above Js file will generate the following content:
You can also use the following method to display multiple languages on the Views interface. The L method is located in the following namespace: Abp. Web. Mvc. Views. AbpWebViewPage