Welcome to FairlyLocalIt's a simple Internationalization (i18n) library that makes ASP.NET websites work with GNU's GetText tools. It's the way that i18n should have worked from the start.
download the library and tools.
The .po files that the build script generates won't be UTF8 by default. You'll need to open them in POEdit (or similar) and explicitly change the encoding the first time you edit them if you want your translated text to correctly show special characters. Sorry!
Note that you can still use UTF8 files with FairlyLocal (that's what it expects, actually). It's just the silly build script that's not generating new, empty .po files in UTF8. Fix it once when you start a new project and you'll be good to go from there on out.
Copying filesYou'll need to copy some of the files from the source distribution into your project and into places that the project can find them.
- Put the GNU stuff someplace you can find it relative to your project. Usually, you'll have a folder near your projects that holds common 3rd-party dependencies. That's a good spot for these tools.
- Drop the /Localization directory straight into your project, either right at the root or into a /Helpers folder if you have one.
Modifying an existing projectIf you're already using the Best-Practice technique of defining your own Page object that you use as the base for all your pages, this part should be simple. If not, you get to touch every file in the project, thus demonstrating why it's a Best Practice to not base anything directly off of System.Web.Page.
- Modify the base Page object for your site. Basically, this involves changing every WebForm that's currently inheriting from System.Web.Page to instead be based off of FairlyLocal.InternationalPage. Hopefully, you won't need to do a giant search & replace to pull this off because you already have your own MyApplicationPage that you're using. Either base MyApplicationPage off of InternationalPage or simply tear the meat out of InternationalPage and paste it into MyApplicationPage.
- Add a post-build action to your debug build. The code below does 3 things:
- creates a text file containing the paths to all the .aspx, .ascx , .cs, and .master files in your project.
- generates a .pot file based on _() calls in those source files.
- merges the .pot file into the (possibly) existing .po file for English
- merges the .pot file into the (possibly) existing .po file for Spanish
dir $(ProjectDir)\*.aspx /S /B > projectfiles.txt dir $(ProjectDir)\*.ascx /S /B >> projectfiles.txt dir $(ProjectDir)\*.cs /S /B >> projectfiles.txt dir $(ProjectDir)\*.master /S /B >> projectfiles.txt rem Create a new .pot from source, place it in the English folder, and merge with the existing .po file in English mkdir $(ProjectDir)\locale\en\LC_MESSAGES if not exist $(ProjectDir)\locale\en\LC_MESSAGES mkdir $(ProjectDir)\locale\en\LC_MESSAGES if not exist $(ProjectDir)\locale\en\LC_MESSAGES\messages.po copy $(ProjectDir)\locale\en\LC_MESSAGES\newmessages.pot $(ProjectDir)\locale\en\LC_MESSAGES\messages.po $(ProjectDir)..\Gnu\xgettext.exe -k_ --from-code=UTF-8 -LC# --omit-header -o$(ProjectDir)\locale\en\LC_MESSAGES\newmessages.pot -fprojectfiles.txt $(ProjectDir)..\Gnu\msgmerge.exe --backup=none -U $(ProjectDir)\locale\en\LC_MESSAGES\messages.po $(ProjectDir)\locale\en\LC_MESSAGES\newmessages.pot rem Merge the newly created .pot file with the Spanish translations: if not exist $(ProjectDir)\locale\es\LC_MESSAGES mkdir $(ProjectDir)\locale\es\LC_MESSAGES if not exist $(ProjectDir)\locale\es\LC_MESSAGES\messages.po copy $(ProjectDir)\locale\en\LC_MESSAGES\newmessages.pot $(ProjectDir)\locale\es\LC_MESSAGES\messages.po $(ProjectDir)..\Gnu\msgmerge.exe --backup=none -U $(ProjectDir)\locale\es\LC_MESSAGES\messages.po $(ProjectDir)\locale\en\LC_MESSAGES\newmessages.potNaturally, you'll need to fix up some paths so that this actually points to the folder where you dropped the gettext tools. You'll also need to modify it every time you add a new language to your project. Hopefully, doing so should be relatively straightforward.
February 22, 2010 by Jason Kester