A simple library for a localizable messages in .NET
C#
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
LocalizableMessageStorage
LocalizableMessageStorageTests
.gitattributes
.gitignore
LICENSE
README.md

README.md

LocalizableMessageStorage

A simple library for a localizable messages in .NET

LocalalizableMessageStorage is a quick solution for internationalizing any UI. It works especially well with the MVC design pattern. It was inspired primarily by the SpringMessageSource in the JAVA world.

Basically, you have a a series of message files that contain key/value pairs for each locale. The syntax is very simple:

First you are allowed comment lines (no sections) with characters #, //, and -- If you want to have multiple lines per key, that is now supported. Just add a new line with no key. If you want to write multiple lines in the file, but don't want them actually to render as new line characters, then you can do a bash style escape on the last character of the line you don't want a newline for. e.g. key=Some line/ another line without a line break.

You have one file for each locale you wish to support, and then come up with any name scheme you want provided it contains the canonical locale name.

For example, I used messagesen-US.properties for my English file and messagesde-DE.properties for a German file.

The contents of the file should be something like this (please ignore the extra line breaks):

# EN-US Website localization messages.

#Feedback Page

fb_page_title = Tell us what you think

fb_submit_label = Submit

fb_feedback_empty = Feedback cannot be empty

fb_submission_error_msg = Submission failed. An unknown error occurred

fb_submission_success_msg = Thank you for your feedback!

fb_brief_desc_label = Brief Description

fb_comments_label = Comments

fb_default_brief_desc = User Feedback

fb_direct_feedback_msg = Form not working? Send us an email at {0}

fb_email_label = Email address

At this point you tell the library what directory to search for your locale files and a regex to use in determining the locale in each one. The regex should have exactly one match group containing the locale For example, for the previous example, I used

const string FilePattern = @".*messages([\w-]+).*\.properties";

var messageStorage = new LocalizableMessageStorage(@"C:\TestAppPath\Localization\", FilePattern);

Sample use:

var sendMessageSubjectValidationMessage = _localizableMessageStorage["SubjectValidationKey"];

var formatedMessage = _localizableMessageStorage.GetLocalizedMessageOnCurrentThread("SendMessageTitleKey", user.DisplayName);