Skip to content

Commit

Permalink
Merge pull request #22 from AndrewKeepCoding/feat/add-support-for-enums
Browse files Browse the repository at this point in the history
Feat/add support for enums
  • Loading branch information
AndrewKeepCoding authored Aug 15, 2023
2 parents 0ec66f7 + adca408 commit 79e7236
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions WinUI3Localizer/Localizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal Localizer(Options options)
this.localizationActions = LocalizationActions.DefaultActions;
}

Uids.DependencyObjectUidSet += Uids_DependencyObjectUidSet; ;
Uids.DependencyObjectUidSet += Uids_DependencyObjectUidSet;
}

public event EventHandler<LanguageChangedEventArgs>? LanguageChanged;
Expand Down Expand Up @@ -257,14 +257,6 @@ private static void DependencyObjectsReferences_DependencyObjectRemoved(object?
return null;
}

private void LocalizeDependencyObjectsWithoutDependencyProperty(DependencyObject dependencyObject, string value)
{
foreach (LocalizationActions.ActionItem item in this.localizationActions
.Where(x => x.TargetType == dependencyObject.GetType()))
{
item.Action(new LocalizationActions.ActionArguments(dependencyObject, value));
}
}
private async Task LocalizeDependencyObjects()
{
foreach (DependencyObject dependencyObject in await this.dependencyObjectsReferences.GetDependencyObjects())
Expand All @@ -280,20 +272,49 @@ private void LocalizeDependencyObject(DependencyObject dependencyObject)
{
foreach (LanguageDictionary.Item item in items)
{
if (GetDependencyProperty(
dependencyObject,
item.DependencyPropertyName) is DependencyProperty dependencyProperty)
{
dependencyObject.SetValue(dependencyProperty, item.Value);
}
else
{
LocalizeDependencyObjectsWithoutDependencyProperty(dependencyObject, item.Value);
}
LocalizeDependencyObject(dependencyObject, item);
}
}
}

private void LocalizeDependencyObject(DependencyObject dependencyObject, LanguageDictionary.Item item)
{
if (GetDependencyProperty(
dependencyObject,
item.DependencyPropertyName) is DependencyProperty dependencyProperty)
{
LocalizeDependencyObjectsWithDependencyProperty(dependencyObject, dependencyProperty, item.Value);
}
else
{
LocalizeDependencyObjectsWithoutDependencyProperty(dependencyObject, item.Value);
}
}

private void LocalizeDependencyObjectsWithDependencyProperty(DependencyObject dependencyObject, DependencyProperty dependencyProperty, string value)
{
if (dependencyObject
.GetValue(dependencyProperty)?
.GetType() is Type propertyType &&
propertyType.IsEnum is true &&
Enum.TryParse(propertyType, value, out object? enumValue) is true)
{
dependencyObject.SetValue(dependencyProperty, enumValue);
return;
}

dependencyObject.SetValue(dependencyProperty, value);
}

private void LocalizeDependencyObjectsWithoutDependencyProperty(DependencyObject dependencyObject, string value)
{
foreach (LocalizationActions.ActionItem item in this.localizationActions
.Where(x => x.TargetType == dependencyObject.GetType()))
{
item.Action(new LocalizationActions.ActionArguments(dependencyObject, value));
}
}

private void OnLanguageChanged(string previousLanguage, string currentLanguage)
{
LanguageChanged?.Invoke(this, new LanguageChangedEventArgs(previousLanguage, currentLanguage));
Expand Down

0 comments on commit 79e7236

Please sign in to comment.