private void Localization_OnLoaded(object sender, RoutedEventArgs e)
        {
            foreach (var resourceDictionary in Application.Current.Resources.MergedDictionaries)
            {
                var imageItem = new ImageListBoxItem
                {
                    Tag     = resourceDictionary.Source?.OriginalString ?? "Settings",
                    Content = resourceDictionary.Source?.OriginalString ?? "Settings"
                };

                if (resourceDictionary.Source == null)
                {
                    imageItem.IsEnabled = false;
                    imageItem.Image     = FindResource("Vector.No") as Canvas;
                    imageItem.Author    = "This is a settings dictionary.";
                }
                else if (resourceDictionary.Source.OriginalString.Contains("StringResources"))
                {
                    imageItem.Image = FindResource("Vector.Translate") as Canvas;

                    #region Name

                    var subs   = resourceDictionary.Source.OriginalString.Substring(resourceDictionary.Source.OriginalString.IndexOf("StringResources"));
                    var pieces = subs.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);

                    if (pieces.Length == 3)
                    {
                        imageItem.Author = "Recognized as " + pieces[1];
                    }
                    else
                    {
                        imageItem.Author = "Not recognized";
                    }

                    #endregion
                }
                else
                {
                    imageItem.IsEnabled = false;
                    imageItem.Image     = FindResource("Vector.No") as Canvas;
                    imageItem.Author    = "This is a style dictionary.";
                }

                ResourceListBox.Items.Add(imageItem);
            }

            ResourceListBox.SelectedIndex = ResourceListBox.Items.Count - 1;
            ResourceListBox.ScrollIntoView(ResourceListBox.SelectedItem);

            CommandManager.InvalidateRequerySuggested();
        }
