diff options
author | Karsten Heimrich <karsten.heimrich@qt.io> | 2022-01-14 16:22:15 +0100 |
---|---|---|
committer | Karsten Heimrich <karsten.heimrich@qt.io> | 2022-01-20 15:06:37 +0000 |
commit | 64e6c7b0d576b2e86ca3d41c36f42d2a2bf8ba44 (patch) | |
tree | 796b73e84b1a59b585b2155278d6c69977ec7618 /QtVsTools.Wizards/ItemWizard/Translation | |
parent | d84945f51a512fc9a503c84fbbd7604d70884f2c (diff) |
Implement templated Qt translation (.ts) file wizard
Change-Id: I0efb2b66c5d7a9e9607b9334bea0212ed8d9cd16
Reviewed-by: Miguel Costa <miguel.costa@qt.io>
Diffstat (limited to 'QtVsTools.Wizards/ItemWizard/Translation')
3 files changed, 354 insertions, 0 deletions
diff --git a/QtVsTools.Wizards/ItemWizard/Translation/TranslationPage.xaml b/QtVsTools.Wizards/ItemWizard/Translation/TranslationPage.xaml new file mode 100644 index 00000000..b87402d9 --- /dev/null +++ b/QtVsTools.Wizards/ItemWizard/Translation/TranslationPage.xaml @@ -0,0 +1,163 @@ +๏ปฟ<!-- + ***************************************************************************** + ** + ** Copyright (C) 2022 The Qt Company Ltd. + ** Contact: https://www.qt.io/licensing/ + ** + ** This file is part of the Qt VS Tools. + ** + ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ + ** Commercial License Usage + ** Licensees holding valid commercial Qt licenses may use this file in + ** accordance with the commercial license agreement provided with the + ** Software or, alternatively, in accordance with the terms contained in + ** a written agreement between you and The Qt Company. For licensing terms + ** and conditions see https://www.qt.io/terms-conditions. For further + ** information use the contact form at https://www.qt.io/contact-us. + ** + ** GNU General Public License Usage + ** Alternatively, this file may be used under the terms of the GNU + ** General Public License version 3 as published by the Free Software + ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT + ** included in the packaging of this file. Please review the following + ** information to ensure the GNU General Public License requirements will + ** be met: https://www.gnu.org/licenses/gpl-3.0.html. + ** + ** $QT_END_LICENSE$ + ** + ***************************************************************************** +--> + +<common:WizardPage x:Class="QtVsTools.Wizards.ItemWizard.TranslationPage" + xmlns:common="clr-namespace:QtVsTools.Wizards.Common" + xmlns:util="clr-namespace:QtVsTools.Wizards.Util" + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + xmlns:d="http://schemas.microsoft.com/expression/blend/2008" + xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" + xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + KeepAlive="True" + mc:Ignorable="d" + d:DesignHeight="445" + d:DesignWidth="585" + FocusManager.FocusedElement="{Binding ElementName=LanguageListBox}"> + <common:WizardPage.Resources> + <BooleanToVisibilityConverter x:Key="b2v" /> + </common:WizardPage.Resources> + <Grid> + <Grid.ColumnDefinitions> + <ColumnDefinition Width="100" /> + <ColumnDefinition Width="*" /> + </Grid.ColumnDefinitions> + + <Image Grid.Column="0" + HorizontalAlignment="Center" + Source="/QtVsTools.Wizards;component/Resources/Qt-logo-small.png" + VerticalAlignment="Top" + Margin="0,25,0,0" + RenderTransformOrigin="1,0"> + <Image.RenderTransform> + <TransformGroup> + <ScaleTransform ScaleY="0.86" + ScaleX="0.86" /> + </TransformGroup> + </Image.RenderTransform> + </Image> + + <Grid Grid.Column="1" + Margin="25,25,25,0"> + <Grid.RowDefinitions> + <RowDefinition Height="Auto" /> + <RowDefinition Height="*" /> + <RowDefinition Height="Auto" /> + </Grid.RowDefinitions> + <TextBlock TextWrapping="Wrap" + Grid.Row="0"> + <Run FontWeight="Bold" + Text="{Binding Path=Header}" /> + <LineBreak /> + <LineBreak /> + <Run Text="{Binding Path=Message}" /> + <LineBreak /> + </TextBlock> + + <Grid Grid.Row="1" + Margin="0,20,0,20"> + <Grid.RowDefinitions> + <RowDefinition Height="Auto" /> + <RowDefinition Height="Auto" /> + <RowDefinition Height="*" /> + <RowDefinition Height="Auto" /> + <RowDefinition Height="Auto" /> + </Grid.RowDefinitions> + <TextBlock Grid.Row="0" + Margin="0,0,0,5" + Text="Select a Language:" /> + <Grid Grid.Row="1" + MinHeight="22" + Background="White"> + <TextBlock Text=" Filter..." + Foreground="LightSteelBlue" + VerticalAlignment="Center" + Visibility="{Binding ElementName=searchBox, + Path=Text.IsEmpty, + Converter={StaticResource b2v}}" /> + <TextBox Name="searchBox" + Background="Transparent" + TextChanged="OnSearchBoxTextChanged" + VerticalContentAlignment="Center" /> + </Grid> + <ListBox Grid.Row="2" + Margin="0,10,0,8" + Name="LanguageListBox" + DisplayMemberPath="Value" + SelectedValuePath="Key" + ItemsSource="{Binding Path=Data.CultureInfos}" + SelectedValue="{Binding Path=Data.CultureInfoName}" + SelectionChanged="OnLanguageBoxSelectionChanged" /> + <TextBlock Grid.Row="3" + Text="Save as:"/> + <Grid Grid.Row="4"> + <Grid.ColumnDefinitions> + <ColumnDefinition Width="*" /> + <ColumnDefinition Width="Auto" /> + </Grid.ColumnDefinitions> + <TextBox Text="{Binding Path=Data.TsFile}"/> + <TextBlock Grid.Column="1" + Margin="5,0,0,0"> + <Run FontWeight="Bold" + Text="{Binding Path=Data.CultureInfoName}" /> + <Run Text=".ts" /> + </TextBlock> + </Grid> + </Grid> + + <StackPanel Grid.Row="2" + HorizontalAlignment="Right" + Orientation="Horizontal" + Margin="0,0,0,10"> + <Button Click="OnPreviousButtonClick" + Name="PreviousButton" + IsEnabled="{Binding Path=PreviousButtonEnabled}" + MinWidth="75">< _Previous</Button> + <Button MinWidth="75" + Name="NextButton" + Click="OnNextButtonClick" + IsEnabled="{Binding Path=NextButtonEnabled}" + Margin="10,0,0,0">_Next ></Button> + <Button MinWidth="75" + Click="OnFinishButtonClick" + Margin="10,0,0,0" + IsDefault="True" + IsEnabled="{Binding Path=FinishButtonEnabled}" + Name="FinishButton" + VerticalAlignment="Bottom">_Finish</Button> + <Button Click="OnCancelButtonClick" + MinWidth="75" + Margin="10,0,0,0" + Name="CancelButton" + IsEnabled="{Binding Path=CancelButtonEnabled}" + IsCancel="True">_Cancel</Button> + </StackPanel> + </Grid> + </Grid> +</common:WizardPage> diff --git a/QtVsTools.Wizards/ItemWizard/Translation/TranslationPage.xaml.cs b/QtVsTools.Wizards/ItemWizard/Translation/TranslationPage.xaml.cs new file mode 100644 index 00000000..aeb82cef --- /dev/null +++ b/QtVsTools.Wizards/ItemWizard/Translation/TranslationPage.xaml.cs @@ -0,0 +1,76 @@ +๏ปฟ/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt VS Tools. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +using System; +using System.Collections.Generic; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using QtVsTools.Wizards.Common; + +namespace QtVsTools.Wizards.ItemWizard +{ + public partial class TranslationPage : WizardPage + { + public TranslationPage() + { + InitializeComponent(); + DataContext = this; + Loaded += OnTranslationPageLoaded; + } + + private void OnTranslationPageLoaded(object sender, RoutedEventArgs e) + { + var view = CollectionViewSource.GetDefaultView(LanguageListBox.ItemsSource); + view.Filter = obj => + { + if (string.IsNullOrEmpty(searchBox.Text)) + return true; + + var item = (KeyValuePair<string, string>)obj; + return item.Value.IndexOf(searchBox.Text, StringComparison.OrdinalIgnoreCase) >= 0; + }; + LanguageListBox.SelectedIndex = 0; + } + + private void OnSearchBoxTextChanged(object sender, TextChangedEventArgs e) + { + CollectionViewSource.GetDefaultView(LanguageListBox.ItemsSource).Refresh(); + if (LanguageListBox.Items.Count == 1 || LanguageListBox.SelectedItem == null) + LanguageListBox.SelectedIndex = 0; + } + + private void OnLanguageBoxSelectionChanged(object sender, SelectionChangedEventArgs e) + { + if (e.RemovedItems != null && (e.AddedItems == null || e.AddedItems.Count == 0)) { + if (LanguageListBox.Items.Count != 0) + LanguageListBox.SelectedIndex = 0; + } + } + } +} diff --git a/QtVsTools.Wizards/ItemWizard/Translation/TranslationWizard.cs b/QtVsTools.Wizards/ItemWizard/Translation/TranslationWizard.cs new file mode 100644 index 00000000..32a44e9b --- /dev/null +++ b/QtVsTools.Wizards/ItemWizard/Translation/TranslationWizard.cs @@ -0,0 +1,115 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt VS Tools. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using EnvDTE; +using QtVsTools.Common; +using QtVsTools.Core; +using QtVsTools.Wizards.Common; +using QtVsTools.Wizards.ProjectWizard; + +namespace QtVsTools.Wizards.ItemWizard +{ + using static EnumExt; + + public sealed class TsWizardData : WizardData + { + public string TsFile { get; set; } + public string CultureInfoName { get; set; } + public List<KeyValuePair<string, string>> CultureInfos { get; set; } + } + + public sealed class TranslationWizard : ProjectTemplateWizard + { + protected override Options TemplateType => Options.ConsoleSystem | Options.GUISystem; + + enum NewTranslationItem + { + [String("safeitemname")] SafeItemName, + [String("tsfilename")] TsFileName, + [String("cultureinfoname")] CultureInfoName + } + + WizardData _WizardData; + protected override WizardData WizardData => _WizardData + ?? (_WizardData = new TsWizardData + { + DefaultModules = new List<string> { "QtCore"} + }); + + WizardWindow _WizardWindow; + protected override WizardWindow WizardWindow => _WizardWindow + ?? (_WizardWindow = new WizardWindow(title: "Qt Translation File Wizard") + { + new TranslationPage + { + Data = WizardData, + Header = @"Welcome to the Qt Translation File Wizard", + Message = @"This wizard will add a new Qt empty translation file to your " + + @"project. The wizard creates a .ts for the selected language.", + PreviousButtonEnabled = false, + NextButtonEnabled = false, + FinishButtonEnabled = true, + CancelButtonEnabled = true + }, + }); + + protected override void BeforeWizardRun() + { + var tmp = WizardData as TsWizardData; + tmp.TsFile = Parameter[NewTranslationItem.SafeItemName]; + tmp.CultureInfos = CultureInfo.GetCultures(CultureTypes.AllCultures) + .ToDictionary( + mc => mc.Name.Replace("-", "_"), + mc => mc.EnglishName, + StringComparer.OrdinalIgnoreCase + ).OrderBy(item => item.Value).ToList(); + } + + protected override void BeforeTemplateExpansion() + { + var tmp = WizardData as TsWizardData; + Parameter[NewTranslationItem.CultureInfoName] = tmp.CultureInfoName; + Parameter[NewTranslationItem.TsFileName] = tmp.TsFile + "_" + tmp.CultureInfoName + ".ts"; + } + + protected override void Expand() + { + // do not call the base class method here + } + + public override void ProjectItemFinishedGenerating(ProjectItem projectItem) + { + Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread(); + QtProject.AdjustWhitespace(Dte, projectItem.Properties.Item("FullPath").Value.ToString()); + } + } +} |