示例#1
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();
    }
示例#2
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();
    }