Example #2
0
        private async void Add_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var ofd = new OpenFileDialog
            {
                AddExtension    = true,
                CheckFileExists = true,
                Title           = "Open a Resource Dictionary",
                Filter          = "Resource Dictionay (*.xaml)|*.xaml;"
            };

            var result = ofd.ShowDialog();

            if (!result.HasValue || !result.Value)
            {
                return;
            }

            StatusBand.Info("Validating resource name...");

            #region Validation

            if (!ofd.FileName.Contains("StringResources"))
            {
                Dialog.Ok("Action Denied", "The name of file does not follow a valid pattern.",
                          "Try renaming like (without the []): StringResources.[Language Code].xaml");

                StatusBand.Hide();
                return;
            }

            var subs = ofd.FileName.Substring(ofd.FileName.IndexOf("StringResources"));

            if (Application.Current.Resources.MergedDictionaries.Any(x => x.Source != null && x.Source.OriginalString.Contains(subs)))
            {
                Dialog.Ok("Action Denied", "You can't add a resource with the same name.",
                          "Try renaming like: StringResources.[Language Code].xaml");

                StatusBand.Hide();
                return;
            }

            var pieces = subs.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);

            if (pieces.Length != 3)
            {
                Dialog.Ok("Action Denied", "Filename with wrong format.",
                          "Try renaming like: StringResources.[Language Code].xaml");

                StatusBand.Hide();
                return;
            }
            var cultureName = pieces[1];

            string properCulture;
            try
            {
                properCulture = await Task.Factory.StartNew(() => CheckSupportedCulture(cultureName));
            }
            catch (CultureNotFoundException)
            {
                Dialog.Ok("Action Denied", "Unknown Language.",
                          $"The \"{cultureName}\" and its family were not recognized as a valid language codes.");

                StatusBand.Hide();
                return;
            }
            catch (Exception ex)
            {
                Dialog.Ok("Action Denied", "Error checking culture.", ex.Message);

                StatusBand.Hide();
                return;
            }

            if (properCulture != cultureName)
            {
                Dialog.Ok("Action Denied", "Redundant Language Code.",
                          $"The \"{cultureName}\" code is redundant. Try using \'{properCulture}\" instead");

                StatusBand.Hide();
                return;
            }

            #endregion

            StatusBand.Info("Importing resource...");

            var fileName = ofd.FileName;

            try
            {
                await Task.Factory.StartNew(() => LocalizationHelper.ImportStringResource(fileName));
            }
            catch (Exception ex)
            {
                Dialog.Ok("Localization", "Localization - Importing Xaml Resource", ex.Message);

                StatusBand.Hide();
                await Task.Factory.StartNew(() => GC.Collect());

                return;
            }

            var resourceDictionary = Application.Current.Resources.MergedDictionaries.LastOrDefault();

            var imageItem = new ImageListBoxItem
            {
                Tag     = resourceDictionary?.Source.OriginalString ?? "Unknown",
                Content = resourceDictionary?.Source.OriginalString ?? "Unknown",
                Image   = FindResource("Vector.Translate") as Canvas,
                Author  = "Recognized as " + pieces[1]
            };

            StatusBand.Hide();

            ResourceListBox.Items.Add(imageItem);
            ResourceListBox.ScrollIntoView(imageItem);

            CommandManager.InvalidateRequerySuggested();
        }
        private void Add_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var ofd = new OpenFileDialog
            {
                AddExtension    = true,
                CheckFileExists = true,
                Title           = "Open a Resource Dictionary",
                Filter          = "Resource Dictionay (*.xaml)|*.xaml;"
            };

            var result = ofd.ShowDialog();

            if (!result.HasValue || !result.Value)
            {
                return;
            }

            #region Validation

            if (!ofd.FileName.Contains("StringResources"))
            {
                Dialog.Ok("Action Denied", "The name of file does not follow a valid pattern.",
                          "Try renaming like (without the []): StringResources.[Language Code].xaml");

                return;
            }

            var subs = ofd.FileName.Substring(ofd.FileName.IndexOf("StringResources"));

            if (Application.Current.Resources.MergedDictionaries.Any(x => x.Source != null && x.Source.OriginalString.Contains(subs)))
            {
                Dialog.Ok("Action Denied", "You can't add a resource with the same name.",
                          "Try renaming like: StringResources.[Language Code].xaml");

                return;
            }

            var pieces = subs.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);

            if (pieces.Length != 3)
            {
                Dialog.Ok("Action Denied", "Filename with wrong format.",
                          "Try renaming like: StringResources.[Language Code].xaml");

                return;
            }

            var culture = new CultureInfo(pieces[1]);

            if (culture.EnglishName.Contains("Unknown"))
            {
                Dialog.Ok("Action Denied", "Unknown Language.", $"The \"{pieces[1]}\" was not recognized as a valid language code.");

                return;
            }

            #endregion

            if (!LocalizationHelper.ImportStringResource(ofd.FileName))
            {
                return;
            }

            var resourceDictionary = Application.Current.Resources.MergedDictionaries.LastOrDefault();

            var imageItem = new ImageListBoxItem
            {
                Tag     = resourceDictionary?.Source.OriginalString ?? "Unknown",
                Content = resourceDictionary?.Source.OriginalString ?? "Unknown",
                Image   = FindResource("Vector.Translate") as Canvas,
                Author  = "Recognized as " + pieces[1]
            };

            ResourceListBox.Items.Add(imageItem);
            ResourceListBox.ScrollIntoView(imageItem);

            CommandManager.InvalidateRequerySuggested();
        }
