ASP.NET MVC & Javascript localization done right
- Singular
- Plural
_
, __
or ___
//var msg = "Some message";
//becomes
var msg = _("Some message");
<div>Some message</div>
<div>@_("Some message")</div>
Both the expression and the arguments will be HTML encodeed
@{ var names = "John&Doe"; }
<div>@_("Welcome {0}. Your age is < 100", name)</div>
//becomes
<div>Welcome John&Doe. Your age is < 100</div>
Make sure that you provide a valid HTML when you translate the expression
@{ var names = "John&Doe"; }
<div>@__("Welcome <b>{0}</b>. Your age is < 100", name)</div>
//becomes
<div>Welcome <b>John&Doe<b>. Your age is < 100</div>
This could be potentially dangerous if the arguments are not trusted i.e. come from user input such as query string, database etc. as it could lead to XSS attacks.
@{ var names = "John <i>Doe</i>"; }
<div>@__("Welcome <b>{0}</b>. Your age is < 100", name)</div>
//becomes
<div>Welcome <b>John <i>Doe</i><b>. Your age is < 100</div>
###Plural
_s
, __s
, ___s
//var msg = count == 1 ? "One item in the basket" : string.Format("{0} items in the basket", count);
//becomes
var msg = _s("One item in the basked", "{0} items in the basket", count);
@if (count == 1) {
<div>One item in the basked</div>
}
else {
<div>@count items in the basked</div>
}
//becomes
<div>@_s("One item in the basked", "{0} items in the basket", count)</div>
[More Documentation to be written]
In the meanwhile you could also navigate to your webiste to /Localization/Help
for more documentation
###Licensing information
The MIT License (MIT)
Copyright (c) 2014 DotNetWise
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.