2016-09-18 76 views
1

我有一個多語言UWP應用程序,我需要本地化一些文本,包括格式樣式。如何本地化UWP應用程序(.NET)中的富文本?

例如,我可以有這樣的文字塊:

<textblock x:Uid="MyText"> 
<Bold>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer leo turpis, rhoncus ac libero maximus, lacinia eleifend sapien.</Bold><LineBreak/> <Italic> Aenean pulvinar eros <Underline>sit amet sodales eleifend.</Underline></Italic> <LineBreak/><Bold>Aliquam feugiat sit amet risus ac venenatis.<LineBreak/> Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.<Bold><LineBreak/><Hyperlink NavigateUri="https://stackoverflow.com"> Fusce fermentum eleifend leo non pulvinar.</Hyperlink> 
</textblock> 

要本地化我創建了一個「MyText.Text」記錄的文本塊是resw文件,但問題是,我不能把XAML中resw。

有一種方法或本地化.NET/UWP富文本模式?

+0

[全球化和本地化](https://msdn.microsoft.com/en-us/windows/ UWP /全球化/全球化門戶)。 – IInspectable

+0

@IInspectable,我已經閱讀文檔,我本地化的所有字符串,但我無法本地化格式的文本。 –

+0

我不明白,爲什麼格式化會有所作爲。這個過程是不是一樣的(除非你不翻譯格式化指令)? – IInspectable

回答

0

本地化格式化XAML使用UID自動是不可能的,但動態XAML可以通過從.resw文件C#代碼被加載。

例如,在resw我有鑰匙MY_FORMATTED_XAML該值:

<Span Name="SPAN_NAME" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> 
     <Run>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque sodales augue sit amet eros cursus, vitae tincidunt velit dictum. Vivamus in dolor sapien. Nunc ac dictum nulla.</Run> 
     <LineBreak/> 
     <Run Text="------------"/> 
     <LineBreak/> 
     <Run> 
     Ut mollis massa ut tortor commodo cursus. Etiam at nisl eu lectus ornare iaculis. Sed consequat dignissim sapien, a tincidunt dolor rutrum at. Suspendisse accumsan eget ante ac aliquet. Nulla non dictum nisi. Nam ultrices libero ut orci pretium, finibus mollis mauris varius. Duis suscipit sapien cursus sagittis blandit. Donec hendrerit varius facilisis.               
     </Run> 
     <LineBreak/> 
     <LineBreak/> 
     <Run> 
      <Bold>This is a sample for stackoverflow</Bold> 
     </Run>  
     <LineBreak/>  
     <Run>  
      Nunc mollis porttitor arcu ac mattis. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed lacinia mattis enim, a tincidunt nisi. Quisque accumsan, sapien quis fringilla pharetra, purus metus maximus velit, ac aliquam enim justo et ante. Aliquam malesuada at velit vel consequat.     
     </Run>&#160; 
<Italic>License:</Italic> 
     <Hyperlink NavigateUri="http://unlicense.org">Unlicense 2016.</Hyperlink> 
     <LineBreak/> 

    </Span> 

在C#中我有一個輔助類加載資源字符串:

public static class ResourceRetriever 
{ 
    private static ResourceMap _resourceMap = null; 
    public static ResourceMap ResourceMap 
    { 
     get 
     { 
      if (_resourceMap == null) 
      { 
       _resourceMap= Windows.ApplicationModel.Resources.Core.ResourceManager.Current.MainResourceMap;      
      } 
      return _resourceMap; 
     } 
    } 

    public static string GetString(string key) 
    { 
     return ResourceMap?.GetValue("Resources/" + key, new ResourceContext())?.ValueAsString; 
    } 

    public static string GetString(string key, ResourceContext context) 
    { 
     return ResourceMap?.GetValue("Resources/"+key, context)?.ValueAsString; 
    } 
} 

我加載與動態XAML此代碼:

String dynamicXaml = ResourceRetriever.GetString("MY_FORMATTED_XAML"); 
Span span = XamlReader.Load(dynamicXaml) as Span; 
this.MyTextBlock.Inlines.Add(span);