WPF의 다문 [폐쇄
-
03-07-2019 - |
문제
WPF 앱 용 다중 언어 시스템을 구현하는 좋은 방법을 추천 할 수 있습니까? 내가 지금 사용하고있는 방법에는 XML, 클래스 및 XAML 확장 기능이 포함됩니다. 대부분의 경우 잘 작동하지만 동적 레이블이나 동적 텍스트를 일반적으로 처리해야 할 때는 약간의 노력이 필요합니다. 나는 프로그래머가 주요 문제에서만 일하고 랭 문제를 잊어 버리고 싶습니다.
해결책
나는 그것을 사용하고있다 WPF 현지화 확장. 모든 유형의 현지화를하는 정말 쉬운 방법입니다. DependencyProperty
~에 DependencyObject
에스.
- 진정한 안정적인 상태에 있습니다
- 바인딩 같은 작문 스타일과 같은 지원을 지원합니다
Text = {LocText ResAssembly:ResFile:ResKey}
- .resx -fallback 메커니즘 (예 : en -us-> en-> 독립 문화)과 함께 작동합니다.
- 문화 강제력을 지원합니다 (예 : "이것은 항상 영어 여야합니다")
- 정상 의존성 속성과 함께 작동합니다
- 제어 템플릿과 함께 작동합니다
- 추가 네임 스페이스없이 XAML (Really : P)에서 사용할 수 있습니다.
- 국소화 된 값을 동적 생성 컨트롤에 바인딩하기 위해 코드에서 사용될 수 있습니다.
- 구현
INotifyPropertyChanged
고급 사용을 위해 - 문자열 서식을 지원합니다
"this is the '{0}' value"
- 접두사 및 접미사 값을 지원합니다 (현재
LocText
확대) - 생산 시스템에서 사용 중입니다 (내 홍보 제품과 같은)
- 언어를 런타임으로 전환하면 영향을 미칩니다 아니 타임 슬라이스
- 모든 리소스 파일과 함께 사용할 수 있습니다 (
.resx
) 모든 어셈블리에서 (런타임에 동적로드 된 동적로드) - 초기화 프로세스가 필요하지 않습니다 ( "특별 현지화 사전을 등록하려면 XYZ에 전화하십시오")
- 설계 시간에 제공됩니다 (MS Expression Blend, MS Visual Studio 2008 (정상 및 SP1)
- 선택한 언어의 변화는 설계 시간에 가능합니다
- 변환기로서 모든 유형의 데이터 유형을 현지화 할 수 있습니다 (
TypeConverter
) 존재하기 때문에 (확장LocalizeExtension
) - 지원을 구축했습니다
Text
, 상단Text
, 낮추다Text
,Image
에스,Brush
ES,Double
그리고Thickness
- 메모리 누출에 영향을 미치지 않습니다
- 떠난다
UID
손대지 않은 속성 - a
SpecificCulture
AS를 사용합니다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);
}
Josh Smith는 자신이 선호하는 방법에 대한 심층적 인 튜토리얼을 작성했습니다. WPF에서 국제화 된 마법사 만들기.
그것은 당신이 큰 재 설계를 향한 것을 지적 할 수 있습니다 (그것은 MVVM 솔루션), 그러나 MVVM을 사용하는 것도 다른 이유로 그만한 가치가있는 것 같습니다.
이 기사를 사용하여 자원 파일을 사용하여 다국어 WPF Windows를 처리했습니다.http://www.codeproject.com/kb/wpf/wpf_resx_localization.aspx정말 간단하고 효과적이기 때문에 점검해야합니다.