⬆️ ⬇️

Transfer ASP.NET application to Mono. Russian language support

Recently I transferred a small ASP application with .NET to Mono. Faced with the almost complete lack of support for the Russian language at the default settings. We used a bunch of Debian lenny (netinst) + Apache2 + mono-apache-server2.



Problem solving:



First problem: Russian text is not displayed in the body of .aspx files


At the first test, there was a problem of losing Russian letters in the body of ASPX.

For example, such a file:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">   <title>Test</title> </head> <body>    . </body> </html> 




In the browser it was displayed like this:

image

')

The problem was solved by installing a fresh version of mono from lenny-backports.

aptitude -t lenny-backports install mono-apache-server libapache2-mod-mono





As a result, I got a readable text:

image



The second problem: switching the application language depending on the default language settings in the browser


In the IIS + .NET bundle, the process automatically switches its locale depending on the settings of the browser making the request. Mono, on the other hand, ignored the language parameters of the request and worked constantly in the English locale.



I had to independently make an HTTP module switching language settings for threads depending on the parameters passed to the browser request.



Module code:

 using System; using System.Collections.Generic; using System.Globalization; using System.Threading; using System.Web; // Class for set interface language from browser language setting. public class LangHTTPModule : IHttpModule { public void Dispose() { } public void Init(HttpApplication context) { context.BeginRequest += new EventHandler(context_BeginRequest); } // Set interface language. private void context_BeginRequest(object sender, EventArgs e) { HttpContext context = ((HttpApplication)sender).Context; HttpRequest req = context.Request; string reqLang = req.UserLanguages[0]; switch (reqLang.ToLower()) { case "ru": // Russian. reqLang = "ru-RU"; break; case "ru-ru": // Russian. reqLang = "ru-RU"; break; default: // Unknow value - set english (US) language. reqLang = "en-US"; break; } Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(reqLang); } } 




And I registered it in the web.config in the <system.web> section:

 <httpModules> <add name="LangHTTPModule" type="LangHTTPModule"/> </httpModules> 




The third problem: the use of non-unicode encodings


Portable application is a text parser for preparing materials for win-applications. Applications are old and can only work with CP-1251. Mono by default does not know this encoding.

And such a code

Encoding enc = Encoding.GetEncoding("Windows-1251");

throws an exception.



Additional encodings are in a separate library. Put the following command:

aptitude -t lenny-backports install libmono-i18n2.0-cil





Results


As a result, I received an ASP-application correctly working with Russian on the basis of Mono. Inside the application did not make any changes. The application uses both its own and already compiled external libraries, and there were no problems with them either. I think that the transition was easy and in the future I am going to continue to use Mono.



Source: https://habr.com/ru/post/88788/



All Articles