Saturday, November 24, 2012

Using T4 for localizing JavaScript resources based on .resx files

In our project we're using two languages: Dutch and English. Furthermore, the IT company who built the application is using a framework on top of EXT.NET and thus there is a lot of JavaScript involved. There are a couple of solutions out there for localizing JavaScript in an ASP.NET MVC application, and I think the most common is this one:
<%$Resources:Resource, FieldName %>
But that his obvious limitations as described here Use ASP.NET Resource strings from within javascript files, namely you cannot use inside the body nor in .js files. I used a slightly different approach to localize the JavaScript in or project, based on the discussion here keeping three goals in mind:
  • Having only 1 place where resources are managed ( namely the .resx files )
  • Support for multiple cultures
  • Leverage IntelliSense - allow for code completion
Because of the last point - I decided to go with code generation. Code generation of course normally comes down to T4 Text Templates. So - currently we're using a text template in the /Scripts folder which generates Resources.js ( marked as 'copy to output directory' ) which is published on Web Deploy. Here's an example:
var Resources = {
  Common: {},
 };

Resources.Common.Greeting = { 
 'nl-NL': 'Hallo',
 'en-GB': 'Hi'
 };
Which gives me IntelliSense in JavaScript as it gives me in C#:
In the master page I'm including the JavaScript file and set the global space var 'locale' using the thread culture on the server:
   

This enables us to use the resources like this:
var msg = Resources.Common.Greeting[locale];
alert(msg);
And now for the T4 template:
<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ assembly name="System.Windows.Forms" #>
<#@ import namespace="System.Resources" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.IO" #>
<#@ output extension=".js"#>
<#
 var path = Path.GetDirectoryName(Host.TemplateFile) + "/../App_GlobalResources/";
 var resourceNames = new string[1]
 {
  "Common"
 };

#>
/**
* Resources
* ---------
* This file is auto-generated by a tool
* 2012 Jochen van Wylick
**/
var Resources = {
 <# foreach (var name in resourceNames) { #>
 <#=name #>: {},
 <# } #>
};
<# foreach (var name in resourceNames) { 
 var nlFile = Host.ResolvePath(path + name + ".nl.resx" );
 var enFile = Host.ResolvePath(path + name + ".resx" );
 ResXResourceSet nlResxSet = new ResXResourceSet(nlFile);
 ResXResourceSet enResxSet = new ResXResourceSet(enFile);
#>

<# foreach (DictionaryEntry item in nlResxSet) { #>
Resources.<#=name#>.<#=item.Key.ToString()#> = { 
 'nl-NL': '<#= ("" + item.Value).Replace("\r\n", string.Empty).Replace("'","\\'")#>',
 'en-GB': '<#= ("" + enResxSet.GetString(item.Key.ToString())).Replace("\r\n", string.Empty).Replace("'","\\'")#>'
 };
<# } #>
<# } #>
Find the code snippet here The resourceNames array takes the names of the resource files that I want included in the .js file. The solution is generic enough for our needs right now, and if you would want to include other languages - some changes would be required. The shortcomings of this solution are of course that the size of the .js file might become quite large. However, since it's cached by the browser, we don't consider this a problem for our application. However - this caching can also result in the browser not finding the resource called from code.

