added possibility to edit language properties and to create new language files. also some cosmetics, e.g. icons

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@1859 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
JKlingen 2012-05-17 09:54:51 +00:00
parent 608ab69082
commit 3e371d5d03
9 changed files with 770 additions and 466 deletions

View file

@ -1,7 +1,7 @@
<Application x:Class="GreenshotLanguageEditor.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
StartupUri="EntriesEditorWindow.xaml">
<Application.Resources>
</Application.Resources>

View file

@ -1,151 +1,229 @@
<?xml version="1.0" encoding="utf-8"?>
<Window
x:Class="GreenshotLanguageEditor.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="800"
Height="600"
Title="Greenshot Language Editor"
Icon="icons\icon.ico">
<Grid
VerticalAlignment="Stretch"
Height="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition
Width="250" />
<ColumnDefinition
Width="50*" />
<ColumnDefinition
Width="50*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition
Height="Auto" />
<RowDefinition
Height="*" />
</Grid.RowDefinitions>
<StackPanel
Grid.Row="0"
Grid.Column="1"
Orientation="Horizontal">
<ComboBox
Grid.Column="0"
Grid.Row="0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="8,8,0,0"
Height="20"
Name="language1ComboBox"
SelectionChanged="languageComboBoxSelectionChanged"
Tag="1"
Width="150"
ItemsSource="{Binding LanguageFiles}"
DisplayMemberPath="FileName"
SelectedItem="{Binding LanguageFile1, Mode=OneWay}"></ComboBox>
<Button
Content="Save"
Click="saveButtonClicked"
Tag="1"></Button>
<Button
Content="Reset"
Click="cancelButtonClicked"
Tag="1" />
</StackPanel>
<StackPanel
Grid.Row="0"
Grid.Column="2"
Orientation="Horizontal">
<ComboBox
Grid.Column="0"
Grid.Row="0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="8,8,0,0"
Height="20"
Name="language2ComboBox"
SelectionChanged="languageComboBoxSelectionChanged"
Tag="2"
Width="150"
ItemsSource="{Binding LanguageFiles}"
DisplayMemberPath="FileName"
SelectedItem="{Binding LanguageFile2, Mode=OneWay}"></ComboBox>
<Button
Content="Save"
Click="saveButtonClicked"
Tag="2" />
<Button
Content="Reset"
Click="cancelButtonClicked"
Tag="2" />
</StackPanel>
<DataGrid
Grid.Row="1"
Grid.Column="0"
Grid.ColumnSpan="3"
Name="LanguageGrid"
ItemsSource="{Binding View}"
AutoGenerateColumns="False"
IsReadOnly="False"
IsSynchronizedWithCurrentItem="True"
CellEditEnding="cellEdited">
<DataGrid.RowStyle>
<Style
TargetType="DataGridRow">
<Style.Setters>
<Setter
Property="Background"
Value="{Binding Path=Background}"></Setter>
</Style.Setters>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTextColumn
Header="Key"
Binding="{Binding Key}"
Width="250" />
<DataGridTextColumn
Header="Language1"
Binding="{Binding Entry1}"
Width="*">
<DataGridTextColumn.EditingElementStyle>
<Style
TargetType="TextBox">
<Setter
Property="ScrollViewer.HorizontalScrollBarVisibility"
Value="Auto" />
<Setter
Property="ScrollViewer.VerticalScrollBarVisibility"
Value="Auto" />
<Setter
Property="MaxHeight"
Value="300" />
<Setter
Property="AcceptsReturn"
Value="true" />
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
<DataGridTextColumn
Header="Language2"
Binding="{Binding Entry2}"
Width="*">
<DataGridTextColumn.EditingElementStyle>
<Style
TargetType="TextBox">
<Setter
Property="ScrollViewer.HorizontalScrollBarVisibility"
Value="Auto" />
<Setter
Property="ScrollViewer.VerticalScrollBarVisibility"
Value="Auto" />
<Setter
Property="MaxHeight"
Value="300" />
<Setter
Property="AcceptsReturn"
Value="true" />
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
<?xml version="1.0" encoding="utf-8"?>
<Window
x:Class="GreenshotLanguageEditor.EntriesEditorWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="850"
Height="600"
Title="Greenshot Language Editor"
Icon="icons\icon.ico">
<Grid
VerticalAlignment="Stretch"
Height="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition
Width="250" />
<ColumnDefinition
Width="50*" />
<ColumnDefinition
Width="50*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition
Height="Auto" />
<RowDefinition
Height="*" />
</Grid.RowDefinitions>
<StackPanel
Grid.Row="0"
Grid.Column="1"
Orientation="Horizontal">
<ComboBox
Grid.Column="0"
Grid.Row="0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Name="language1ComboBox"
SelectionChanged="languageComboBoxSelectionChanged"
Tag="1"
Width="150"
ItemsSource="{Binding LanguageFiles, Mode=OneWay}"
DisplayMemberPath="FileName"
SelectedItem="{Binding LanguageFile1, Mode=OneWay}"
Margin="5,5,5,5"
Height="26"></ComboBox>
<Button
Click="metaButtonClicked"
Tag="1"
ToolTip="Edit language properties"
Width="26"
Height="26"
Margin="0,5,5,5">
<Image
Source="icons\property.png"
Width="16"
Height="16" />
</Button>
<Button
Click="saveButtonClicked"
Tag="1"
ToolTip="Save to file"
Width="26"
Height="26"
Margin="0,5,5,5">
<Image
Width="16"
Height="16"
Source="icons\disk-black.png" />
</Button>
<Button
Click="cancelButtonClicked"
Tag="1"
ToolTip="Reset (reload from file)"
Width="26"
Height="26"
Margin="0,5,5,5">
<Image
Source="icons\cross.png"
Width="16"
Height="16" />
</Button>
<Button
Click="newButtonClicked"
Tag="1"
ToolTip="Create new file"
Width="26"
Height="26"
Margin="0,5,5,5">
<Image
Source="icons\new.png"
Width="16"
Height="16" />
</Button>
</StackPanel>
<StackPanel
Grid.Row="0"
Grid.Column="2"
Orientation="Horizontal">
<ComboBox
Grid.Column="0"
Grid.Row="0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Name="language2ComboBox"
SelectionChanged="languageComboBoxSelectionChanged"
Tag="2"
Width="150"
ItemsSource="{Binding LanguageFiles, Mode=OneWay}"
DisplayMemberPath="FileName"
SelectedItem="{Binding LanguageFile2, Mode=OneWay}"
Margin="5,5,5,5"
Height="26"></ComboBox>
<Button
Click="metaButtonClicked"
Tag="2"
ToolTip="Edit language properties"
Width="26"
Height="26"
Margin="0,5,5,5">
<Image
Source="icons\property.png"
Width="16"
Height="16" />
</Button>
<Button
Click="saveButtonClicked"
Tag="2"
Width="26"
Height="26"
Margin="0,5,5,5">
<Image
Source="icons\disk-black.png"
Width="16"
Height="16" />
</Button>
<Button
Click="cancelButtonClicked"
Tag="2"
Width="26"
Height="26"
Margin="0,5,5,5">
<Image
Source="icons\cross.png"
Width="16"
Height="16" />
</Button>
<Button
Click="newButtonClicked"
Tag="2"
ToolTip="Create new file"
Width="26"
Height="26"
Margin="0,5,5,5">
<Image
Source="icons\new.png"
Width="16"
Height="16" />
</Button>
</StackPanel>
<DataGrid
Grid.Row="1"
Grid.Column="0"
Grid.ColumnSpan="3"
Name="LanguageGrid"
ItemsSource="{Binding View}"
AutoGenerateColumns="False"
IsReadOnly="False"
IsSynchronizedWithCurrentItem="True"
CellEditEnding="cellEdited">
<DataGrid.RowStyle>
<Style
TargetType="DataGridRow">
<Style.Setters>
<Setter
Property="Background"
Value="{Binding Path=Background}"></Setter>
</Style.Setters>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTextColumn
Header="Key"
Binding="{Binding Key}"
Width="250" />
<DataGridTextColumn
Header="Language1"
Binding="{Binding Entry1}"
Width="*">
<DataGridTextColumn.EditingElementStyle>
<Style
TargetType="TextBox">
<Setter
Property="ScrollViewer.HorizontalScrollBarVisibility"
Value="Auto" />
<Setter
Property="ScrollViewer.VerticalScrollBarVisibility"
Value="Auto" />
<Setter
Property="MaxHeight"
Value="300" />
<Setter
Property="AcceptsReturn"
Value="true" />
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
<DataGridTextColumn
Header="Language2"
Binding="{Binding Entry2}"
Width="*">
<DataGridTextColumn.EditingElementStyle>
<Style
TargetType="TextBox">
<Setter
Property="ScrollViewer.HorizontalScrollBarVisibility"
Value="Auto" />
<Setter
Property="ScrollViewer.VerticalScrollBarVisibility"
Value="Auto" />
<Setter
Property="MaxHeight"
Value="300" />
<Setter
Property="AcceptsReturn"
Value="true" />
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>

