Multi-language architecture implementation of platform project

Source: Internet
Author: User

Why the project should be platform-based and easy to maintain

Encapsulation can greatly reduce coupling, and when changes occur, only one place needs to be modified. Platform-based projects and packaging are differentiated, platform projects are all components of the project, events, etc. to develop a standard, these components are high-level packaging standards, implementation of the configuration interface, through the configuration greatly reduce the amount of coding.

When it is necessary to change this standard, the maintenance point is more clear.

Second, expandable

1, first one point I want to correct, many people say that the packaging is too strong things can only do some qualitative projects, can not meet the complex needs. This view is wrong and can only be said that you are using someone else's highly encapsulated framework, without a frame of your own or a framework that is too low for someone else to master. Good frameworks are available to meet development needs, because they all have a lot of customization in place.

2, when the project needs are not satisfied, you can follow the standard to extend the existing components or create new components.

3, even if the component code is too garbage does not matter, as long as the call is to ensure that the specification, the component can be reconstructed at any time.

Third, the growth of

As projects become more and more, standards become more sophisticated and components become more and more

Multi-language traditional implementation mode

Throw away the Database section

I. Resource documents

Using the value of the cookie to determine which resource file to invoke, I'm using. NET and I'm going to spit it out. NET resource file is so inconvenient

1, need to define the N-more key.

2, why to divide the Chinese and English into two files, switching back and forth how much trouble, and actually we need to be such a resource file.

3, poor performance cumbersome

Second, XML

1. Need self-designed code

2, can not be used in the static file, do not be separated before and after

3, not easy to view, no Excel intuitive

Three or more pages

There are two drawbacks to using multiple pages to implement multiple languages:

1, when a page HTML structure found changes in the other pages also need to change

2, there is no ready-made documents to the specialized department to translate, to self-organized or translators in the programmer's side

A platform-based approach to implementation design resource file

If we choose Excel as a resource file, Excel is divided into multiple files according to the function, and the traditional resource file partitioning method is different.

Multiple sheet are defined according to the page, and several languages define several columns, rather than having several words defining how many files and traditional ways are qualitative.

Just find a little brother to the CN, we do not need to develop the Department of any physical strength, perhaps someone asked why no key, right! We just don't need to use key as a liability, and here's how to do it.

Second, how to parse Excel

. NET can use components such as Aspose.cell or Nopi to convert Excel to objects, you can save this object into the cache to reduce IO operations

The stored structure is list<t>

public class t{

Directory

Module

Page

Language A

Language b

Language C ...

}

This structure makes it easy to filter out the language needed for the current page

Third, achieve 1: Template resolution engine inside the hands and feet to achieve global replacement

Take. NET mvc to give examples:

cshtml to HTML using RazorEngine.dll self implementation

Public ActionResult Index () {String templateobj = Getviewbyfilepath ("~/home/index.cshtml");  var model=new {id = xxx};string html = razor.parse (templateobj, model); According to the list<t> of Excel, filter out the language of the current page list<t> the traversal and substitution processing//code for this HTML is not encapsulated, I just want to make it easier for everyone to see. foreach (var it in List <T>) {  html=html. Replace (it. Default language, it. Current language);} Return View ((new htmlstring (HTML), "~/home/shared/language.cshtml");}

  

All pages end with the language.cshtml file

@model Htmlstring@model

The replacement code above is just the simplest example of a more detailed place where I will discuss the disadvantages below:

Even if the cache is used, there is a bit of performance impact, at least not worse than the resource file.

Iv. Implementation 2: Generate static file mode

You only need to develop all the pages in the default language and then use the tools to generate additional pages

Index_en.cshtml is a tool-generated page

Overriding the view engine automatically invokes the corresponding view file based on a cookie

{    /// <summary>    ///Customizing the MVC view Engine/// </summary>     Public Sealed classBestviewengine:razorviewengine { PublicBestviewengine () {}/// <summary>        ///rewrite Findview/// </summary>        /// <param name= "ControllerContext" ></param>        /// <param name= "ViewName" ></param>        /// <param name= "Mastername" ></param>        /// <param name= "UseCache" ></param>        /// <returns></returns>         Public OverrideViewengineresult Findview (ControllerContext controllercontext,stringViewName,stringMastername,BOOLUseCache) {            varKey ="Languagekey";//COOKIE KEY            varCM = cookiesmanager<string>.            GetInstance (); if(CM. ContainsKey (Key))//verifying that multilingual cookies have values            {                varCacheval = Cm[key];//Get multiple language suffixes (e.g. en)                if(Cacheval.isvaluable ()) ViewName+="_{0}". Toformat (Cm[key]);//Add the original view name suffix such as index add suffix is index_en            }            return Base. Findview (ControllerContext, ViewName, Mastername, UseCache);//Call Base's Findview method after parameter processing        }     }}

The logic of the tool is simple to read the file index.cshtml html = "Get the language list<t>=> filter language list<t>=> traversal replaced by a new HTML write index_en.cshtml

Disadvantages

Using a tool, the total feeling is a little bit less.

V. Implementation 3: How our platform is implemented

Cond...

Six, the replacement of small tricks

Cond...

Perhaps a friend will ask, although the module and the page is clear, greatly reducing the replacement error rate, but there is no guarantee that the absence of key replacement is completely correct. Since 23 points have been written to 2, I will continue to update this post tomorrow evening. Don't you give me a good?

Multi-language architecture implementation of platform project

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.