Silverlight localization

放肆的年华 提交于 2020-01-04 05:37:25

问题


What are the avaliable options for translating a silverlight 5 application (Prism + MEF)? If possible I would like to:

  • not have resx files (I don't like the way they are managed)
  • rather have xaml string extracted and then translated (I think there was a tool from Microsoft, that marked all XAML nodes with custom attribute and extracted the strings)
  • have translations in external assemblies (in Prism they could be modules) and load them when needed
  • change language at runtime

I'm open to suggestions and best practices on this topic.

edit: LocBaml is what I was talking about above, but looks like it's not avaliable for Silverlight


回答1:


I know this will get a number of traditional answers, but I would also like to put forward something completely original we tried (and succeeded) doing ourselves for more efficient localisation of Silverlight:

Localisation of Silverlight projects after completion

It basically uses attached properties to provide hooks, in the XAML, for dynamic changing of localisation strings. When they are first parsed your code gets a chance to make changes to the related elements, and a language swap just means reloading the current page.

The localisation strings come down from a server database using a simple RIA services call. The entries returned are simple key/value pairs for the chosen language.

This method also does not have the (quite large) overhead of the traditional binding methods you will see a lot of. We download only the chosen language strings when the app starts up. It also potentially allows edits/corrections to be sent back to the server (if you can bear the overhead of an editor in the app... we did this but it was a lot of work).

P.S. We were using PRISM, but it works as well in any Silverlight app as there are no dependencies (e.g. on Unity etc).

PPS. I will post some code snippets for this system when I get home tonight.

(Resx is so "last century")




回答2:


My current solution is using DataBinding on properties you want to localize (ie TextBox's Text, Button's Content, ToolTips, etc...) to a globally available string collection, in other words a Singleton. In my case, since I'm using MVVM Light, I have my LocalizedStringCollection exposed by the ViewModelLocator.

This collection is loaded from a XLIFF file (see https://en.wikipedia.org/wiki/Xliff) into a Dictionary<string, string> that is a member of my collection.

Here is the key parts of the collection to expose the strings.

/// <summary>
/// A Singleton bindeable collection of localized strings.
/// </summary>
public class LocalizedStringCollection : ObservableObject, INotifyCollectionChanged
{
    private Dictionary<string, string> _items;
    /// <summary>
    /// The content of the collection.
    /// </summary>
    public Dictionary<string, string> Items
    {
        get { return _items; }
        private set
        {
            _items = value;
            RaisePropertyChanged();
            if (CollectionChanged != null)
                CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }
    }

    /// <summary>
    /// Convenience accessor, most likely entry point.
    /// </summary>
    /// <param name="key">A localized string name</param>
    /// <returns>The localized version if found, an error string else (ErrorValue in DEBUG mode, the key in RELEASE)</returns>
    public string this[string key]
    {
        get
        {
            Contract.Requires(key != null);
            Contract.Ensures(Contract.Result<string>() != null);

            string value;
            if (Items.TryGetValue(key, out value))
                return value ?? String.Empty;

#if DEBUG
            return ErrorValue;
#else
            return key;
#endif
        }
    }
}

The XLIFF parsing is trivial using built-in XML support.

And here are XAML usage examples (can be more or less verbose depending on the syntax used):

<TextBlock Text="{Binding LocalizedStrings[login_label], Source={StaticResource Locator}}" />

<Button ToolTipService.ToolTip="{Binding LocalizedStrings[delete_content_button_tip], Source={StaticResource Locator}}" />

Hope this helps :)

I might make an article of this (with full source) if people are interested.



来源:https://stackoverflow.com/questions/10927854/silverlight-localization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!