using System;
using System.Collections.ObjectModel;
namespace Claunia.Localization.Core
{
///
/// Represent a message and its translations
///
public sealed class Message
{
readonly ObservableCollection parameters;
readonly ObservableCollection translations;
string comments;
string context;
string id;
internal EventHandler Modified;
string previousContext;
string reference;
internal Message()
{
Source = new LocalizedString();
Source.Modified += Modified;
translations = new ObservableCollection();
translations.CollectionChanged += (sender, args) => Modified?.Invoke(this, EventArgs.Empty);
parameters = new ObservableCollection();
parameters.CollectionChanged += (sender, args) => Modified?.Invoke(this, EventArgs.Empty);
Translations = new ReadOnlyObservableCollection(translations);
Parameters = new ReadOnlyObservableCollection(parameters);
}
///
/// Describes the context where this message occurs
///
public string Context
{
get => context;
set
{
context = value;
Modified?.Invoke(this, EventArgs.Empty);
}
}
///
/// Describes the context where the previous version of this message occurred
///
public string PreviousContext
{
get => previousContext;
set
{
previousContext = value;
Modified?.Invoke(this, EventArgs.Empty);
}
}
///
/// Comments added by the original writer of the message
///
public string Comments
{
get => comments;
set
{
comments = value;
Modified?.Invoke(this, EventArgs.Empty);
}
}
///
/// Source code line reference or equivalent
///
public string Reference
{
get => reference;
set
{
reference = value;
Modified?.Invoke(this, EventArgs.Empty);
}
}
///
/// Unique identifier of this message
///
public string Id
{
get => id;
set
{
id = value;
Modified?.Invoke(this, EventArgs.Empty);
}
}
///
/// Original locale of this message
///
public LocalizedString Source { get; }
///
/// List of translations
///
public ReadOnlyObservableCollection Translations { get; }
///
/// List of parameters
///
public ReadOnlyObservableCollection Parameters { get; }
public override string ToString() => Source.Singular;
///
/// Initializes a new localized string in this message
///
/// The newly initialized localized string
public LocalizedString NewLocalizedString()
{
LocalizedString localizedString = new LocalizedString();
localizedString.Modified += Modified;
translations.Add(localizedString);
return localizedString;
}
///
/// Removes a localized string
///
/// The localized string to remove
/// true if the localized string has been removed successfully, false otherwise
public bool RemoveLocalizedString(LocalizedString localizedString) => translations.Remove(localizedString);
///
/// Initializes a new parameter in this message
///
/// The newly initialized parameter
public Parameter NewParameter()
{
Parameter parameter = new Parameter();
parameter.Modified += Modified;
parameters.Add(parameter);
return parameter;
}
///
/// Removes a parameter
///
/// The parameter to remove
/// true if the parameter string has been removed successfully, false otherwise
public bool RemoveParameter(Parameter parameter) => parameters.Remove(parameter);
}
}