Example #4
0
    private async void Localization_Loaded(object sender, RoutedEventArgs e)
    {
        AddButton.IsEnabled    = false;
        SaveButton.IsEnabled   = false;
        RemoveButton.IsEnabled = false;
        DownButton.IsEnabled   = false;
        UpButton.IsEnabled     = false;
        OkButton.IsEnabled     = false;

        var actualIndex = 0;

        foreach (var resourceDictionary in Application.Current.Resources.MergedDictionaries)
        {
            //If it's not a localization resource, ignore it.
            if (resourceDictionary.Source?.OriginalString.Contains("StringResources") != true)
            {
                actualIndex++;
                continue;
            }

            var imageItem = new ExtendedListBoxItem
            {
                Content             = resourceDictionary.Source.OriginalString,
                Icon                = FindResource("Vector.Translate") as Brush,
                Index               = actualIndex++,
                ShowMarkOnSelection = false
            };

            #region Language code

            var pieces = resourceDictionary.Source.OriginalString.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);

            if (pieces.Length == 3 || pieces.Length == 4)
            {
                imageItem.Author = LocalizationHelper.GetWithFormat("S.Localization.Recognized", "Recognized as {0}", pieces[1]);
            }
            else
            {
                imageItem.Author = LocalizationHelper.Get("S.Localization.NotRecognized");
            }

            #endregion

            ResourceListBox.Items.Add(imageItem);
        }

        //Selects the last item on the list.
        ResourceListBox.SelectedItem = ResourceListBox.Items.Cast <ExtendedListBoxItem>().LastOrDefault(w => w.IsEnabled);

        if (ResourceListBox.SelectedItem != null)
        {
            ResourceListBox.ScrollIntoView(ResourceListBox.SelectedItem);
        }

        StatusBand.Info(LocalizationHelper.Get("S.Localization.GettingCodes"));

        _cultures = await GetProperCulturesAsync();

        AddButton.IsEnabled    = true;
        SaveButton.IsEnabled   = true;
        RemoveButton.IsEnabled = true;
        DownButton.IsEnabled   = true;
        UpButton.IsEnabled     = true;
        OkButton.IsEnabled     = true;

        StatusBand.Hide();
        SizeToContent = SizeToContent.Width;
        MaxHeight     = double.PositiveInfinity;

        CommandManager.InvalidateRequerySuggested();
    }