View file

@ -1,309 +1,347 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2012 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.ComponentModel;
using System.Xml;
namespace GreenshotLanguageEditor {
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window {
IDictionary<string, LanguageEntry> languageResources = new SortedDictionary<string, LanguageEntry>();
public IList<LanguageFile> LanguageFiles {
get;
set;
}
// maybe refactor this encapsulating column related info
bool unsavedChangesInLanguage1 = false;
bool unsavedChangesInLanguage2 = false;
public LanguageFile LanguageFile1 {
get;
set;
}
public LanguageFile LanguageFile2 {
get;
set;
}
public ICollectionView View {
get;
set;
}
// TODO user should be able to create a new translation file using this app
// TODO possibility to edit language file meta data, such as version, languagegroup, description, etc.
public Window1() {
// TODO remember last selected location
/*var dialog = new System.Windows.Forms.FolderBrowserDialog();
dialog.ShowNewFolderButton = false;
System.Windows.Forms.DialogResult result = dialog.ShowDialog();
string languagePath;
if (result == System.Windows.Forms.DialogResult.OK) {
languagePath = dialog.SelectedPath;
} else {
this.Close();
return;
}*/
string languagePath = @"C:\Users\jens\Documents\Sharpdevelop Projects\Greenshot\trunk\Greenshot\Languages\";
InitializeComponent();
DataContext = this;
this.Activate();
View = CollectionViewSource.GetDefaultView(LoadResources(languagePath));
}
private IList<LanguageEntry> LoadResources(string languagePath) {
LanguageFiles = new List<LanguageFile>();
foreach (LanguageFile languageFile in GreenshotLanguage.GetLanguageFiles(languagePath, "language*.xml")) {
LanguageFiles.Add(languageFile);
if ("en-US".Equals(languageFile.IETF)) {
// we should always start with en-US, so the grid is initialized with the probably most-complete language file as benchmark for translations
LanguageFile1 = languageFile;
PopulateColumn(languageFile, 1);
} else if(LanguageFile2 == null) {
LanguageFile2 = languageFile;
PopulateColumn(languageFile, 2);
}
}
if(LanguageFile1 == null) {
MessageBox.Show("language-en-US.xml does not exist in the location selected. It is needed as reference for the translation.");
this.Close();
}
return new List<LanguageEntry>(languageResources.Values);
}
private void PopulateColumn(LanguageFile languageFile, int columnIndex) {
ClearColumn(columnIndex);
IDictionary<string, string> resources = GreenshotLanguage.ReadLanguageFile(languageFile);
foreach(string key in resources.Keys) {
LanguageEntry entry = GetOrCreateLanguageEntry(key);
if(columnIndex == 1) entry.Entry1 = resources[key];
else if (columnIndex == 2) entry.Entry2 = resources[key];
else throw new ArgumentOutOfRangeException("Argument columnIndex must be either 1 or 2");
}
if(columnIndex == 1) unsavedChangesInLanguage1 = false;
if(columnIndex == 2) unsavedChangesInLanguage2 = false;
}
private void ClearColumn(int columnIndex) {
// we do not throw out LanguageEntries that do not exist in selected language,
// so that en-US (loaded at startup) is always the benchmark, even when other languages are displayed
foreach(LanguageEntry e in languageResources.Values) {
if (columnIndex == 1) e.Entry1 = null;
else if (columnIndex == 2) e.Entry2 = null;
else throw new ArgumentOutOfRangeException("Argument columnIndex must be either 1 or 2");
}
}
private LanguageEntry GetOrCreateLanguageEntry(string key) {
LanguageEntry entry;
if (languageResources.ContainsKey(key)) {
entry = languageResources[key];
} else {
entry = new LanguageEntry();
entry.Key = key;
languageResources.Add(key, entry);
}
return entry;
}
private void saveButtonClicked(object sender, RoutedEventArgs e) {
int targetColumn = GetTargetColumn((Control)sender);
LanguageFile editedFile = (LanguageFile) (targetColumn == 1 ? language1ComboBox.SelectedItem : language2ComboBox.SelectedItem);
var dialog = new System.Windows.Forms.SaveFileDialog();
dialog.AutoUpgradeEnabled = true;
dialog.DefaultExt = ".xml";
dialog.InitialDirectory = Path.GetDirectoryName(editedFile.FilePath);
dialog.FileName = editedFile.FileName;
dialog.CheckFileExists = true;
System.Windows.Forms.DialogResult result = dialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK) {
CreateXML(dialog.FileName, targetColumn);
}
}
private void languageComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e) {
int targetColumn = GetTargetColumn((Control)sender);
LanguageFile file = (LanguageFile)((ComboBox)sender).SelectedItem;
if((targetColumn == 1 && file.Equals(LanguageFile1)) || (targetColumn == 2 && file.Equals(LanguageFile2))) {
// nothing changed
return;
}
if((targetColumn == 1 && unsavedChangesInLanguage1) || (targetColumn == 2 && unsavedChangesInLanguage2)) {
MessageBoxResult res = MessageBox.Show("Do you really want to switch language? Unsaved changes will be lost.", "Confirm language switch", MessageBoxButton.OKCancel, MessageBoxImage.Warning,MessageBoxResult.Cancel,MessageBoxOptions.None);
if(res != MessageBoxResult.OK) {
// cancelled by user
((ComboBox)sender).SelectedItem = (targetColumn == 1) ? LanguageFile1 : LanguageFile2;
return;
}
}
if(targetColumn == 1) LanguageFile1 = file;
else if(targetColumn == 2) LanguageFile2 = file;
PopulateColumn(file, targetColumn);
}
private void cancelButtonClicked(object sender, RoutedEventArgs e) {
int targetColumn = GetTargetColumn((Control)sender);
if((targetColumn == 1 && unsavedChangesInLanguage1) || (targetColumn == 2 && unsavedChangesInLanguage2)) {
MessageBoxResult res = MessageBox.Show("Do you really want to reset this column? Unsaved changes will be lost.", "Confirm language reset", MessageBoxButton.OKCancel, MessageBoxImage.Warning,MessageBoxResult.Cancel,MessageBoxOptions.None);
if(res == MessageBoxResult.OK) {
LanguageFile file = (LanguageFile)(targetColumn == 1 ? language1ComboBox.SelectedItem : language2ComboBox.SelectedItem);
PopulateColumn(file, targetColumn);
}
}
}
private int GetTargetColumn(Control control) {
object tag = control.Tag;
if(tag == null && !tag.Equals("1") && !tag.Equals("2")) {
throw new ApplicationException("Please use the control's Tag property to indicate the column to interact with (1 or 2).");
} else {
return tag.Equals("1") ? 1 : 2;
}
}
private void cellEdited(object sender, DataGridCellEditEndingEventArgs e) {
if(e.Column.DisplayIndex == 1) unsavedChangesInLanguage1 = true;
else if(e.Column.DisplayIndex == 2) unsavedChangesInLanguage2 = true;
}
public void CreateXML(string savePath, int targetColumn) {
ICollectionView view = (ICollectionView)LanguageGrid.ItemsSource;
IList<LanguageEntry> entries = (IList<LanguageEntry>)view.SourceCollection;
using (XmlTextWriter xmlWriter = new XmlTextWriter(savePath, Encoding.UTF8)) {
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.Indentation = 1;
xmlWriter.IndentChar = '\t';
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("language");
xmlWriter.WriteAttributeString("description", "testdescription");
xmlWriter.WriteAttributeString("ietf", "testietf");
xmlWriter.WriteAttributeString("version", "testversion");
xmlWriter.WriteAttributeString("languagegroup", "testlanguagegroup");
xmlWriter.WriteStartElement("resources");
foreach(LanguageEntry entry in entries) {
xmlWriter.WriteStartElement("resource");
xmlWriter.WriteAttributeString("name", entry.Key);
if(targetColumn == 1) xmlWriter.WriteString(entry.Entry1);
else if(targetColumn == 2 ) xmlWriter.WriteString(entry.Entry2);
else throw new ArgumentOutOfRangeException("Argument columnIndex must be either 1 or 2");
xmlWriter.WriteEndElement();
}
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
}
}
}
public class LanguageEntry : IEditableObject, INotifyPropertyChanged {
private string key;
private string entry1;
private string entry2;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info) {
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public Brush Background {
get {
if (String.IsNullOrEmpty(Entry1)) {
return Brushes.Red;
}
if (String.IsNullOrEmpty(Entry2)) {
return Brushes.Red;
}
return Brushes.White;
}
}
public string Key {
get {
return key;
}
set {
key = value;
NotifyPropertyChanged("Key");
}
}
public string Entry1 {
get {
return entry1;
}
set {
entry1 = value;
NotifyPropertyChanged("Entry1");
NotifyPropertyChanged("Background");
}
}
public string Entry2 {
get {
return entry2;
}
set {
entry2 = value;
NotifyPropertyChanged("Entry2");
NotifyPropertyChanged("Background");
}
}
public void BeginEdit() {
}
public void EndEdit() {
}
public void CancelEdit() {
}
}
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2012 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Xml;
namespace GreenshotLanguageEditor {
/// <summary>
/// Interaction logic for EntriesEditorWindow.xaml
/// </summary>
public partial class EntriesEditorWindow : Window, INotifyPropertyChanged {
private string languagePath;
IDictionary<string, LanguageEntry> languageResources = new SortedDictionary<string, LanguageEntry>();
IList<LanguageFile> languageFiles;
public IList<LanguageFile> LanguageFiles {
get {return languageFiles;}
set {languageFiles = value; NotifyPropertyChanged("languageFiles");}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info) {
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
// maybe refactor this encapsulating column related info
bool unsavedChangesInLanguage1 = false;
bool unsavedChangesInLanguage2 = false;
public LanguageFile LanguageFile1 {
get;
set;
}
public LanguageFile LanguageFile2 {
get;
set;
}
public ICollectionView View {
get;
set;
}
public EntriesEditorWindow() {
/*var dialog = new System.Windows.Forms.FolderBrowserDialog();
dialog.ShowNewFolderButton = false;
System.Windows.Forms.DialogResult result = dialog.ShowDialog();
string languagePath;
if (result == System.Windows.Forms.DialogResult.OK) {
languagePath = dialog.SelectedPath;
} else {
this.Close();
return;
}*/
languagePath = @"C:\Users\jens\Documents\Sharpdevelop Projects\Greenshot\trunk\Greenshot\Languages\";
InitializeComponent();
DataContext = this;
this.Activate();
View = CollectionViewSource.GetDefaultView(LoadResources(languagePath));
}
private IList<LanguageEntry> LoadResources(string languagePath) {
LanguageFiles = new BindingList<LanguageFile>();
foreach (LanguageFile languageFile in GreenshotLanguage.GetLanguageFiles(languagePath, "language*.xml")) {
LanguageFiles.Add(languageFile);
if ("en-US".Equals(languageFile.IETF)) {
// we should always start with en-US, so the grid is initialized with the probably most-complete language file as benchmark for translations
LanguageFile1 = languageFile;
PopulateColumn(languageFile, 1);
} else if(LanguageFile2 == null) {
LanguageFile2 = languageFile;
PopulateColumn(languageFile, 2);
}
}
if(LanguageFile1 == null) {
MessageBox.Show("language-en-US.xml does not exist in the location selected. It is needed as reference for the translation.");
this.Close();
}
return new List<LanguageEntry>(languageResources.Values);
}
private void PopulateColumn(LanguageFile languageFile, int columnIndex) {
ClearColumn(columnIndex);
IDictionary<string, string> resources = GreenshotLanguage.ReadLanguageFile(languageFile);
foreach(string key in resources.Keys) {
LanguageEntry entry = GetOrCreateLanguageEntry(key);
if(columnIndex == 1) entry.Entry1 = resources[key];
else if (columnIndex == 2) entry.Entry2 = resources[key];
else throw new ArgumentOutOfRangeException("Argument columnIndex must be either 1 or 2");
}
if(columnIndex == 1) unsavedChangesInLanguage1 = false;
if(columnIndex == 2) unsavedChangesInLanguage2 = false;
}
private void ClearColumn(int columnIndex) {
// we do not throw out LanguageEntries that do not exist in selected language,
// so that en-US (loaded at startup) is always the benchmark, even when other languages are displayed
foreach(LanguageEntry e in languageResources.Values) {
if (columnIndex == 1) e.Entry1 = null;
else if (columnIndex == 2) e.Entry2 = null;
else throw new ArgumentOutOfRangeException("Argument columnIndex must be either 1 or 2");
}
}
private LanguageEntry GetOrCreateLanguageEntry(string key) {
LanguageEntry entry;
if (languageResources.ContainsKey(key)) {
entry = languageResources[key];
} else {
entry = new LanguageEntry();
entry.Key = key;
languageResources.Add(key, entry);
}
return entry;
}
private void saveButtonClicked(object sender, RoutedEventArgs e) {
int targetColumn = GetTargetColumn((Control)sender);
LanguageFile editedFile = (LanguageFile) (targetColumn == 1 ? language1ComboBox.SelectedItem : language2ComboBox.SelectedItem);
CreateXML(editedFile.FilePath, targetColumn);
}
private void languageComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e) {
int targetColumn = GetTargetColumn((Control)sender);
LanguageFile file = (LanguageFile)((ComboBox)sender).SelectedItem;
if((targetColumn == 1 && file.Equals(LanguageFile1)) || (targetColumn == 2 && file.Equals(LanguageFile2))) {
// nothing changed
return;
}
if((targetColumn == 1 && unsavedChangesInLanguage1) || (targetColumn == 2 && unsavedChangesInLanguage2)) {
MessageBoxResult res = MessageBox.Show("Do you really want to switch language? Unsaved changes will be lost.", "Confirm language switch", MessageBoxButton.OKCancel, MessageBoxImage.Warning,MessageBoxResult.Cancel,MessageBoxOptions.None);
if(res != MessageBoxResult.OK) {
// cancelled by user
((ComboBox)sender).SelectedItem = (targetColumn == 1) ? LanguageFile1 : LanguageFile2;
return;
}
}
if(targetColumn == 1) LanguageFile1 = file;
else if(targetColumn == 2) LanguageFile2 = file;
PopulateColumn(file, targetColumn);
}
private void cancelButtonClicked(object sender, RoutedEventArgs e) {
int targetColumn = GetTargetColumn((Control)sender);
if((targetColumn == 1 && unsavedChangesInLanguage1) || (targetColumn == 2 && unsavedChangesInLanguage2)) {
MessageBoxResult res = MessageBox.Show("Do you really want to reset this column? Unsaved changes will be lost.", "Confirm language reset", MessageBoxButton.OKCancel, MessageBoxImage.Warning,MessageBoxResult.Cancel,MessageBoxOptions.None);
if(res == MessageBoxResult.OK) {
LanguageFile file = (LanguageFile)(targetColumn == 1 ? language1ComboBox.SelectedItem : language2ComboBox.SelectedItem);
PopulateColumn(file, targetColumn);
}
}
}
private void newButtonClicked(object sender, RoutedEventArgs e) {
int targetColumn = GetTargetColumn((Control)sender);
if((targetColumn == 1 && unsavedChangesInLanguage1) || (targetColumn == 2 && unsavedChangesInLanguage2)) {
MessageBoxResult res = MessageBox.Show("Do you really want to discard this column? Unsaved changes will be lost.", "Confirm new language file creation", MessageBoxButton.OKCancel, MessageBoxImage.Warning,MessageBoxResult.Cancel,MessageBoxOptions.None);
if(res != MessageBoxResult.OK) {
return;
}
}
LanguageFile newLang = new LanguageFile();
newLang.FilePath = languagePath;
new MetaEditorWindow(newLang).ShowDialog();
if(newLang.FileName != null && newLang.FileName.Length > 0) {
ClearColumn(targetColumn);
CreateXML(newLang.FilePath,targetColumn);
LanguageFiles.Add(newLang);
LanguageFiles = LanguageFiles.OrderBy(f => f.FileName).ToList();
if(targetColumn == 1) {
LanguageFile1 = newLang;
language1ComboBox.SelectedItem = newLang;
}
else {
LanguageFile2 = newLang;
language2ComboBox.SelectedItem = newLang;
}
PopulateColumn(newLang, targetColumn);
}
}
private void metaButtonClicked(object sender, RoutedEventArgs e) {
int targetColumn = GetTargetColumn((Control)sender);
new MetaEditorWindow(targetColumn == 1 ? LanguageFile1 : LanguageFile2).ShowDialog();
}
private int GetTargetColumn(Control control) {
object tag = control.Tag;
if(tag == null && !tag.Equals("1") && !tag.Equals("2")) {
throw new ApplicationException("Please use the control's Tag property to indicate the column to interact with (1 or 2).");
} else {
return tag.Equals("1") ? 1 : 2;
}
}
private void cellEdited(object sender, DataGridCellEditEndingEventArgs e) {
if(e.Column.DisplayIndex == 1) unsavedChangesInLanguage1 = true;
else if(e.Column.DisplayIndex == 2) unsavedChangesInLanguage2 = true;
}
public void CreateXML(string savePath, int targetColumn) {
LanguageFile langfile = targetColumn == 1 ? LanguageFile1 : LanguageFile2;
ICollectionView view = (ICollectionView)LanguageGrid.ItemsSource;
IList<LanguageEntry> entries = (IList<LanguageEntry>)view.SourceCollection;
using (XmlTextWriter xmlWriter = new XmlTextWriter(savePath, Encoding.UTF8)) {
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.Indentation = 1;
xmlWriter.IndentChar = '\t';
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("language");
xmlWriter.WriteAttributeString("description", langfile.Description);
xmlWriter.WriteAttributeString("ietf", langfile.IETF);
xmlWriter.WriteAttributeString("version", langfile.Version);
xmlWriter.WriteAttributeString("languagegroup", langfile.Languagegroup);
xmlWriter.WriteStartElement("resources");
foreach(LanguageEntry entry in entries) {
xmlWriter.WriteStartElement("resource");
xmlWriter.WriteAttributeString("name", entry.Key);
if(targetColumn == 1) xmlWriter.WriteString(entry.Entry1);
else if(targetColumn == 2 ) xmlWriter.WriteString(entry.Entry2);
else throw new ArgumentOutOfRangeException("Argument columnIndex must be either 1 or 2");
xmlWriter.WriteEndElement();
}
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
}
}
}
public class LanguageEntry : IEditableObject, INotifyPropertyChanged {
private string key;
private string entry1;
private string entry2;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info) {
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public Brush Background {
get {
if (String.IsNullOrEmpty(Entry1)) {
return Brushes.Red;
}
if (String.IsNullOrEmpty(Entry2)) {
return Brushes.Red;
}
return Brushes.White;
}
}
public string Key {
get {
return key;
}
set {
key = value;
NotifyPropertyChanged("Key");
}
}
public string Entry1 {
get {
return entry1;
}
set {
entry1 = value;
NotifyPropertyChanged("Entry1");
NotifyPropertyChanged("Background");
}
}
public string Entry2 {
get {
return entry2;
}
set {
entry2 = value;
NotifyPropertyChanged("Entry2");
NotifyPropertyChanged("Background");
}
}
public void BeginEdit() {
}
public void EndEdit() {
}
public void CancelEdit() {
}
}
}

