Dreamine.UI.Wpf 1.0.1
Dreamine.UI.Wpf 사용자 인터페이스 기능과 구성 요소를 제공합니다.
로딩중...
검색중...
일치하는것 없음
DreamineLocalization.cs
이 파일의 문서화 페이지로 가기
1using System.Globalization;
2using System.Text.RegularExpressions;
3using System.Windows;
4using System.Windows.Controls;
5using System.Windows.Documents;
6
8
17public static class DreamineLocalization
18{
19 #region Attached Properties
20
29 public static readonly DependencyProperty LanguageProperty =
30 DependencyProperty.RegisterAttached(
31 "Language", typeof(Language), typeof(DreamineLocalization),
32 new FrameworkPropertyMetadata(default(Language),
33 FrameworkPropertyMetadataOptions.Inherits, OnLanguageChanged));
34
59 public static void SetLanguage(DependencyObject obj, Language value) => obj.SetValue(LanguageProperty, value);
84 public static Language GetLanguage(DependencyObject obj) => (Language)obj.GetValue(LanguageProperty);
85
94 public static readonly DependencyProperty LocalizationSectionProperty =
95 DependencyProperty.RegisterAttached(
96 "LocalizationSection", typeof(string), typeof(DreamineLocalization),
97 new FrameworkPropertyMetadata(default(string), FrameworkPropertyMetadataOptions.Inherits));
98
123 public static void SetLocalizationSection(DependencyObject obj, string value) => obj.SetValue(LocalizationSectionProperty, value);
148 public static string GetLocalizationSection(DependencyObject obj) => (string)obj.GetValue(LocalizationSectionProperty);
149
158 public static readonly DependencyProperty LocalizationKeyProperty =
159 DependencyProperty.RegisterAttached(
160 "LocalizationKey", typeof(string), typeof(DreamineLocalization),
161 new PropertyMetadata(null, OnLocalizationKeyChanged));
162
187 public static void SetLocalizationKey(DependencyObject obj, string value) => obj.SetValue(LocalizationKeyProperty, value);
212 public static string GetLocalizationKey(DependencyObject obj) => (string)obj.GetValue(LocalizationKeyProperty);
213
222 public static readonly DependencyProperty PlaceholderProperty =
223 DependencyProperty.RegisterAttached(
224 "Placeholder", typeof(string), typeof(DreamineLocalization),
225 new PropertyMetadata(null, OnPlaceHolderChanged));
226
251 public static void SetPlaceholder(DependencyObject obj, string value) => obj.SetValue(PlaceholderProperty, value);
276 public static string GetPlaceholder(DependencyObject obj) => (string)obj.GetValue(PlaceholderProperty);
277
286 public static readonly DependencyProperty TextCaseProperty =
287 DependencyProperty.RegisterAttached(
288 "TextCase", typeof(TextcaseType), typeof(DreamineLocalization),
289 new PropertyMetadata(TextcaseType.Default, OnTextCaseTypeChanged));
290
315 public static void SetTextCase(DependencyObject obj, TextcaseType value) => obj.SetValue(TextCaseProperty, value);
340 public static TextcaseType GetTextCase(DependencyObject obj) => (TextcaseType)obj.GetValue(TextCaseProperty);
341
342 #endregion
343
368 private static void OnLanguageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => ApplyLocalization(d);
393 private static void OnLocalizationKeyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => ApplyLocalization(d);
418 private static void OnPlaceHolderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => ApplyLocalization(d);
443 private static void OnTextCaseTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => ApplyLocalization(d);
444
461 private static void ApplyLocalization(DependencyObject d)
462 {
463 var key = GetLocalizationKey(d);
464 var lang = GetLanguage(d);
465 var placeholder = GetPlaceholder(d);
466 var textCase = GetTextCase(d);
467 var section = GetLocalizationSection(d);
468
469 if (string.IsNullOrWhiteSpace(key)) return;
470
471 string? localizedText = section != null
472 ? DreamineLocalizationManager.Get(lang, section, key)
473 : TryFindLocalization(lang, key);
474
475 if (string.IsNullOrWhiteSpace(localizedText)) localizedText = key;
476
477 localizedText = NormalizeNewlines(localizedText);
478
479 if (localizedText.Contains("{0}"))
480 localizedText = localizedText.Replace("{0}", placeholder);
481 else if (!string.IsNullOrEmpty(placeholder))
482 localizedText += $" {placeholder}";
483
484 localizedText = ApplyTextCase(localizedText, textCase);
485
486 bool multiLine = ContainsNewline(localizedText);
487
488 switch (d)
489 {
490 case Window w: w.Title = localizedText; break;
491 case MenuItem m: m.Header = BuildHeader(localizedText, multiLine); break;
492 case TreeViewItem tv: tv.Header = BuildHeader(localizedText, multiLine); break;
493 case GroupBox gb: gb.Header = BuildHeader(localizedText, multiLine); break;
494 case TabItem ti: ti.Header = BuildHeader(localizedText, multiLine); break;
495 case HeaderedContentControl hcc: hcc.Header = BuildHeader(localizedText, multiLine); break;
496 case HeaderedItemsControl hic: hic.Header = BuildHeader(localizedText, multiLine); break;
497 case ListViewItem li: li.Content = BuildContent(localizedText, multiLine); break;
498 case ComboBoxItem ci: ci.Content = BuildContent(localizedText, multiLine); break;
499 case Label lbl: lbl.Content = BuildContent(localizedText, multiLine); break;
500 case Button btn: btn.Content = BuildContent(localizedText, multiLine); break;
501 case CheckBox cb: cb.Content = BuildContent(localizedText, multiLine); break;
502 case RadioButton rb: rb.Content = BuildContent(localizedText, multiLine); break;
503 case ContentControl cc: cc.Content = BuildContent(localizedText, multiLine); break;
504 case TextBlock tb:
505 tb.Text = localizedText.Contains("null:vs") ? string.Empty : localizedText;
506 if (multiLine) tb.TextWrapping = TextWrapping.Wrap;
507 break;
508 case TextBox tbx:
509 tbx.Text = localizedText;
510 if (multiLine) { tbx.AcceptsReturn = true; tbx.TextWrapping = TextWrapping.Wrap; tbx.VerticalScrollBarVisibility = ScrollBarVisibility.Auto; }
511 break;
512 case Run run: run.Text = localizedText; break;
513 case DataGridTextColumn dtc: dtc.Header = BuildHeader(localizedText, multiLine); break;
514 case DataGridCheckBoxColumn dcc: dcc.Header = BuildHeader(localizedText, multiLine); break;
515 case DataGridComboBoxColumn dcb: dcb.Header = BuildHeader(localizedText, multiLine); break;
516 case DataGridTemplateColumn dtp: dtp.Header = BuildHeader(localizedText, multiLine); break;
517 case FrameworkElement fe:
518 fe.ToolTip = BuildTooltip(localizedText);
519 break;
520 }
521 }
522
563 public static string Get(string key, params string[] args)
564 {
566 return args.Length > 0 ? string.Format(text, args) : text;
567 }
568
601 private static string? TryFindLocalization(Language lang, string key)
602 {
603 if (DreamineLocalizationManager.Languages.TryGetValue(lang, out var sections))
604 foreach (var section in sections)
605 if (section.Value.TryGetValue(key, out var value)) return value;
606 return null;
607 }
608
641 private static string ApplyTextCase(string text, TextcaseType textCase) => textCase switch
642 {
643 TextcaseType.UpperCase => text.ToUpper(),
644 TextcaseType.LowerCase => text.ToLower(),
645 TextcaseType.TitleCase => string.Join(" ", text.Split(' ').Select(w =>
646 string.IsNullOrWhiteSpace(w) ? w :
647 char.ToUpper(w[0], CultureInfo.CurrentCulture) +
648 (w.Length > 1 ? w[1..].ToLower(CultureInfo.CurrentCulture) : ""))),
649 TextcaseType.SentenceCase => string.IsNullOrWhiteSpace(text) ? text :
650 char.ToUpper(text[0], CultureInfo.CurrentCulture) + text[1..].ToLower(CultureInfo.CurrentCulture),
651 TextcaseType.EmphasisCase => string.IsNullOrWhiteSpace(text) ? text :
652 text.IndexOf(' ') is int i && i >= 0
653 ? text[..i].ToUpperInvariant() + text[i..]
654 : text.ToUpperInvariant(),
655 _ => text
656 };
657
682 private static string NormalizeNewlines(string s)
683 {
684 if (string.IsNullOrEmpty(s)) return s;
685 s = Regex.Replace(s, @"<br\s*/?>", "\n", RegexOptions.IgnoreCase);
686 s = s.Replace(@"\r\n", "\n").Replace(@"\n", "\n").Replace("\r\n", "\n");
687 return s.Replace("\n", Environment.NewLine);
688 }
689
714 private static bool ContainsNewline(string s) => s?.Contains(Environment.NewLine) == true;
715
748 private static object BuildHeader(string text, bool multiLine) => multiLine
749 ? new TextBlock { Text = text, TextWrapping = TextWrapping.Wrap, TextTrimming = TextTrimming.CharacterEllipsis, MaxWidth = 300 }
750 : (object)new TextBlock { Text = text };
751
784 private static object BuildContent(string text, bool multiLine) => multiLine
785 ? new TextBlock { Text = text, TextWrapping = TextWrapping.Wrap }
786 : (object)new TextBlock { Text = text };
787
812 private static object BuildTooltip(string text)
813 => new TextBlock { Text = text, TextWrapping = TextWrapping.Wrap, MaxWidth = 420 };
814}
static TextcaseType GetTextCase(DependencyObject obj)
static void OnLocalizationKeyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
static object BuildHeader(string text, bool multiLine)
static string ApplyTextCase(string text, TextcaseType textCase)
static string GetLocalizationKey(DependencyObject obj)
static object BuildContent(string text, bool multiLine)
static readonly DependencyProperty LocalizationKeyProperty
static void OnLanguageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
static readonly DependencyProperty TextCaseProperty
static ? string TryFindLocalization(Language lang, string key)
static void OnPlaceHolderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
static void SetLocalizationKey(DependencyObject obj, string value)
static void SetTextCase(DependencyObject obj, TextcaseType value)
static string Get(string key, params string[] args)
static void OnTextCaseTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
static readonly DependencyProperty LanguageProperty
static readonly DependencyProperty PlaceholderProperty
static void SetLanguage(DependencyObject obj, Language value)
static Language GetLanguage(DependencyObject obj)
static void SetLocalizationSection(DependencyObject obj, string value)
static string GetLocalizationSection(DependencyObject obj)
static readonly DependencyProperty LocalizationSectionProperty
static void SetPlaceholder(DependencyObject obj, string value)
static string GetPlaceholder(DependencyObject obj)
static IReadOnlyDictionary< Language, IReadOnlyDictionary< string, IReadOnlyDictionary< string, string > > > Languages