سؤال

يمكنك يوصي وسيلة جيدة لتنفيذ متعدد اللغات نظام WPF التطبيق ؟ طريقة أستعمل الآن ينطوي XML, دروس و xaml التمديد.أنه يعمل بشكل جيد في معظم الحالات, ولكن عندما تضطر إلى التعامل مع العلامات الحيوية أو دينامية النص بشكل عام يتطلب بعض الجهد الإضافي.أود أن السماح مبرمج يعمل فقط في المشكلة الرئيسية و نسيت lang القضايا.

هل كانت مفيدة؟

المحلول

أنا باستخدام WPF التعريب امتداد.هو حقا سهلة طريقة تعريب أي نوع من DependencyProperty على DependencyObjects.

  • هو في دولة مستقرة
  • يدعم ملزمة مثل أسلوب الكتابة مثل Text = {LocText ResAssembly:ResFile:ResKey}
  • يعمل مع .resx تراجع آلية (مثلen-us -> ar -> ثقافة مستقلة)
  • يدعم الثقافة إجبار (مثلا ، "هذا يجب أن يكون باللغة الإنجليزية طوال الوقت")
  • يعمل مع العادي التبعية خصائص
  • يعمل مع التحكم في القوالب
  • يمكن استخدامها في XAML (حقا :P) دون أي مساحات إضافية
  • يمكن استخدامها في التعليمات البرمجية خلف لربط مترجمة القيم الديناميكية المتولدة الضوابط
  • تنفذ INotifyPropertyChanged للاستخدام المتقدم
  • تدعم سلسلة التنسيق مثلا "this is the '{0}' value"
  • يدعم البادئة واللاحقة القيم (حاليا مع LocText ملحق)
  • في النظم الإنتاجية (مثل العلاقات العامة المنتج)
  • التحول من لغة وقت التشغيل يؤثر على لا timeslice
  • يمكن استخدامها مع أي ملف الموارد (.resx) عبر جميع الجمعيات (أيضا دينامية تحميل واحد في وقت التشغيل)
  • لا تحتاج إلى أي عملية تهيئة (مثل "استدعاء xyz تسجيل خاص توطين القاموس")
  • متاح في وقت التصميم (MS مزيج التعبير, MS Visual Studio 2008 (العادي SP1)
  • تغيير اللغة المختارة من الممكن في وقت التصميم
  • يمكن حصر أي نوع من نوع البيانات ، طالما محول (TypeConverter) له (يمتد LocalizeExtension)
  • وقد بنيت في دعم Text, العلوي Text, أقل Text, Images ، Brushes ، Double و Thickness
  • لا يؤثر على أي تسرب الذاكرة
  • يترك UID الملكية يمسها
  • يقدم SpecificCulture استخدام IFormatProvider (مثلا ، (123.20).ToString(LocalizeDictionary.SpecificCulture) = "123.20" أو "123,20")
  • يقدم بعض وظائف للتحقق والحصول على الموارد القيم في التعليمات البرمجية خلف
  • لا يغير الثقافة على Thread.CurrentCulture أو Thread.CurrentUICulture (يمكن تغييرها بسهولة)

نصائح أخرى

اتبع الخطوات التالية:

1) كل مكان String شظايا في فصل ملف المورد.

على سبيل المثال: StringResources.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib">

    <!-- String resource that can be localized -->
    <system:String x:Key="All_Vehicles">All Vehicles</system:String>

</ResourceDictionary>

2) عمل نسخ لكل لغة وإضافتها (الكلمة) إلى دمج القواميس.لا تنس أن تضيف البلاد ISO رمز لجعل الأمور أسهل.

على سبيل المثال App.xaml:

<Application x:Class="WpfStringTables.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
    <Application.Resources>
        <ResourceDictionary >
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="StringResources.de-DE.xaml" />
                <ResourceDictionary Source="StringResources.nl-NL.xaml" />
                <ResourceDictionary Source="StringResources.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

آخر ملف الموارد مع سلاسل سيتم استخدامها لتحل محل أجزاء النص في القانون.

3a) استخدام النص أجزاء من String الجدول:

على سبيل المثال Window1.xaml:

<Window x:Class="WpfStringTables.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
    <Grid>
        <Button Margin="51,82,108,129" Name="AllVehiclesButton" 
                Content="{StaticResource All_Vehicles}"/>
    </Grid>
</Window>

3b) تحميل المورد من رمز (فقط استخدام هذا الرمز إذا كنت لا ترغب في تعيين طريق XAML):

void PageLoad()
{
  string str = FindResource("All_Vehicles").ToString();
}

4) التحول إلى ثقافة جديدة في بداية التطبيق:

Codesnippet من App.xaml.cs:

public static void SelectCulture(string culture)    
{      
    if (String.IsNullOrEmpty(culture))
        return;

    //Copy all MergedDictionarys into a auxiliar list.
    var dictionaryList = Application.Current.Resources.MergedDictionaries.ToList();

    //Search for the specified culture.     
    string requestedCulture = string.Format("StringResources.{0}.xaml", culture);
    var resourceDictionary = dictionaryList.
        FirstOrDefault(d => d.Source.OriginalString == requestedCulture);

    if (resourceDictionary == null)
    {
        //If not found, select our default language.             
        requestedCulture = "StringResources.xaml";
        resourceDictionary = dictionaryList.
            FirstOrDefault(d => d.Source.OriginalString == requestedCulture);
    }

    //If we have the requested resource, remove it from the list and place at the end.     
    //Then this language will be our string table to use.      
    if (resourceDictionary != null)
    {
        Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
        Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
    }

    //Inform the threads of the new culture.     
    Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);

}

وجوش سميث كتب تعليمي متعمقة حول الأسلوب المفضل له لذلك: إنشاء معالج تدويلها في WPF .

وقد توجيهك نحو اعادة تصميم كبير (انها حل MVVM )، ولكن باستخدام MVVM يبدو يستحق كل هذا العناء لأسباب أخرى أيضا.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top