Skip to content
itechnology edited this page Dec 5, 2012 · 11 revisions

#TOC

  • Basic Usage
  • Advance Usage
  • Configuration Options

Basic Usage

###Inside a non website project Drop T4ResX.tt inside any project containing RESX files, open it, and do a CTRL+S to save it. This will trigger the T4 code generation and output strongly typed classes in the form of T4ResX.cs. Compile the project, and you now have a strongly typed assembly that you can use inside other projects.

Then:

  1. Start writing code like
    • [Required( ErrorMessageResourceName = "RequiredError", ErrorMessageResourceType = typeof(Resources.User))]
    • public string PseudoError(params string[] errors) { return Resources.User.PseudoError(errors); }
    • string translated = Resources.User.Pseudo;

###Inside web pages Once your localized assembly generated and compiled, include a reference to it in you website project.

If you are not using a separate assembly to localize your files, drop T4ResX.tt in the root of your website project, double click to open it, do a CTRL+S to save it. Now that your localized code is generated, rebuild your site.

Then:

  1. Start writing code like @Resources.User.Pseudo or <%= Resources.User.Pseudo %> depending on whether you are using MVC or ASP.NET

###Localized JavaScript Once your localized assembly generated and compiled, include a reference to it in you website project.

Then :

  1. We first need to include a small helper method inside our project to be able to return JSON serialized content.
  2. We declare our method in our page or controller that returns the JSON generated above in the form of raw JavaScript.
  3. Include a <script src="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/MyController/MyMethod?ns=NameSpaceToLoad"></script> inside whatever page you want to use localized JavaScript in.

Then:

  • Start writing JavaScript like alert(Resources.User.PseudoExists)

#Advanced Usage (docs in progress....)

  • Formatting Variables
    • Doing things like Field {0}, must be different than field {1}
    • This for example, is how MVC formats error messages
  • Replacement Variables
    • Doing things like Hello {0}, welcome to {BRAND}
      • This allows you to store replacement variables for things like, DOMAIN, BRAND, PRODUCTNAME, ETC inside a database or a configuration file, and then have those variables replaced on the fly.
  • Constants
  • Pulling parts out of the assembly in the form of Dictionary<string, Dictionary<string, string>>
    • This is how we are able to store text in RESX files and then extract parts of it into localized JavaScript files.

#Configuration Options (docs in progress....)

  • Removing redundant *.designer.cs files
  • Setting up a custom replacement function
  • Configuring your website to accept switching cultures (cookie, hostname, querystring, preference, ...)
    • Via web.config
    • Via HttpModule
    • Via inline code
Clone this wiki locally