Interesting approach to localization

131 views
Skip to first unread message

Laurent Bugnion, GalaSoft

unread,
Sep 8, 2009, 5:26:52 PM9/8/09
to wpf-di...@googlegroups.com

Hey guys,

 

Here is what I think is an innovative approach to localization, using a standard binding syntax. As far as I can judge, this solution will fail in Silverlight (as will most other solutions anyway…), but it has the advantage of being dynamic, of using a Binding syntax which is more familiar than a custom markup extension (even though the “dot” and array syntax is probably not very familiar to many newbies), and provides a fallback value for design mode.

 

http://www.grumpydev.com/2009/09/08/localising-wpf-applications-using-resx-files-and-standard-data-binding-without-a-markupextension/

 

What do you guys think?

 

Laurent

--

Laurent Bugnion [Microsoft MVP, MCP]

Blog: http://blog.galasoft.ch

Web: http://www.galasoft.ch

Support children in Calcutta: http://www.calcutta-espoir.ch

 

cid:image001.png@01C9C8AA.B722DA80

My

business

card

as

Microsoft Tag

 

 

image001.png

Corrado Cavalli

unread,
Sep 8, 2009, 6:19:31 PM9/8/09
to wpf-di...@googlegroups.com

Very interesting and far better that locbaml stuff, love to have same syntax on SL too, at the moment closest counterpart is the one’s from Tim Heuer

http://timheuer.com/blog/archive/2009/08/26/silverlight-string-localization.aspx

 

-Corrado

image001.png

Eric Burke

unread,
Sep 8, 2009, 7:03:26 PM9/8/09
to wpf-di...@googlegroups.com
We did something similar on Messenger except we used a MarkupExtension:

MyProp="{local:MsgrString MyStringName}"
 
No bindings to update since the string is static per locale.  No requirement to change locale on the fly.

It also allowed us to use it as a format string:

MyProp="{Binding Path=SomeValue, StringFormat={local:MsgrString MyStringFormatKey}}"

All our strings were stored in XAML files instead of RESX and loaded at runtime.  One of our devs had written a locbaml processor to generate satellite DLLs from our XAML files based on our custom translation database that is used across products.



From: "Laurent Bugnion, GalaSoft" <lau...@galasoft.ch>
To: wpf-di...@googlegroups.com
Sent: Tuesday, September 8, 2009 5:26:52 PM

Subject: [WPF Disciples] Interesting approach to localization

Corrado Cavalli

unread,
Sep 9, 2009, 1:05:10 AM9/9/09
to wpf-di...@googlegroups.com

BTW: In my current Silverlight project I’ve created a LocalizedViewModelBase as base for others Viewmodel that requires localization:

 

public class LocalizableViewModelBase : ViewModelBase

   {

      private static Resources resources = new Resources();

      private static CultureInfo cachedCulture;

 

      public Resources LocalizedText

      {

         get

         {

            return resources;

         }

      }

 

      protected void SetCulture(string userCulture, bool cacheCurrent)

      {

         …

         if (Thread.CurrentThread.CurrentUICulture.Name != userCulture)

         {           

            Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(userCulture);

            this.RaisePropertyChanged("LocalizedText");           

         }

      }

   }

 

and then I bind UI elements this way:

 

<TextBlock Text="{Binding LocalizedText.UserName, Mode=OneWay}"

                             TextWrapping="Wrap"

                             Height="15"

                             VerticalAlignment="Bottom"

                             Grid.Column="1" />

What do you think?

 

-Corrado

 

From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com] On Behalf Of Laurent Bugnion, GalaSoft


Sent: martedì 8 settembre 2009 23:27
To: wpf-di...@googlegroups.com

Subject: [WPF Disciples] Interesting approach to localization

image001.png

Laurent Bugnion, GalaSoft

unread,
Sep 9, 2009, 1:48:03 AM9/9/09
to wpf-di...@googlegroups.com

Hey Corrado,

 

How do you set the texts in the resources? Do you use the Resx file?

 

Laurent

image001.png

Peter O'Hanlon

unread,
Sep 9, 2009, 3:37:21 AM9/9/09
to wpf-di...@googlegroups.com
I think that I'd just finished putting this into my MVVM framework, and I was feeling pretty smug about it, now I feel less smug because somebody else got there first.
--
Peter O'Hanlon
image001.png

Corrado Cavalli

unread,
Sep 9, 2009, 3:46:53 AM9/9/09
to wpf-di...@googlegroups.com

Yes, I use Visual Studio integrated editor, works pretty well…

 

-Corrado

 

From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com] On Behalf Of Laurent Bugnion, GalaSoft


Sent: mercoledì 9 settembre 2009 07:48
To: wpf-di...@googlegroups.com