Example #5
0
    private async void Add_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        var ofd = new OpenFileDialog
        {
            AddExtension    = true,
            CheckFileExists = true,
            Title           = LocalizationHelper.Get("S.Localization.OpenResource"),
            Filter          = LocalizationHelper.Get("S.Localization.File.Resource") + " (*.xaml)|*.xaml;"
        };

        var result = ofd.ShowDialog();

        if (!result.HasValue || !result.Value)
        {
            return;
        }

        #region Validations

        var position = ofd.FileName.IndexOf("StringResources", StringComparison.InvariantCulture);
        var subs     = position > -1 ? ofd.FileName.Substring(position) : "";
        var pieces   = subs.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);

        //Wrong filename format.
        if (position < 0 || pieces.Length != 3)
        {
            Dialog.Ok(Title, LocalizationHelper.Get("S.Localization.Warning.Name"), LocalizationHelper.Get("S.Localization.Warning.Name.Info"));
            StatusBand.Hide();
            return;
        }

        //Repeated language code.
        if (Application.Current.Resources.MergedDictionaries.Any(x => x.Source != null && x.Source.OriginalString.Contains(subs)))
        {
            Dialog.Ok(Title, LocalizationHelper.Get("S.Localization.Warning.Repeated"), LocalizationHelper.Get("S.Localization.Warning.Repeated.Info"));
            StatusBand.Hide();
            return;
        }

        try
        {
            var properCulture = await Task.Factory.StartNew(() => CheckSupportedCulture(pieces[1]));

            if (properCulture != pieces[1])
            {
                Dialog.Ok(Title, LocalizationHelper.Get("S.Localization.Warning.Redundant"), LocalizationHelper.GetWithFormat("S.Localization.Warning.Redundant.Info",
                                                                                                                              "The \"{0}\" code is redundant. Try using \"{1}\" instead.", pieces[1], properCulture));
                StatusBand.Hide();
                return;
            }
        }
        catch (CultureNotFoundException cn)
        {
            LogWriter.Log(cn, "Impossible to validade the resource name, culture not found");
            Dialog.Ok(Title, LocalizationHelper.Get("S.Localization.Warning.Unknown"), LocalizationHelper.GetWithFormat("S.Localization.Warning.Unknown.Info",
                                                                                                                        "The \"{0}\" and its family were not recognized as valid language codes.", pieces[1]));
            StatusBand.Hide();
            return;
        }
        catch (Exception ex)
        {
            LogWriter.Log(ex, "Impossible to validade the resource name");
            Dialog.Ok(Title, LocalizationHelper.Get("S.Localization.Warning.NotPossible"), ex.Message);
            StatusBand.Hide();
            return;
        }

        #endregion

        StatusBand.Info(LocalizationHelper.Get("S.Localization.Importing"));

        try
        {
            var fileName = ofd.FileName;

            await Task.Factory.StartNew(() => LocalizationHelper.ImportStringResource(fileName));
        }
        catch (Exception ex)
        {
            LogWriter.Log(ex, "Impossible to import the resource");
            Dialog.Ok(Title, LocalizationHelper.Get("S.Localization.Warning.NotPossible"), ex.Message);
            StatusBand.Hide();
            return;
        }

        var resourceDictionary = Application.Current.Resources.MergedDictionaries.LastOrDefault();

        var imageItem = new ExtendedListBoxItem
        {
            Content             = resourceDictionary?.Source.OriginalString ?? "...",
            Icon                = FindResource("Vector.Translate") as Brush,
            Author              = LocalizationHelper.GetWithFormat("S.Localization.Recognized", "Recognized as {0}", pieces[1]),
            Index               = Application.Current.Resources.MergedDictionaries.Count - 1,
            ShowMarkOnSelection = false
        };

        StatusBand.Hide();

        ResourceListBox.Items.Add(imageItem);
        ResourceListBox.ScrollIntoView(imageItem);

        UpdateIndexes();

        CommandManager.InvalidateRequerySuggested();
    }
        private async void Localization_OnLoaded(object sender, RoutedEventArgs e)
        {
            StatusBand.Info("Getting resources...");

            AddButton.IsEnabled    = false;
            SaveButton.IsEnabled   = false;
            RemoveButton.IsEnabled = false;
            DownButton.IsEnabled   = false;
            UpButton.IsEnabled     = false;
            OkButton.IsEnabled     = false;

            foreach (var resourceDictionary in Application.Current.Resources.MergedDictionaries)
            {
                var imageItem = new ImageListBoxItem
                {
                    Tag     = resourceDictionary.Source?.OriginalString ?? "Settings",
                    Content = resourceDictionary.Source?.OriginalString ?? "Settings"
                };

                if (resourceDictionary.Source == null)
                {
                    imageItem.IsEnabled = false;
                    imageItem.Image     = FindResource("Vector.No") as Canvas;
                    imageItem.Author    = "This is a settings dictionary.";
                }
                else if (resourceDictionary.Source.OriginalString.Contains("StringResources"))
                {
                    imageItem.Image = FindResource("Vector.Translate") as Canvas;

                    #region Name

                    //var subs = resourceDictionary.Source.OriginalString.Substring(resourceDictionary.Source.OriginalString.IndexOf("StringResources"));
                    var pieces = resourceDictionary.Source.OriginalString.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);

                    if (pieces.Length == 3 || pieces.Length == 4)
                    {
                        imageItem.Author = "Recognized as " + pieces[1];
                    }
                    else
                    {
                        imageItem.Author = "Not recognized";
                    }

                    #endregion
                }
                else
                {
                    imageItem.IsEnabled = false;
                    imageItem.Image     = FindResource("Vector.No") as Canvas;
                    imageItem.Author    = "This is a style dictionary.";
                }

                ResourceListBox.Items.Add(imageItem);
            }

            ResourceListBox.SelectedIndex = ResourceListBox.Items.Count - 1;
            ResourceListBox.ScrollIntoView(ResourceListBox.SelectedItem);

            SaveButton.IsEnabled   = true;
            RemoveButton.IsEnabled = true;
            DownButton.IsEnabled   = true;
            UpButton.IsEnabled     = true;
            OkButton.IsEnabled     = true;

            StatusBand.Info("Getting language codes...");

            _cultures = await GetProperCulturesAsync();

            StatusBand.Hide();
            AddButton.IsEnabled = true;
            SizeToContent       = SizeToContent.Width;
            MaxHeight           = double.PositiveInfinity;

            CommandManager.InvalidateRequerySuggested();
        }