WPF Localization at run-time
I needed a better WPF Localization solution and I came up with one and posted it over on my WPF blog.
I would love some feed back, so if you are interested, check it out.
Archive for January 2012
I needed a better WPF Localization solution and I came up with one and posted it over on my WPF blog.
I would love some feed back, so if you are interested, check it out.
In C# there is an object called Dictionary<TKey, TValue> that can be used to store data. The Dictionary<TKey, TValue> is essentially a collection of KeyValuePair<TKey, TValue> objects.
In this example, we create a simple dictionary with a few words and there definitions and show you how they are accessed.
using System; using System.Collections.Generic; using System.Linq; namespace DictionaryExample { class Program { static void Main(string[] args) { Dictionary<String, String> words = new Dictionary<string, string>(); words.Add("Hello", "An expression or gesture of greeting."); words.Add("Goodbye", "A concluding remark or gesture at parting."); words.Add("Computer", "A programmable usually electronic device that can store, retrieve, and process data."); words.Add("Friend", "One attached to another by affection or esteem"); Console.WriteLine("Word - Definition"); Console.WriteLine("==================================================="); foreach (KeyValuePair<string, string> pair in words) { Console.WriteLine(string.Format("{0} - {1}", pair.Key, pair.Value)); } } } }
You can access the value using the key. In the case of our word dictionary, the word is the key and the definition is the value.
While you could access the value as follows, there is a problem with the below method.
private string GetDefinition(String inWord, Dictionary<String, String> inDictionary) { return inDictionary[inWord]; }
Do you know what the problem is? Right, it doesn’t handle a missing value. If the value doesn’t exist, this will throw a KeyNotFoundException.
There are two ways to prevent the KeyNotFoundException.
TryGetValue() return a bool It takes in the Key and also an out reference. It populates the out reference. Here is an example.
private string GetDefinition(String inWord, Dictionary<String, String> inDictionary) { string retVal; // String is set to null if the value is not found return inDictionary.TryGetValue(inWord, out retVal) ? retVal : ; }
I am not a big fan of how the TryGetValue() function was implemented to return a bool However, I understand why it was implemented to return a bool. One might wonder why it returns bool. You may be thinking that TryGetValue() could return a value if found, null otherwise, right? Wrong! Reason 1 – Don’t forget that the value might actually be null. Reason 2 – While this Dictionary used a nullable type, string, another implementation might implement using a type that is not nullable, such as int.
Alternately you could check if the value exists first using ContainsKey(). Here is an example.
private string GetDefinition(String inWord, Dictionary<String, String> inDictionary) { return inDictionary.ContainsKey(inWord) ? inDictionary[inWord] : string.Empty; }
I prefer this because to me, it is more readable than the TryGetValue() function, but feel free to make your own opinion.
Now imagine you wanted to get all the words and their definitions that start with a certain letter. In this case you are creating a Dictionary that is a subset of the full Dictionary. You could do this with a foreach loop. Notice that the object in the foreach loop is a KeyValuePair.
private Dictionary<String,String> GetWordsAndDefinitionsWhereWordsStartWith(Dictionary<String, String> inDictionary, char inChar) { Dictionary<String, String> wordsAndDefinitionsWhereWordsStartWithC = new Dictionary<string, string>(); foreach (KeyValuePair<string, string> pair in inDictionary) { if (pair.Value.StartsWith(inChar.ToString(), StringComparison.CurrentCultureIgnoreCase)) wordsAndDefinitionsWhereWordsStartWithC.Add(pair.Key, pair.Value); } return wordsAndDefinitionsWhereWordsStartWithC; }
You could alternately use LINQ against the C# Dictionary to get the method body to be a single line.
private Dictionary<String,String> GetWordsAndDefinitionsWhereWordsStartWith(Dictionary<String, String> inDictionary, char inChar) { return inDictionary.Where(pair => pair.Value.StartsWith(inChar.ToString(), StringComparison.CurrentCultureIgnoreCase)).ToDictionary(pair => pair.Key, pair => pair.Value); }
You could just get the words and not the definitions in a List<String> as well.
private List<String> GetWordstartingWith(Dictionary<String, String> inDictionary, char inChar) { List<String> wordsStartingWith = new List<String>(); foreach (KeyValuePair<string, string> pair in inDictionary) { if (pair.Value.StartsWith(inChar.ToString(), StringComparison.CurrentCultureIgnoreCase)) wordsStartingWith.Add(pair.Key); } return wordsStartingWith; }
Again, you could use LINQ against the C# Dictionary to make this function one line.
private List<String> GetWordstartingWith(Dictionary<String, String> inDictionary, char inChar) { return (from pair in inDictionary where pair.Value.StartsWith(inChar.ToString(), StringComparison.CurrentCultureIgnoreCase) select pair.Key).ToList(); }
Today I noticed that a few sites, including WordPress.com have a corner banner on their pages to help alert others to a specific cause (in this case they are against SOPA & PIPA).
And so everyone can add a corner banner to their sites, I am going to post how to do it.
There are dozens of ways to create the images and if you can create it yourself, just do so.
Note: While I for this cause WordPress would care if you used their image, I thought it best to make my own if for no other reason than to use my color style.
Here is my image. Feel free to download it and use it.
#right-corner { position: fixed; /* Make sure you can align it exactly */ cursor: pointer; /* Change the cursor on mouse over */ top: 0px; /* Change to 100px to put it under a 100px banner */ right: 0px; /* Change to 100px to put it left of a 100px right-side bar */ z-index: 99999; /* make sure it is the top element always */ }
Note: If you do not have a css file, then go to step two and add this as well in step 2.
<style> #right-corner { position: fixed; /* Make sure you can align it exactly */ cursor: pointer; /* Change the cursor on mouse over */ top: 0px; /* Change to 100px to put it under a 100px banner */ right: 0px; /* Change to 100px to put it left of a 100px right-side bar */ z-index: 99999; /* make sure it is the top element always */ } </style>
<a id="right-corner" href="http://americancensorship.org/" target="_blank"> <img src="/wp-content/uploads/2012/01/Stop-Censorship.png" alt="Stop Censorship"> </a>
You now have a corner banner image on your page.
While I want to add a corner banner to my site and state that I am against SOPA and PIPA as well, here is my disclaimer:
I haven’t fully read the SOPA & PIPA document, so I am against it based on hearsay…but it is reliable hearsay. Also there may be statement in the document that I am for. Instead of telling you what I am against in the SOPA & PIPA document, it is easier just to write my thoughts.
If the bill could be written with those few sentences, it would pass easily and I don’t think anyone would care.
Note: Tested on FreeBSD 9
# cd /usr/ports/databases/mysql55-server
# make install
MySQL 5.5 Server (and MySQL 5.5 client) will download, compile, and install automagically for you.
Note: You may be wondering about the WITH_CHARSET option that used to exist. This is not necessary during compile and install and we will set the character set in a later step. Don’t start the MySQL service until we make these changes.
pkg_add -r mysql55-server
Configuration of MySQL is done in the my.cnf file.
For this example, we will change our server to use UTF8.
cd /usr/local/etc/
# # # | > /usr/local/etc/my.cnf echo '[mysqld]' >> /usr/local/etc/my.cnf echo character-set-server=utf8 >> /usr/local/etc/my.cnf echo collation-server=utf8_general_ci |
Note: FreeBSD has multiple example my.cnf files here: /usr/local/share/
# # |
echo # MySQL 5.5 Server >> /etc/rc.conf echo 'mysql_enable="YES"' >> /etc/rc.conf |
# /usr/local/etc/rc.d/mysql-server start
MySQL documentation covers this and I’ll not repeat it here. Instead, go here:
2.2 Securing the Initial MySQL Accounts
If you want to integrate Apache and PHP see these articles.
FreeBSD 9 comes with a new installer and installing it is quite fast.
I am not going to give you steps for burning an ISO, as you could be on Windows, Linux, OS X, and you could be using any of the DVD burning tools out there.
I’ll give you this hint though…Do not burn the ISO file onto the disk as a file.
Note: Skip this step if you are installing to a virtual machine.
Important! Do not boot of the DVD again!
Your system will now boot and create an SSH key on its own and give you a login prompt.
FreeBSD is now installed.
What do you want to do next?
Let me start by saying that I do not fall into the “my os is better than yours” mantra. I like to talk about the right tool for the job.
Those are just a generalizations based on the idea of using the right tool for the job. In every case others have different opinions and there are always exceptions. The appropriate thought is that you should use the right tool for the job.
So if you are a developer, what is the right tool for the job? Here is my new generalization.
Notice I said Apple Hardware, but I didn’t say that I recommend OS X. Run whatever OS you need.
Also, this has nothing to do with saying that Apple hardware is better. Dell, HP, Lenovo, Sony, and others all make some great hardware for running whatever OS you can run.
Many argue that Alienware makes the best hardware, not any of the above vendors. It is not about the best hardware. So if it was a hardware issue, I would be recommending Alienware. But it is not.
Let me repeat.
Notice, I qualified this by saying For a developer. I am not telling everybody to get Apple hardware.
Legally you cannot run OS X on any hardware other than Apple hardware (an no Hacintosh is not legal) . Since iOS development realistically can only happen on OS X, you cannot legally run iOS on hardware designed for windows either.
You need every Operating System today.
Lets talk about where development has taken us. We have gone from not really cross-platform (Windows only and sometimes support Macintosh too), to having to be able to code on Windows, Windows Phone 7, OS X, iOS, Android, and possibly Linux and Unix as well.
Lets list these in a table and you will see why buying an Apple Laptop frees you to develop on all those platforms.
Operating System | Apple Hardware | Windows Hardware |
---|---|---|
Windows | Run OS and Develop for it | Run OS and Develop for it |
BSD/Linux | Run OS and Develop for it | Run OS and Develop for it |
Android | Emulate OS and Develop for it | Emulate OS and Develop for it |
OS X | Run OS and Develop for it | Not supported* |
iOS | Emulate OS and Develop for it | Not supported* |
* While it can be done it is illegal and breaks license agreements.
Only Apple hardware allows you to install on all the popular Operating Systems that exist today.
So Apple has put themselves in an interesting position where cross-platform development on all popular operating systems can only occur on Apple hardware. It is this position that has me making this statement.
As for the operating system that I recommend, you probably have guessed from this post my recommendation: If you are a developer, run them all.