Welcome to MSDN Blogs Sign in | Join | Help

DevDivLocTeamBlog

This blog is written by members of the Developer Division Localization Team.

Syndication

Tags

    No tags have been created or used yet.
Hardcoded resources

When we think about localizability, the first thing that jumps to our mind is "hardcoded strings". Check on the GlobalDev page about isolating localizable resources (http://www.microsoft.com/globaldev/getwr/steps/wrg_locresources.mspx), it's right there , first entry. The page is filled with words like "most extreme cases", "biggest nightmare", which should scare everyone reading the post.

On the day-to-day side, this is what we tell our developers: "It's OK (note the word choice: OK, not good, not recommended, but OK, acceptable) to hardcode a string if you're working on a prototype; as soon as your code will turn into production, even if there aren't short-term plans to localize your project, DON'T HARDCODE". What we notice is that for sure, it's a lot easier to write:

// Display string in MessageBox

MessageBox.Show("Hello world!");

Than:

// Declare a Resource Manager instance

ResourceManager LocRM = new ResourceManager("MyStrings",Assembly.GetExecutingAssembly());

// Assign the string for the "strHelloWorld" key to a messagebox

MessageBox.Show(LocRM.GetString("strHelloWorld"));

Besides that on top of that, you've had to add the "Hello World!" string to a resource file! It takes 30 seconds in the first case, it takes the double in the second one. That's a lot...

But then, when you've written 1000 lines, 10000 lines, 500000 lines projects, with the first method, and you have to make your project localizable by moving all these hardcoded strings into resources, you're not going to get this done in 30 seconds per string; you're going to forget strings; bump into false positives (you're going to have to crawl through double-quoted strings that should stay in the code and not go in the resources). Even if your V1 application is targeting your local market, let's imagine that it gets so successful that you want to expand its reach… That's the benefit of doing it right from the start, and not hardcoding strings.

Like any topic, this can get controversial of course, especially when it comes to the contrary situation: putting in resource files strings that shouldn't be localized, e.g. registry keys, references to function names, filenames, … Why would you have in the resources a string that says "user32.dll"?! There are lot of cons to this practice:

  • the biggest risk you take when you do this is that your strings can get over-localized (user32.dll becomes utilisateur32.dll; I know, I'm pushing this a little here with this example, but these things happen…). So now you're reading the string, your code is expecting an existing and valid filename, and your functionality is broken, because of localization (!). And we are not even going into the scenario of a malicious usage of the string, that could direct your code somewhere else.
  • these kind of resources can add-up to your word count estimates, and you'll end up paying unnecessary "translation" work for them

This makes sense, so why is it controversial? Because as a developer, you'll say: "It was easier to have it in the resources than adding another global variable/constant to my program". That's a valid reason, but not one we hear very much on these days of memory galore. What we hear most of the time now is: "My resources are being shared with other programs, I just can't hardcode them". We, localization teams, don't have the silver bullet against that one; we still have a recommendation that will help though: if you're developing in managed code, give your resource ID a meaningful name, such as "DoNotLoc_User32_Filename". Localizers will see these resource IDs, can hopefully even filter them and not pay attention to them. If you're in non-managed code, then get an agreement with your localization team: "All resources which ID > 10000 must not be localized".

Communication is key with our day-to-day work. You mitigate a lot of risk by building this developer-localization dialog. As you can see, I haven't talked about tools, just best practices, common sense, communication.

Published Monday, September 12, 2005 7:49 PM by LocTeam

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Hardcoded resources @ Monday, September 12, 2005 4:50 PM

You know this is a very valid and good post, however it reminds me one time while taking a Microsoft Certification Course years ago, however it still applies today. The instructor started off the class, said something along these lines. "Your getting twice the value in this course by me as a teacher, because I am going to show you everything in the book by Microsoft because I am required to. Then I am going to show you the right way."

Basically his main point was that every single book produced in these courses had hard coded strings. Also that this time he offered to buy lunch for anyone who could bring in a computer book on a programming language that specifically told you and built their examples without hard coding strings. No one got a free lunch back then. But he drilled the not hard coding into our heads and made us really think to get through his course. However you usually aren't taught this and before this a lot of my prior code had hard coded strings.

A few years later, I remember sitting in a room with fellow developers after we had aquired several locations overseas and all of our apps needed to be translated and rebuilt and we needed to get a collection of the strings to be translated. All the developers were complaining and so on about thier apps and the time it was going to take. I just did a dump of the resources and said here you go. Got the translations and just plugged them in. Before that time I used to get a lot of guff from guys saying I took too long to program some stuff. But if you do it right, you save time in the long run.

Jeff Parker

# re: Hardcoded resources @ Friday, September 16, 2005 9:27 PM

Thanks for sharing your experience Jeff!

Note also that we are advocating this best practice with the same methods as your instructor... In Visual Studio 2005, with our Starter Kits, or almost any project with a little bit of UI, we hardcoded strings all over the place (!). Like in your example, it's hardcoded because we want to focus on the technology supported by the sample/project: do we need to over-complicate these with all the best practices when we just want to demo how to connect to a SQL Server database? On the other hand, on our SDK localization samples, we go the whole nine yards on how it should be done.

Again, thanks for your input!

Olivier.

LocTeam

# DevDivLocTeamBlog Hardcoded resources | Insomnia Cure @ Tuesday, June 09, 2009 8:32 PM

PingBack from http://insomniacuresite.info/story.php?id=9208

DevDivLocTeamBlog Hardcoded resources | Insomnia Cure

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
Page view tracker