View file

@ -78,23 +78,36 @@
<DependentUpon>App.xaml</DependentUpon>
</Compile>
<Compile Include="Language.cs" />
<Compile Include="MetaEditorWindow.xaml.cs">
<DependentUpon>MetaEditorWindow.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\WPFAssemblyInfo.cs" />
<Compile Include="Window1.xaml.cs">
<Compile Include="EntriesEditorWindow.xaml.cs">
<SubType>Code</SubType>
<DependentUpon>Window1.xaml</DependentUpon>
<DependentUpon>EntriesEditorWindow.xaml</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<Page Include="Window1.xaml" />
<Page Include="MetaEditorWindow.xaml" />
<Page Include="EntriesEditorWindow.xaml" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="icons\cross.png" />
<Resource Include="icons\cross.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
<Resource Include="icons\disk-black.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<LogicalName>icon-save</LogicalName>
</Resource>
<Resource Include="icons\new.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
<Resource Include="icons\property.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
</ItemGroup>
<ItemGroup>
<Folder Include="icons" />

View file

@ -27,11 +27,19 @@ using System.Xml.Linq;
namespace GreenshotLanguageEditor {
public class LanguageFile {
public string FilePath {
get;
set;
get {return Path.Combine(FileDir,FileName);}
set {
FileDir = Path.GetDirectoryName(value);
FileName = Path.GetFileName(value);
}
}
public string FileDir {
get;
set;
}
public string FileName {
get {return Path.GetFileName(FilePath);}
get;
set;
}
public string IETF {
get;

View file

@ -0,0 +1,102 @@
<?xml version="1.0" encoding="utf-8"?>
<Window
x:Class="GreenshotLanguageEditor.MetaEditorWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="GreenshotLanguageEditor"
Width="320"
Height="243"
Icon="icons\icon.ico">
<Grid>
<Grid.RowDefinitions>
<RowDefinition
Height="Auto"></RowDefinition>
<RowDefinition
Height="Auto"></RowDefinition>
<RowDefinition
Height="Auto"></RowDefinition>
<RowDefinition
Height="Auto"></RowDefinition>
<RowDefinition
Height="Auto"></RowDefinition>
<RowDefinition
Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition
Width="*"></ColumnDefinition>
<ColumnDefinition
Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label
Grid.Row="0"
Grid.Column="0"
Height="22"
Padding="2"
Margin="5">Filename</Label>
<TextBox
Grid.Row="0"
Grid.Column="1"
Height="22"
Margin="5"
Width="150"
Text="{Binding Path=LangFile.FileName, RelativeSource={RelativeSource AncestorType={x:Type Window}}, Mode=TwoWay}"></TextBox>
<Label
Grid.Row="1"
Grid.Column="0"
Height="22"
Padding="2"
Margin="5">Description</Label>
<TextBox
Grid.Row="1"
Grid.Column="1"
Height="22"
Margin="5"
Width="150"
Text="{Binding Path=LangFile.Description, RelativeSource={RelativeSource AncestorType={x:Type Window}}, Mode=TwoWay}"></TextBox>
<Label
Grid.Row="2"
Grid.Column="0"
Height="22"
Padding="2"
Margin="5">IETF</Label>
<TextBox
Grid.Row="2"
Grid.Column="1"
Height="22"
Margin="5"
Width="150"
Text="{Binding Path=LangFile.IETF, RelativeSource={RelativeSource AncestorType={x:Type Window}}, Mode=TwoWay}"></TextBox>
<Label
Grid.Row="3"
Grid.Column="0"
Height="22"
Padding="2"
Margin="5">Language group</Label>
<TextBox
Grid.Row="3"
Grid.Column="1"
Height="22"
Margin="5"
Width="150"
Text="{Binding Path=LangFile.Languagegroup, RelativeSource={RelativeSource AncestorType={x:Type Window}}, Mode=TwoWay}"></TextBox>
<Label
Grid.Row="4"
Grid.Column="0"
Height="22"
Padding="2"
Margin="5">Version</Label>
<TextBox
Grid.Row="4"
Grid.Column="1"
Height="22"
Margin="5"
Width="150"
Text="{Binding Path=LangFile.Version, RelativeSource={RelativeSource AncestorType={x:Type Window}}, Mode=TwoWay}"></TextBox>
<Button
Grid.Row="5"
Grid.Column="1"
Content="Close"
Margin="5,10,5,5"
Name="button1"
Click="button1_Click"></Button>
</Grid>
</Window>

View file

@ -0,0 +1,65 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2012 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Text.RegularExpressions;
namespace GreenshotLanguageEditor
{
/// <summary>
/// Interaction logic for LanguageMetaEditor.xaml
/// </summary>
public partial class MetaEditorWindow : Window {
private static Regex FILENAME_PATTERN = new Regex(@"[a-z]+\-[a-z]{2}\-[A-Z]{2}.xml");
private LanguageFile langFile = new LanguageFile();
public LanguageFile LangFile {
get {return langFile;}
set {langFile = value;}
}
public MetaEditorWindow(LanguageFile langFile) {
InitializeComponent();
this.LangFile = langFile;
this.Closing += new System.ComponentModel.CancelEventHandler(OnClosing);
}
void OnClosing(object sender, System.ComponentModel.CancelEventArgs e) {
if(langFile.FileName != null && langFile.FileName.Length != 0 && !FILENAME_PATTERN.IsMatch(langFile.FileName)) {
MessageBox.Show("The filename is not valid, please use a file name like language-en-US.xml","Filename not valid", MessageBoxButton.OK, MessageBoxImage.Stop);
e.Cancel = true;
}
}
void button1_Click(object sender, RoutedEventArgs e) {
this.Close();
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 269 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 769 B