Wednesday, September 18, 2013

Localization in Xamarin + MVVMCross



Here is how I implement localization in my Xamarin MVVMCross (what a great pair!) app:
#1. In the Core PCL project, create resource (project properties \ Resources) and use editor to enter all localizable strings in the app (or, you might use localization tools which produce the .resx file).
     Make sure to select Public visibility for the members of the generated Resources class.image
     For the resource name, I usually use the actual string + 'Text':
         'Username' –> UsernameText. 
         'Change password' –> ChangePasswordText.
     For long strings, I try to give it a short distinctive name  + usage + 'Text':
         'NewAppVersionAvailableMessageText'  -> 'A new version of the application is currently available. Would you like to download it?'
         'DeleteUserConfirmationText'  ->  'Are you sure you want to delete selected user?'
#2. I have a BaseViewModel class derived from MvxViewModel serving as a base for all my view models. It uses an indexer to get the translated string value based on an index resource value  
public class ViewModelBase : MvxViewModel
{
    public string this[string index]
    {
        get
        {
            return Resources.ResourceManager.GetString(index);
        }
    }

#3.
(for Android, but the idea is similar to other platforms too) In the layout, I am using MVVMCross binding to bind the controls to the view model’s indexer and pass the resource name:

<TextView local:MvxBind="Text [UsernameText]"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" />

At runtime, because of the MVVMCross binding between the TextView’s Text property and view model indexer, the Text property will call the view model’s indexer using the ‘UsernameText’ as index.
This in turn calls ResourceManager.GetString which returns the localized string.

This setup works great. I haven’t yet tried it on all the platforms but it makes sharing a single set of localized strings. Also, the resource strings can be referenced by the statically typed properties of the Resources class.

If the application needs to switch the language at runtime, it should be enough to tell the view model to notify its bound controls to refresh their values. I haven’t tried it yet, but viewModel.RaisePropertyChanged(string.Empty) should work.

There are different approaches to localization, here are few links:
N=21 - Internationalisation - i18n - N+1 Days of MvvmCros (using the MVMCross plugin) 
Using resx-files for localization in MvvmCross (mentioned in the video)
http://danielvaughan.org/post/Generating-Localized-Resources-in-Mono-for-Android-Using-T4.aspx (interesting ideas)

I am happy to hear your thoughts on this approach.

Thursday, September 12, 2013

WebBrowser control compatibility

in WPF\WinForms app, the WebBrowser control runs in a compatibility mode
this means it will run in an earlier version than then the current version of the IE on the machine
to disable it, you need to setup a registry key

\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION
"AppName.exe"=dword:00002328

webbrowser.reg

identify iPod model

Look on the back of the iPod and read the number "Model No. AXXXX"
Mine says A1367.
Then google the model number.

Thursday, September 05, 2013

Copy Paste stopped working


If Copy / Paste stops working it might because of a running Virtual PC / Remote desktop.
Close the Virtual PC / Remote desktop and it will start working again.

More info on this:
http://blogs.msdn.com/b/oldnewthing/archive/2008/06/04/8572362.aspx

Disable Windows Narrator

I found myself hitting Space + Win keys accidentally many times.
This registry setting will disable Narrator.

DisableNarrator.reg

How it works:
Windows has a feature to launch a debugger when an app is executed.
This can be done using the following String registry value:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\[AppName]\Debugger
What it's doing is it tells OS to launch debugger when the app is executed.
Usually Debugger value is the path of a debugger app like vsjitdebugger.exe.
In this case we put "%1" and it will simply fail.

More info on setting up a debugger launch for an app:
http://msdn.microsoft.com/en-us/library/a329t4ed(v=vs.90).aspx
http://blogs.msdn.com/b/greggm/archive/2005/02/21/377663.aspx


More info on different things which 'Image File Execution Options' can do:
http://blogs.msdn.com/b/junfeng/archive/2004/04/28/121871.aspx

This one goes into much low access details
http://blogs.msdn.com/b/reiley/archive/2011/07/30/a-debugging-approach-to-ifeo.aspx



Wednesday, September 04, 2013

Running multiple local sites in IIS

 

1. Create your site in IIS, e.g. Site1

2. Setup binding

IP Address: 127.0.0.1

Host: site1.localdev.com

3. edit C:\Windows\System32\Drivers\etc\hosts file

127.0.0.1       site1.localdev.com

Repeat the same steps for other sites Site2

Note that you should choose your names carefully, you won’t be able to get to the sites on internet