124 comments:

  1. Dear Jochen,

    What a joy to find your well thought generic solution!

    I've integrated it in my project and I'm happily taking advantage of strings stored in both .resx (*.pt-BR and .resx) files. No more duplication. DRY principle.

    I'm going to share your post here: http://stackoverflow.com/q/104022/114029


    Thanks and God bless your life,


    Leniel

    ReplyDelete
    Replies
    1. Solving It Puzzles: Using T4 For Localizing Javascript Resources Based On .Resx Files >>>>> Download Now

      >>>>> Download Full

      Solving It Puzzles: Using T4 For Localizing Javascript Resources Based On .Resx Files >>>>> Download LINK

      >>>>> Download Now

      Solving It Puzzles: Using T4 For Localizing Javascript Resources Based On .Resx Files >>>>> Download Full

      >>>>> Download LINK Oj

      Delete
  2. Thanks Leniel, glad you like it, thank you for the feedback.

    ReplyDelete
  3. Exactly what I needed! I came to the same conclusion as you, but the T4-part scared me. Fortunately, you have shown me the right path. Thank you.

    ReplyDelete
  4. Hello Jochen! If you are involved in l10n projects, I would suggest to evaluate a collaborative translation management platform like POEditor that can help you easily manage your projects.

    ReplyDelete
  5. Hi ,
    thanks for your great article , where can I find the code?!

    ReplyDelete
  6. I recently came across your blog and have been reading along. I thought I would leave my first comment.
    Java training in Chennai

    Java training in Bangalore

    ReplyDelete
  7. Thanks for sharing this amazing article. You are an awesome writer. Waiting for your future posts.
    IELTS Coaching in Chennai
    IELTS Training in Chennai

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. Thanks For sharing Your Information The Information shared Is Very Valuable Please Keep Updating Us Python Online Course Hadoop Online Course Data Science Online Course Aws Online Course

    ReplyDelete
  10. Elementary Quantitative Analysis Theory and Practice · Modern Production Among Backward Peoples · Unsolved mysteries of the Arctic · Philadelphia Elibrary

    ReplyDelete
  11. Thanks for Sharing this useful information. Get sharepoint apps development from veelead solutions

    ReplyDelete
  12. Thank you for providing the valuable information …
    If you want to connect with AI (Artificial Intelligence) World
    as like

    Python


    RPA (Robotic Process Automation)


    UiPath Training


    Blue Prism


    Data -Science


    ML(Machine Learning)
    related more information then meet on EmergenTeck Training Institute .

    Thank you.!
    Reply

    ReplyDelete
  13. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging!!Machine Learning Course Bangalore

    ReplyDelete
  14. ES File Explorer Pro Apk is a premium version of es file explorer file manager Learn More

    ReplyDelete
  15. Nice Blog, The thoughts are clear and well explained. Thank you for sharing your work, truly worth reading. I really thankful to you, Keep it up!! Machine Learning Course

    ReplyDelete
  16. Glad to chat your blog, I seem to be forward to more reliable articles and I think we all wish to thank so many good articles, blog to share with us.
    360digitmg data analytics course malaysia

    ReplyDelete
  17. Nice information, valuable and excellent design, as share good stuff with good ideas and concepts, lots of great information and inspiration, both of which I need, thanks to offer such a helpful information here.

    data analytics courses malaysia

    ReplyDelete
  18. Congratulations This is the great things. Thanks to giving the time to share such a nice information.best Mulesoft training in bangalore

    ReplyDelete
  19. The Information which you provided is very much useful for Agile Training Learners. Thank You for Sharing Valuable Information.Salesforce CRM Training in Bangalore

    ReplyDelete
  20. Enjoyed reading the article above, really explains everything in detail, the article is very interesting and effective. Thank you and good luck…

    Start your journey with Database Developer Training in Bangalore and get hands-on Experience with 100% Placement assistance from experts Trainers @Bangalore Training Academy Located in BTM Layout Bangalore.

    ReplyDelete
  21. Enjoyed reading the article above, really explains everything in detail, the article is very interesting and effective. Thank you and good luck.

    Real Time Experts is a leading SAP CRM Training Institutes in Bangalore providing real time and Job oriented SAP CRM Course with real time Expert Trainers who are Working Professionals with 6+ Years of SAP CRM Experience.

    ReplyDelete
  22. Let me assume, you have chosen the niche and the very next step, choose the domain and hosting to get started. You can also our top Web Hosting Deals page are Blogging we listed companies offering legal deals where you can reduce your expenses while starting your blogging business.

    ReplyDelete
  23. This post is really nice and informative. The explanation given is really comprehensive and informative . Thanks for sharing such a great information..Its really nice and informative . Hope more artcles from you. I want to share about the best java tutorial videos with free bundle videos provided and java training .

    ReplyDelete
  24. This comment has been removed by the author.

    ReplyDelete
  25. We as a team of real-time industrial experience with a lot of knowledge in developing applications in python programming (7+ years) will ensure that we will deliver our best in python training in vijayawada. , and we believe that no one matches us in this context.

    ReplyDelete
  26. This comment has been removed by the author.

    ReplyDelete
  27. Snapdeal Winner List 2020 here came up with an Offer where you can win Snapdeal prize list by just playing a game & win prizes.
    Snapdeal winner name also check the Snapdeal lucky draw

    ReplyDelete
  28. Whatever we gathered information from the blogs, we should implement that in practically then only we can understand that exact thing clearly, data science training but it’s no need to do it, because you have explained the concepts very well. It was crystal clear, keep sharing..data science tutorial

    ReplyDelete




  29. I am really happy to say it’s an interesting post to read . I learn new information from your article , you are doing a great job . Keep it up and a i also want to share some information regarding selenium online course and selenium videos


    ReplyDelete
  30. Thanks for sharing the best blog
    Shopclues lucky draw 2020| Shopclues winner 2020|Get to Know Shopclues Lucky Draw Winner 2020. Call Shopclues Helpline Phone Number+91-9835565523 for Shopclues Online Shopping Lucky Draw.
    Shopclues online lucky draw winner
    Shopclues winner 2020
    shopclues car lucky draw

    ReplyDelete


  31. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.i want to share about learn java Programming and java tutorial videos for beginners .

    ReplyDelete
  32. Awesome blog I enjoyed reading your articles. This is a really informative post.
    I am also sharing some nice stuff to you guys please go through it and take a review.
    hire a virtual assistant
    vitual assistant
    hiring a virtual assistant
    hiring virtual assistants
    developer web
    free lance web developer
    web developer

    ReplyDelete
  33. Hey, thanks for this great article I really like this post and I love your blog and also Check Python course Training in 360DIGITMG. Python Training certification program provides an overview of how Python and R programming can be employed in Data Mining of structured (RDBMS) and unstructured (Big Data) data. Comprehend the concepts of Data Preparation, Data Cleansing and Exploratory Data Analysis. Perform Text Mining to enable Customer Sentiment Analysis. Learn Machine learning and developing Machine Learning Algorithms for predictive modeling using Regression Analysis. Assimilate various black-box techniques like Neural Networks, SVM and present your findings with attractive Data Visualization techniques.
    360Digitmg Python Training institute

    ReplyDelete
  34. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for learn hadoop online sharing this great content to my vision, keep sharing.

    ReplyDelete
  35. I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!. machine learning courses in Bangalore

    ReplyDelete
  36. This comment has been removed by the author.

    ReplyDelete
  37. Nice article. For offshore hiring services visit:
    livevictoria

    ReplyDelete
  38. I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work....machine learning courses in bangalore

    ReplyDelete
  39. Thanks for the informative article About Devops. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.

    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

    ReplyDelete
  40. This is exactly the information I'm looking for, I couldn't have asked for a simpler read with great tips like this... Thanks! ExcelR Data Science Course In Pune

    ReplyDelete
  41. Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing.....machine learning courses in bangalore

    ReplyDelete
  42. Thanks for sharing this informations.
    Blue prism training in coimbatore

    embedded training institute in coimbatore

    C and C++ training in coimbatore

    ios training in coimbatore

    Software Testing Course in Coimbatore

    Selenium Training in Coimbatore

    CCNA Course in Coimbatore

    CCNA Training Institute in Coimbatore

    ReplyDelete
  43. thanks for sharing wonderful blog like this.keep posting us.As the most reputed website designers in Chennai, our work is always elegant & has a visual story to it. Our team comprises the best website designers in India.


    digital marketing agencies in chennai | best web developers and designers in chennai | best website designing companies in chennai | | Website designers in chennai | Best logo designers in chennai

    ReplyDelete
  44. Thanks for sharing this valuable information on High DA. I hope you will keep us updated on it.Its is very fascinating the way that this blog were made. i am very interested to view how do this blog were made.I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.Java training in Chennai

    Java Online training in Chennai

    Java Course in Chennai

    Best JAVA Training Institutes in Chennai

    Java training in Bangalore

    Java training in Hyderabad

    Java Training in Coimbatore

    Java Training

    Java Online Training

    ReplyDelete
  45. Thanks for sharing. I didn’t know one could build backlinks from these platforms. I’ll try them out.Thanks for sharing this valuable information on High DA. I hope you will keep us updated on it.Its is very fascinating the way that this blog were made. i am very interested to view how do this blog were made.I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this siteJava training in Chennai

    Java Online training in Chennai

    Java Course in Chennai

    Best JAVA Training Institutes in Chennai

    Java training in Bangalore

    Java training in Hyderabad

    Java Training in Coimbatore

    Java Training

    Java Online Training

    ReplyDelete
  46. thanks for sharing with us.keep posting like this with us.River Group of Salon and spa, T.Nagar, provide a wide range of spa treatments, like body massage, scrub, wrap and beauty parlour services. We ensure unique care and quality service.

    massage in t.nagar | body massage t.nagar | massage spa in t.nagar | body massage center in t.nagar | massage centre in chennai | body massage in chennai | massage spa in chennai | body massage centre in chennai | full body massage in t.nagar

    ReplyDelete
  47. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...

    AWS training in Chennai

    AWS Online Training in Chennai

    AWS training in Bangalore

    AWS training in Hyderabad

    AWS training in Coimbatore

    AWS training

    ReplyDelete
  48. great article blog.thanks for posting.keep updating us,River Group of Salon and spa, T.Nagar, provide a wide range of spa treatments, like body massage, scrub, wrap and beauty parlour services. We ensure unique care and quality service.

    massage in t.nagar | body massage t.nagar | massage spa in t.nagar | body massage center in t.nagar | massage centre in chennai | body massage in chennai | massage spa in chennai | body massage centre in chennai | full body massage in t.nagar

    ReplyDelete
  49. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging!!
    acte reviews

    acte velachery reviews

    acte tambaram reviews

    acte anna nagar reviews

    acte porur reviews

    acte omr reviews

    acte chennai reviews

    acte student reviews

    ReplyDelete
  50. This is the best post I have ever seen. Very clear and simple. Mid-portion Is quite interesting though. Keep doing this. I will visit your site again.
    valorant phoenix jacket

    ReplyDelete

  51. Attend The PMP Certification From ExcelR. Practical PMP Certification Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The PMP Certification.


    PMP Certification

    ReplyDelete
  52. This was not just great in fact this was really perfect your talent in writing was great.ExcelR Data Analytics Courses

    ReplyDelete
  53. I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.Business Analytics Courses

    ReplyDelete
  54. Thanks for the Valuable information.Really useful information. Thank you so much for sharing. It will help everyone.

    SASVBA is recognized as the best machine learning training in Delhi. Whether you are a project manager, college student, or IT student, Professionals are the best machine learning institute in Delhi, providing the best learning environment, experienced machine learning instructors, and flexible training programs for the entire module.

    FOR ORE INFO:

    ReplyDelete
  55. it’s really nice and meanful. it’s really cool blog. Linking is very useful thing.you have really helped lots of people who visit blog and provide them usefull information.
    Data Science Training in Hyderabad

    I am really happy to say it’s an interesting post to read . I learn new information from your article , you are doing a great job . Keep it up

    Devops Training in Hyderabad

    Hadoop Training in Hyderabad

    Python Training in Hyderabad

    Tableau Training in Hyderabad

    Selenium Training in Hyderabad

    ReplyDelete
  56. it’s really nice and meanful. it’s really cool blog. Linking is very useful thing.you have really helped lots of people who visit blog and provide them usefull information.
    Data Science Training in Hyderabad

    I am really happy to say it’s an interesting post to read . I learn new information from your article , you are doing a great job . Keep it up

    Devops Training in Hyderabad

    Hadoop Training in Hyderabad

    Python Training in Hyderabad

    Tableau Training in Hyderabad

    Selenium Training in Hyderabad

    ReplyDelete
  57. I am very happy to read this. This is the kind of manual that needs to be given and not the accidental misinformation that's at the other posts. Appreciate your sharing this greatest doc. massage centre in coimbatore

    ReplyDelete
  58. Thanks for the interesting content. I like your post and your blog is amazing.
    If you are interested in Video Downloader apps you can check my blog site. It is new and really informative.

    VidMate app download 9apps

    ReplyDelete
  59. Really it was an awesome article,very interesting to read.
    You have provided an nice article,Thanks for sharing.
    Data Science Training In Pune

    ReplyDelete
  60. Incredible data. Fortunate me I went over your site by some coincidence (stumbleupon). I've book-checked it for some other time! best interiors

    ReplyDelete
  61. It's actually a great and helpful piece of information. I am satisfied that you just shared this useful information for us. 49ers varsity jacket

    ReplyDelete
  62. I am very much pleased with the contents you have mentioned. I wanted to thank you for this great article.Purple Joker Jacket

    ReplyDelete
  63. Thanks for sharing such a worthy information. Keep update like this.....
    IELTS Coaching in Hyderabad
    IELTS Coaching in Bangalore

    ReplyDelete
  64. Nice blog and absolutely outstanding. You can do something much better but i still say this perfect.Keep trying for the best.
    data analytics course in hyderabad

    ReplyDelete
  65. This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information... data science training in kanpur

    ReplyDelete
  66. Solving It Puzzles: Using T4 For Localizing Javascript Resources Based On .Resx Files >>>>> Download Now

    >>>>> Download Full

    Solving It Puzzles: Using T4 For Localizing Javascript Resources Based On .Resx Files >>>>> Download LINK

    >>>>> Download Now

    Solving It Puzzles: Using T4 For Localizing Javascript Resources Based On .Resx Files >>>>> Download Full

    >>>>> Download LINK

    ReplyDelete
  67. wordpress website design studio Need professional WordPress Web Design Services? We're experts in developing attractive mobile-friendly WordPress websites for businesses. Contact us today!

    ReplyDelete
  68. Thank you so much for sharing this information. Do visit ASP.NET training in Chennai

    ReplyDelete
  69. I am thoroughly impressed by your excellent post and review. Please continue to produce such remarkable content. Thank you!

    CEC Best Colleges in Hyderabad

    ReplyDelete
  70. I'm delighted to express that your post is quite captivating. I've gained fresh insights from your write-up, and you're doing an exceptional job. Keep up the good work. For More Vist this website

    Top CA Colleges in Hyderabad

    ReplyDelete
  71. I am delighted to express that your post is captivating to read. I acquire fresh knowledge from your article, and you are doing an excellent job. Please continue your great work.

    Best CA Coaching in Hyderabad

    ReplyDelete
  72. I haven't come across such a valuable resource in a long time. It is well-written and contains excellent information. I am truly grateful to you for sharing it.

    Best CA Coaching Centers in Hyderabad

    ReplyDelete
  73. I haven't come across such a valuable resource in a long time. It is excellently written and contains incredibly useful information. I truly cannot express my gratitude enough for sharing it.

    Best CA Colleges in Hyderabad

    ReplyDelete
  74. Your blog is truly amazing. The content is informative and full of knowledge. I had a great time reading your article. Please continue to share more content like this. Thank you.

    Top CA Colleges in Hyderabad

    ReplyDelete