using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; using ScrewTurn.Wiki.PluginFramework; namespace ScrewTurn.Wiki.Plugins.PluginPack { /// /// Implements a Formatter Provider that allows to write multi-language content in Wiki Pages. /// public class MultilanguageContentPlugin : IFormatterProviderV30 { private IHostV30 host; private string config; private ComponentInformation info = new ComponentInformation("Multilanguage Content Plugin", "ScrewTurn Software", "3.0.0.180", "http://www.screwturn.eu", "http://www.screwturn.eu/Version/PluginPack/Multilanguage.txt"); private string defaultLanguage = "en-us"; private bool displayWarning = false; private const string DivStyle = "padding: 2px; margin-bottom: 10px; font-size: 11px; background-color: #FFFFC4; border: solid 1px #DDDDDD;"; private const string StandardMessage = @"
The content of this Page is localized in your language. To change the language settings, please go to the Language Selection page.
"; private const string NotLocalizedMessage = @"
The content of this Page is not available in your language, and it is displayed in the default language of the Wiki. To change the language settings, please go to the Language Selection page.
"; /// /// Specifies whether or not to execute Phase 1. /// public bool PerformPhase1 { get { return false; } } /// /// Specifies whether or not to execute Phase 2. /// public bool PerformPhase2 { get { return false; } } /// /// Specifies whether or not to execute Phase 3. /// public bool PerformPhase3 { get { return true; } } /// /// Performs a Formatting phase. /// /// The raw content to Format. /// The Context information. /// The Phase. /// The Formatted content. public string Format(string raw, ContextInformation context, FormattingPhase phase) { string result = ExtractLocalizedContent(context.Language, raw); // Try to load localized content bool notLocalized = false; bool noLocalization = false; if(result == null) { result = ExtractLocalizedContent(defaultLanguage, raw); // Load content in the default language notLocalized = true; noLocalization = false; } if(result == null) { result = raw; // The Page is not localized, return all the content notLocalized = false; noLocalization = true; } if(displayWarning && !noLocalization && context.Page != null) { if(notLocalized) return NotLocalizedMessage + result; else return StandardMessage + result; } else return result; } private string ExtractLocalizedContent(string language, string content) { string head = "\\<" + language + "\\>", tail = "\\<\\/" + language + "\\>"; Regex regex = new Regex(head + "(.+?)" + tail, RegexOptions.IgnoreCase | RegexOptions.Singleline); Match match = regex.Match(content); StringBuilder sb = new StringBuilder(1000); while(match.Success) { sb.Append(match.Groups[1].Value); match = regex.Match(content, match.Index + match.Length); } if(sb.Length > 0) return sb.ToString(); else return null; } /// /// Initializes the Storage Provider. /// /// The Host of the Component. /// The Configuration data, if any. /// If the configuration string is not valid, the methoud should throw a . public void Init(IHostV30 host, string config) { this.host = host; this.config = config != null ? config : ""; defaultLanguage = host.GetSettingValue(SettingName.DefaultLanguage); displayWarning = config.ToLowerInvariant().Equals("display warning"); } /// /// Method invoked on shutdown. /// /// This method might not be invoked in some cases. public void Shutdown() { } /// /// Gets the Information about the Provider. /// public ComponentInformation Information { get { return info; } } /// /// Gets the execution priority of the provider (0 lowest, 100 highest). /// public int ExecutionPriority { get { return 50; } } /// /// Prepares the title of an item for display (always during phase 3). /// /// The input title. /// The context information. /// The prepared title (no markup allowed). public string PrepareTitle(string title, ContextInformation context) { string result = ExtractLocalizedContent(context.Language, title); // Try to load localized content if(context.ForIndexing || result == null) { result = ExtractLocalizedContent(defaultLanguage, title); // Load content in the default language } if(result == null) { result = title; // The Page is not localized, return all the content } return result; } /// /// Gets a brief summary of the configuration string format, in HTML. Returns null if no configuration is needed. /// public string ConfigHelpHtml { get { return "Specify 'display warning' to notify the user of the available content languages."; } } } }