添加要在多个项目之间共享的本地化资源

时间:2016-10-20 07:48:29

标签: c# xaml windows-phone-8.1

由于架构设计规范,我有一个应用程序从*.resw填充其视图。应用程序本身就像一种集成器。

现在我需要添加本地化资源,我可以通过添加TextBlocks文件成功实现它,但只有在应用程序项目内部声明控件时才能

我真正需要的是能够以某种方式在整体解决方案之间共享这些资源。

然后,重点是能够通过使用本地化资源来翻译任何控件的解决方案内容,最好使用上面解释的结构。

例如,我有以下视图,根据所选语言填充<ComboBox x:Name="Languages" ItemsSource="{Binding Languages}" SelectedItem="{Binding SelectedLanguage, Mode=TwoWay}"> <i:Interaction.Behaviors> <iCore:EventTriggerBehavior EventName="SelectionChanged"> <iCore:InvokeCommandAction Command="{Binding ChangeLanguage}" /> </iCore:EventTriggerBehavior> </i:Interaction.Behaviors> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding LanguageName}" /> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> <TextBlock Text="{Binding Model.HelloText}" FontSize="50" Foreground="Red"/> <TextBlock Text="{Binding Model.HowAreYouText}" FontSize="50" Foreground="Red"/> <BFview:BFView /> </StackPanel> '内容:

BFView

其中Model是存储在另一个项目中的视图(也有两个虚拟文本块)

该视图的public class MainModel : TranslatableStrings { private string helloText, howareuText; public string HelloText { get { return this.helloText; } set { SetProperty(ref this.helloText, value); } } public string HowAreYouText { get { return this.howareuText; } set { SetProperty(ref this.howareuText, value); } } }

Model

public abstract class TranslatableStrings : BindableBase { } 的基类只是一个契约类,因为它没有实现,只有一个基类型:

View

然后,public class MainViewModel : BaseViewModel { private ObservableCollection<MainViewListRscs> languages = new ObservableCollection<MainViewListRscs>(); private ICommand changeLang; private MainModel model = new MainModel(); public MainViewModel() { Languages = new ObservableCollection<MainViewListRscs>() { new MainViewListRscs { LanguageCode = "es-ES", LanguageName = "Español" }, new MainViewListRscs { LanguageCode = "en-EN", LanguageName = "English" }, new MainViewListRscs { LanguageCode = "fr-FR", LanguageName = "Français" }, new MainViewListRscs { LanguageCode = "de-DE", LanguageName = "Deutsch" } }; } public ICommand ChangeLanguage { get { return changeLang = changeLang ?? new DelegateCommand(OnChangeLanguageRequested); } } public ObservableCollection<MainViewListRscs> Languages { get { return this.languages; } set { this.languages = value; OnPropertyChanged(); } } public MainViewListRscs SelectedLanguage { get; set; } public MainModel Model { get { return this.model; } set { this.model = value; } } private void OnChangeLanguageRequested() { Logger.Debug("MAINVIEW", SelectedLanguage.LanguageName + " selected."); TranslateManager.UpdateStrings<TranslatableStrings>(SelectedLanguage.LanguageCode, this.Model); } public override Task OnNavigatedFrom(NavigationEventArgs args) { return null; } public override Task OnNavigatedTo(NavigationEventArgs args) { return null; } } 数据上下文如下:

TranslateManager

public class TranslateManager { public async static void UpdateStrings<T>(string langCode, T instance) where T : TranslatableStrings { //Get all the classes that implement TranslatableStrings var currentAssembly = instance.GetType().GetTypeInfo().Assembly; var translatableClasses = currentAssembly.DefinedTypes.Where(type => type.BaseType == typeof(T)).ToList(); //Open RESX file ResourceLoader resx = ResourceLoader.GetForCurrentView(langCode); foreach(var Class in translatableClasses) { foreach(var property in Class.DeclaredProperties) { string value = resx.GetString(property.Name); var vmProp = instance.GetType().GetTypeInfo().GetDeclaredProperty(property.Name); vmProp.SetValue(instance, value); } } } }

TextBlocks

我已经改变了MainView的两个[],但没有改变另一个项目中的视图。我需要做的是获得解决方案中包含的程序集列表。我想,由于我正在使用通用实现,因此获得这一切将使一切正常。

任何建议都将不胜感激。

谢谢!

1 个答案:

答案 0 :(得分:0)

Your translation files are loaded as resources. So you can access them anywhere, even in other projects by doing something like

private ResourceLoader _resourceLoader = new ResourceLoader();
var someTranslation =_resourceLoader.GetString("your_localization_key");

Wrap this code nicely into a lib so that you can have an easy access to it from everywhere, and there you go !