Implementation of multi-language architecture for platform projects and language architecture for platform projects
Why is the project platform-oriented? 1. Easy to maintain
Encapsulation can greatly reduce coupling, and only one place needs to be modified when a change occurs. There are differences between platform-based projects and encapsulation. A platform-based project sets a standard for all components and events of the project. These components are highly encapsulated based on standards to implement the configuration interface, greatly reduce the coding amount by configuring.
When the overall change is required, you only need to change this standard to make the maintenance point clearer.
Ii. scalability
1. First of all, I want to correct it. Many people say that something with too strong encapsulation can only be used for some qualitative projects and cannot meet complicated requirements. This is wrong. It can only be said that you are using a highly encapsulated framework of others, but you do not have your own framework or have no grasp of others' frameworks or that framework is too LOW. Excellent frameworks can meet the development needs, because they all have a lot of customization.
2. When the project requirements cannot be met, You can expand the existing components or create new components according to the standards.
3. Even if the component code is too spam, You Can refactor the component at any time to ensure that the call is standard.
Iii. Growth
With more and more projects, more and more standards and components will be improved.
Traditional multi-language implementation
Leave the database aside
I. resource files
The Cookie value is used to determine which resource file to call. I am using. NET, so I will vomit how inconvenient the. NET Resource file is.
1. Multiple keys need to be defined.
2. Why do we need to divide Chinese and English into two files and switch back and forth? What we actually need is this resource file.
3. Poor performance and heavy lifting
Ii. XML
1. You need to design your own code.
2. It cannot be used in static files and cannot be separated before and after
3. viewing is not convenient, and there is no intuitive EXCEL
3. Multiple pages
Using multiple pages to implement multiple languages has two disadvantages:
1. When the HTML structure of a page is changed, other pages also need to be changed.
2. There are no ready-made documents for translation by a specialized department. You have to organize your own documents or translate the documents by the programmer's side.
Platform-based implementation method 1. design resource files
For example, if we select EXCEL as a resource file, EXCEL is divided into multiple files based on different functions, which is different from the traditional method of resource file division.
Multiple Sheet are defined based on the page, and several columns are defined in several languages, instead of files defined in several languages. The traditional approach is qualitative change.
If you find a younger brother to sort out CN, you don't need the Development Department to be physically competent. Maybe someone will ask why there is no key, right! We do not need to use the 'key' feature. The following describes how to implement it.
Ii. How to parse EXCEL
. NET can use Aspose. cell or NOPI components to convert an EXCEL file into an object. This object can be stored in the Cache to reduce IO operations.
The storage structure is List <T>
Public class T {
Directory
Module
Page
Language
Language B
Language C...
}
This structure can easily filter out the language required for the current page
Iii. Implementation 1: Implement Global replacement in the template parsing Engine
Take. net mvc for example:
Use RazorEngine. dll to convert CSHTML to HTML
Public ActionResult Index () {string templateObj = GetViewByFilePath ("~ /Home/index. cshtml "); var model = new {id = xxx}; string html = Razor. parse (templateObj, model); // filter the language List on the current page based on the List converted into an Excel file <T>, this html is replaced by traversal. // the code is not encapsulated. I just want to give you a better understanding of foreach (var it in List <T>) {html = html. replace (it. default language, it. current language);} return View (new HtmlString (html ),"~ /Home/Shared/Language. cshtml ");}
// The Language. CSHTML file is used at the end of all pages
@model HtmlString@Model
The above replacement code is just the simplest example. I will discuss the disadvantages below in details:
Even if cache is used, the performance may be slightly affected, at least not worse than the resource file.
IV. Implementation 2: generate static files
You only need to develop all pages in the default language, and then generate other pages using tools.
Index_en.cshtml is a page generated by a tool.
Rewrite View engine automatically calls the corresponding View File Based on cookies
{// <Summary> /// custom MVC view engine /// </summary> public sealed class BestViewEngine: RazorViewEngine {public BestViewEngine () {}/// <summary> /// rewrite FindView /// </summary> /// <param name = "controllerContext"> </param> /// <param name = "viewName"> </param> // <param name = "masterName"> </param> // <param name = "useCache"> </param> /// <returns> </returns> public override ViewEngineResult FindView (ControllerContext controllerContext, string viewName, string masterName, bool useCache) {var key = "languageKey"; // cookie key var cm = CookiesManager <string>. getInstance (); if (cm. containsKey (key) // verify that the multi-language COOKIE has a value {var cacheVal = cm [key]; // obtain the multi-language suffix (for example, en) if (cacheVal. isValuable () viewName + = "_ {0 }". toFormat (cm [key]); // Add the suffix of the original view name. For example, after adding the suffix of index, it is index_en} return base. findView (controllerContext, viewName, masterName, useCache); // call the FindView method of the BASE after parameter processing }}}
The logic of the tool is very simple to read the file index. cshtml HTML =, get the language List <T >=> filter language List <T >=> traversal to replace the New HTML into Index_en.cshtml
Disadvantages
The tool is used, and there is always something missing.
V. Implementation 3: Implementation of our Platform
1. All menus on the page are generated through platform Configuration
Navigation map generated through background Configuration
2. Integrate the multilingual tool into the platform configuration interface, and click Generate to automatically create the corresponding VIEW File
Generated directory
3. Because the database has a language table and a menu table, Sheet in Excel is easier to filter. You only need to add a menu ID in the column, and the algorithm is more streamlined, the EXCEL structure also supports more styles.
6. Tips for replacement
1. JavaScript code Chinese Extraction
JS Use Page Definition
2. According to the organized Sheet, the JS part and the text in the page part are all sorted in, and the current menu ID is also added to facilitate filtering.
3. Take the first row and the second row in EXCEL for example. If you simply replace it, there will be problems.
The "Required number" in the page will be changed to the "Required number]
(1) We can solve this problem by replacing long ones with short ones.
(2) Replace the current page
(3) According to the above two replacement methods, we can ensure that the replacement of the 99% page is positive, in special cases, you can [^ move] Add a priority by adding a symbol. As long as the priority is replaced first with "=" starting with ", then press" length = "to display the current page and general page, in this way, we can ensure 99.9%, and so on (to put it bluntly, we are doing something above the priority ).
Don't you give me a thumbs up?