source: trunk/CrypPluginBase/Miscellaneous/LocExtension.cs

Last change on this file was 8983, checked in by kopal, 7 months ago

Complete CrypTool 2 project

  • renamed "Cryptool" namespace to "CrypTool" namespace
File size: 3.1 KB
Line 
1/*
2   Copyright 2010 Sven Rech
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15*/
16
17using System;
18using System.Resources;
19using System.Windows.Markup;
20using System.Xaml;
21using CrypTool.PluginBase.Attributes;
22using CrypTool.PluginBase.Properties;
23
24// Register the extention in the Microsoft's default namespaces
25[assembly: System.Windows.Markup.XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "CrypTool.PluginBase.Miscellaneous")]
26[assembly: System.Windows.Markup.XmlnsDefinition("http://schemas.microsoft.com/winfx/2007/xaml/presentation", "CrypTool.PluginBase.Miscellaneous")]
27[assembly: System.Windows.Markup.XmlnsDefinition("http://schemas.microsoft.com/winfx/2008/xaml/presentation", "CrypTool.PluginBase.Miscellaneous")]
28
29namespace CrypTool.PluginBase.Miscellaneous
30{
31    [MarkupExtensionReturnType(typeof(object))]
32    [ContentProperty("Key")]
33    public class LocExtension : MarkupExtension
34    {
35        public delegate void GuiLogMessageHandler(string message, NotificationLevel logLevel);
36        public static event GuiLogMessageHandler OnGuiLogMessageOccured;
37
38        public static void GuiLogMessageOccured(string message, NotificationLevel loglevel)
39        {
40            if (OnGuiLogMessageOccured != null)
41                OnGuiLogMessageOccured(message, loglevel);
42        }
43
44        public string Key { get; set; }
45
46        public LocExtension(String key)
47        {
48            Key = key;
49        }
50
51        public override object ProvideValue(IServiceProvider serviceProvider)
52        {
53            try
54            {
55                IRootObjectProvider service = serviceProvider.GetService(typeof(IRootObjectProvider)) as IRootObjectProvider;
56                var locAttribute = (LocalizationAttribute)Attribute.GetCustomAttribute(service.RootObject.GetType(), typeof(LocalizationAttribute));
57                ResourceManager resman = new ResourceManager(locAttribute.ResourceClassPath, service.RootObject.GetType().Assembly);
58
59                if (resman.GetString(Key) != null)
60                    return resman.GetString(Key);
61                else
62                {
63                    GuiLogMessageOccured(string.Format(Resources.Can_t_find_localization_key, Key, service.RootObject.GetType()), NotificationLevel.Warning);
64                    return Key;
65                }
66            }
67            catch (Exception ex)
68            {
69                GuiLogMessageOccured(string.Format(Resources.Error_trying_to_lookup_localization_key, Key, ex.Message), NotificationLevel.Warning);
70                return Key;
71            }
72        }
73
74    }
75}
Note: See TracBrowser for help on using the repository browser.