Windows 8 學習筆記(二十二)–全球化資源

來源:互聯網
上載者:User

在項目需求中,我們經常需要實現多語言應用,專業術語就是資源全球化。在WinRT庫中,在命名空間Windows.Globalization下也提供了相應的全球化資源的方法。
下面咱就來一步步實現一個簡單的全球化:
1、準備好我們的資源,在這之前,有個” BCP-47”這個名字需要瞭解,我們資源語言套件的名稱需要按照BCP-47規則來命名。如中文包的名稱為:zh-CN,英文包的名稱為:en-US.
我們在工程中建立一個strings檔案夾,在該檔案夾下再建立zh-CN和en-US檔案夾,在兩個檔案夾下分別建立一個resource.resw資源檔
2 、資源檔與控制項顯示之間是通過控制項的uid關聯的,建立一個空白頁面ResourcePage.xaml

<StackPanel Orientation="Horizontal"  HorizontalAlignment="Center" VerticalAlignment="Center">
           <ComboBox Margin="10, 10, 0, 13" Grid.Column="0"  SelectedValuePath="Tag" Name="langComboBox"  HorizontalAlignment="Left" VerticalAlignment="Top" Width="106" Height="34" SelectionChanged="langComboBox_SelectionChanged">             
                <ComboBoxItem Tag="en-US">English</ComboBoxItem>
                <ComboBoxItem Tag="zh-CN">Chinese</ComboBoxItem>
            </ComboBox>
            <TextBlock x:Uid="tbWelcome" x:Name="tbWelcome" HorizontalAlignment="Left" Margin="20,0,0,0" TextWrapping="Wrap" FontSize="30" Foreground="Red"  VerticalAlignment="Top" Height="49" Width="287"/>
        </StackPanel>

3、添加資源內容:

中文:

英文也類似的添加

 

4、工程檔案中的Package.appxmanifest中”應用程式UI”可以設定預設的語言。為了測試,我們添加了一個ComboBox,進行兩種語言的切換

private void langComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string language=Convert.ToString(langComboBox.SelectedValue);
            if (Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride != language)
            {
                Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = language;
                ResourceManager.Current.DefaultContext.Reset();

                ResourceLoader loader = new ResourceLoader();
                tbWelcome.Text = loader.GetString("tbWelcome/Text");
            }
        }

以上就實現了一個簡單的控制項顯示的全球化,當然全球化的用法不光光是語言顯示還,當不同語言下控制項顯示的寬度可以不同,如芬蘭語就比英語的字元長,所以我們也可以將控制項的寬度作一個全不過化,例如上面的控制項就可以在資源檔中添加tbWelcome.Width相應的值。
我們注意到,在切換不同語言時我們主要分為兩步,將應用程式的慣用語言設定為我們當前選擇的語言;然後利用ResourceLoader載入相應的資源套件,通過該資源套件去載入不同控制項的資源。

對於ResourceLoader,我們也可以手動指定對應的資源我,ResourceLoader  resource=new ResourceLoader(.resw檔案名稱)即可。

說到資源載入,還有一種用法不得不提,這種用法是讓應用自動去匹配最適合的資源,如我們的資源檔可能很多,這時可以通過以下用法讓應用程式去篩選:

ResourceContext context = new ResourceContext();
            if (context.QualifierValues.ContainsKey("language"))
            {
                context.QualifierValues["language"] = language;
            }
            else
            {
                context.QualifierValues.Add("language", language);
            }

            ResourceMap resourceStringMap = ResourceManager.Current.MainResourceMap.GetSubtree("Resources");
            ResourceCandidate resourceCandidate = resourceStringMap.GetValue("tbWelcome/Text", context);
            if (resourceCandidate!=null)
            {
                tbWelcome.Text = resourceCandidate.ValueAsString;
            }

ResourceContext表示當前應用的上下關聯的資源,通過該資源中對應的語言去擷取我們需要的資源值。

 

 還有一種切換資源的用法,該用法需要先指定PrimaryLanguageOverride的值為當前我們的語言,用法如下:

string language=Convert.ToString(langComboBox.SelectedValue);
            if (Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride != language)
            {
                Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = language;
                ResourceManager.Current.DefaultContext.Reset();
                tbWelcome.Text = ResourceManager.Current.MainResourceMap.GetValue("Resources/tbWelcome/Text").ValueAsString;
        }

當然,全球化資源這塊還有日期和時間、貨幣的一些全球化,因為每個國家的顯示形式也是不一樣的,這些相對簡單,只要參照API中給的一些模板格式化就ok啦,微軟樣本都有提供哦~

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.