Subject: [WPF Disciples] Re: Interesting approach to localization

 

Hey Corrado,

 

How do you set the texts in the resources? Do you use the Resx file?

 

Laurent

 

From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com] On Behalf Of Corrado Cavalli


Sent: Wednesday, September 09, 2009 7:05 AM

To: wpf-di...@googlegroups.com
Subject: [WPF Disciples] Re: Interesting approach to localization

 

BTW: In my current Silverlight project I’ve created a LocalizedViewModelBase as base for others Viewmodel that requires localization:

image001.png

Laurent Bugnion, GalaSoft

unread,
Sep 9, 2009, 4:30:17 AM9/9/09
to wpf-di...@googlegroups.com

Looking good. I made myself a note to look into this more closely later J

image001.png

Sacha Barber

unread,
Sep 9, 2009, 5:28:52 AM9/9/09
to wpf-di...@googlegroups.com
I quite like this approach, and have used something simliar a while ago, Ill see if I still have the code somewhere (it got canned), and send it around.
 
I agree with Corrado, it does work well using RESX file in VS
 

From: corrado...@gmail.com

To: wpf-di...@googlegroups.com
Subject: [WPF Disciples] Re: Interesting approach to localization
Date: Wed, 9 Sep 2009 09:46:53 +0200

New! Receive and respond to mail from other email accounts from within Hotmail Find out how.
image001.png

Daniel Vaughan

unread,
Sep 9, 2009, 6:30:29 AM9/9/09
to WPF Disciples
Yes interesting.
Resources, hmmm this sounds like a candidate for a T4 solution.

You could do the following at the moment with the MetadataGeneration
template I wrote about last week. If you have a public access modifier
on resource properties:

<Window.Resources>
<blah:StringResources x:Key="strings"/>
</Window.Resources>
...
<TextBox Text="{Binding Source={StaticResource strings}, Path=
{x:Static Metadata:StringResourcesMetadata.JustATestPath},
Mode=OneWay}" />

Somewhat verbose, but it has the benefit of being validated at compile
time. Although it lacks support for changing culture at runtime. I
will fix that and improve the syntax etc.

Thanks for the article headsup.


On Sep 8, 11:26 pm, "Laurent Bugnion, GalaSoft" <laur...@galasoft.ch>
wrote:
> Hey guys,
>
> Here is what I think is an innovative approach to localization, using a
> standard binding syntax. As far as I can judge, this solution will fail in
> Silverlight (as will most other solutions anyway.), but it has the advantage
> of being dynamic, of using a Binding syntax which is more familiar than a
> custom markup extension (even though the "dot" and array syntax is probably
> not very familiar to many newbies), and provides a fallback value for design
> mode.
>
> http://www.grumpydev.com/2009/09/08/localising-wpf-applications-using...
> iles-and-standard-data-binding-without-a-markupextension/
>
> What do you guys think?
>
> Laurent
>
> --
>
> Laurent Bugnion [Microsoft MVP, MCP]
>
> Blog:  <http://www.galasoft.ch/>http://blog.galasoft.ch
>
> Web:  <http://www.galasoft.ch/>http://www.galasoft.ch
>
> Support children in Calcutta:  <http://www.calcutta-espoir.ch/>http://www.calcutta-espoir.ch
>
> cid:image001....@01C9C8AA.B722DA80
>
> My
>
> business
>
> card
>
> as
>
>  <http://www.microsoft.com/tag> Microsoft Tag
>
>  image001.png
> 7KViewDownload

Sacha Barber

unread,
Sep 9, 2009, 6:33:55 AM9/9/09
to wpf-di...@googlegroups.com
Ha Ha,
 
Thats how my articles starto too, I run out of ideas and then BANG a new idea comes in
 
 

 
> Date: Wed, 9 Sep 2009 03:30:29 -0700

> Subject: [WPF Disciples] Re: Interesting approach to localization

Daniel Vaughan

unread,
Sep 9, 2009, 7:03:42 AM9/9/09
to WPF Disciples
Sacha Barber running out of ideas. That'll be the day! :)


On Sep 9, 12:33 pm, Sacha Barber <sachabar...@hotmail.com> wrote:
> Ha Ha,
>
> Thats how my articles starto too, I run out of ideas and then BANG a new idea comes in
>
>
>
> > Date: Wed, 9 Sep 2009 03:30:29 -0700
> > Subject: [WPF Disciples] Re: Interesting approach to localization
> > From: dbvaug...@gmail.com
> _________________________________________________________________
> Learn how to add other email accounts to Hotmail in 3 easy steps.http://clk.atdmt.com/UKM/go/167688463/direct/01/
Reply all
Reply to author
Forward
0 new messages