我通过披露不必本地化 WPF 应用程序来回答这个问题。
以下是一些可能有帮助的选项:
看看WPF 本地化扩展。
如果您必须使用该Translate()
方法,您可以创建一个转换器。有几种方法可以转换值,通过直接绑定或通过绕过 Binding 表达式的参数。
XAML
<Window x:Class="so.Localization.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Localization="clr-namespace:so.Localization" Title="Tranlate"
Width="525"
Height="350"
WindowStartupLocation="CenterScreen">
<Window.Resources>
<Localization:TranslateConverter x:Key="Translater" />
</Window.Resources>
<Grid>
<Menu>
<MenuItem Header="{Binding Converter={StaticResource Translater}, ConverterParameter='Copier', FallbackValue='Copier'}" />
</Menu>
</Grid>
</Window>
代码背后
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
namespace so.Localization
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
[ValueConversion( typeof( string ), typeof( string ) )]
public class TranslateConverter : IValueConverter
{
#region Implementation of IValueConverter
public object Convert( object value, Type targetType, object parameter, CultureInfo culture )
{
// if using binding
if( value != null && value is string )
{
return ( (string)value ).TranslateBinding();
}
// if using a general paramater
if( parameter != null && parameter is string )
{
return ( (string)parameter ).TranslateParameter();
}
return DependencyProperty.UnsetValue;
}
public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture )
{
throw new NotImplementedException();
}
#endregion
}
public static class Ext
{
public static string TranslateBinding( this string input )
{
// translat to whatever...
return input + " (translated binding)";
}
public static string TranslateParameter( this string input )
{
// translat to whatever...
return input + " (translated parameter)";
}
}
}