120 likes | 221 Views
TntTranslator.aspx. A Complete ASP.NET Localization Solution. Localization ala VS 2005. The ASP.NET localization story in Visual Studio 2005 has improved a lot over past versions! This is in large part due to the built-in IDE command: Tools | Generate Local Resources
E N D
TntTranslator.aspx A Complete ASP.NET Localization Solution
Localization ala VS 2005 • The ASP.NET localization story in Visual Studio 2005 has improved a lot over past versions! • This is in large part due to the built-in IDE command: Tools | Generate Local Resources • This new command generates resource (resx) files per page allowing localization. • (Localization 101: You don’t hard-code text that should be translatable, but instead you move it into resources that can be translated.) • Although this is a great start, there are still five reasons why this is not yet ready for production!
“Generate Local Resources” 5 Critical Flaws • It can only be run from within the IDE on the current file (one file at a time). • Updates made to the page are not merged back to the resx file as the project evolves. • Custom or user controls are not covered. • Static text in the markup isn’t touched. • No source or object model is given by which the “Generate Local Resources” feature can be enhanced or fixed.
The .NET Community to the Rescue!http://blog.smart-ms.ordina.nl/2006/12/01/GenerateLocalResourceFiles.aspx • John Dekker (from the Netherlands) wrote a tool from scratch to emulate the “Generate Local Resources” feature and provided the source. • This tool brought me 95% towards the goal. Because it’s open source, I was able to enhance it. • First I worked on it until the resx files it generated were exactly the same as what VS generated. • Then I worked on it more so that it handled controls and text that even VS won’t touch. • Thanks, John!
3 Types of Text to Localize • Localizable Controls • Static Text in aspx markup • Resourcestrings (in my code) • Resourcestring is a Delphi language feature similar to strongly typed resources in .NET 2.0
Making it all “resourcestring” • As a Delphi developer, having all the additional resx files seemed messy. • I have to be able to translate “resourcestring” anyway for a complete solution. • So I decided to build a code-gen tool based on John Dekker’s tool that puts everything into resourcestrings. • Each page has a function called “TntTranslate()” that will translate controls on the page based on resourcestrings. • TntTranslate() is always called right before InitializeComponents() in OnInit(). • I can run this tool anytime, and it will go through my entire project and keep the TntTranslate() function on each page up to date. • This makes it easy to use source control to track what’s going on (no black box). Also, I can run this tool as part of my automated build process. • Send me an email at troy.wolbrink@ccci.org if you’d like the source for this tool.
{$REGION 'TntTranslator Managed Code'} resourcestring aspx_GiftGrid_Columns_0__HeaderText__2104471430 = 'Ministry Partner'; aspx_GiftGrid_Columns_1__HeaderText_1272578850 = 'Date'; aspx_GiftGrid_Columns_2__HeaderText__1051447098 = 'Type'; aspx_font__656683344 = 'Donor ID:'; aspx_NoDonationsLbl_Text = '(There are no donations available within this time frame.)'; aspx_p__2000594695 = 'CSV'; aspx_Title = 'Staff Portal :: Ministry Partner Information'; aspx_td__865700069 = 'Download:'; aspx_TopPartnerLbl_Text = 'Ministry Partner Information'; procedure TGiftsForm.TntTranslate; begin Self.GiftGrid.Columns[0].HeaderText := aspx_GiftGrid_Columns_0__HeaderText__2104471430; Self.GiftGrid.Columns[1].HeaderText := aspx_GiftGrid_Columns_1__HeaderText_1272578850; Self.GiftGrid.Columns[2].HeaderText := aspx_GiftGrid_Columns_2__HeaderText__1051447098; SetControlInnerText(Self, 'font', 'Donor ID:', aspx_font__656683344); Self.NoDonationsLbl.Text := aspx_NoDonationsLbl_Text; SetControlInnerText(Self, 'p', 'CSV', aspx_p__2000594695); Self.Title := aspx_Title; SetControlInnerText(Self, 'td', 'Download:', aspx_td__865700069); Self.TopPartnerLbl.Text := aspx_TopPartnerLbl_Text; end; {$ENDREGION}
Localizable <> Complete Solution • Everything that should be translated is now a “resourcestring”. (Good 1st step.) • What we need is a web page that presents to the translator a list of resourcestrings and lets them translate them into other languages. • The built-in resource manager in .NET looks at resx files in the application directory (not a good solution for online translation work). • I wrote a custom resource manager so that resources could be retrieved from (and saved to) a web service (based on SQL Server) instead of resx files. Why? • SQL Server is backed up regularly. wwwroot is not. • By default, an asp.net app can’t write out to wwwroot\etc.
Custom Resource Provider • Delphi.System creates its own private .NET ResourceManager. • Using reflection I was able to overwrite Delphi’s resource manager with my own. • I do this at Application_Start(): procedure TGlobal.Application_Start(sender: System.Object; e: EventArgs); begin TTntResourceManager.CheckDelphiSystem; end;
Demo the web site • https://dataserver.tntware.com/dataserver/test/ • Source code for this site is available at: https://dataserver.tntware.com/dataserver/test/staffportal/staffportal_source.zip • The webservice looks something like this: TLanguage = record Culture: string; end; TLanguageArray = arrayof TLanguage; TResourceStr = record TranID: string; RefValue: string; TranValue: string; end; TResourceStrArray = arrayof TResourceStr; function GetStaffPortalLanguages: TLanguageArray; function GetStaffPortalResourceStrings(Culture: string): TResourceStrArray; procedure SetStaffPortalResourceStrings(Culture: string; ResourceStrings: TResourceStrArray);
http://www.tntware.com/ Thanks!