Beispiel #1
0
            public PeptideToMoleculeTextMapper(SrmDocument.DOCUMENT_TYPE modeUI, ModeUIExtender extender)
            {
                // Japanese has a set of characters that are easily swapped between Hiragana and Katakana
                // One orginally appeared in about 2% of the translations of the word "peptide"
//                const char jaHeHiragana = 'へ';
//                const char jaHeKatakana = 'ヘ';
//                const char jaBeHiragana = 'べ';
//                const char jaBeKatakana = 'ベ';
                const char jaPeHiragana = 'ぺ';  // Used as a typo in "peptide"
                const char jaPeKatakana = 'ペ';  // Used in "peptide"

                // The basic replacements (not L10N to pick up not-yet-localized UI - maintain the list below in concert with this one)
                var dict = new Dictionary <string, string>
                {
                    // ReSharper disable LocalizableElement
                    { "Peptide", "Molecule" },
                    { "Peptides", "Molecules" },
                    { "Protein", "Molecule List" },
                    { "Proteins", "Molecule Lists" },
                    { "Modified Sequence", "Molecule" },
                    { "Peptide Sequence", "Molecule" },
                    { "Modified Peptide Sequence", "Molecule" },
                    { "Ion Charges", "Ion adducts" }
                    // ReSharper restore LocalizableElement
                };

                // Handle lower case as well
                foreach (var kvp in dict.ToArray())
                {
                    dict.Add(kvp.Key.ToLowerInvariant(), kvp.Value.ToLowerInvariant());
                    // Also handle combinations of upper/lower case (eg "Modified peptide sequence")
                    var space = kvp.Key.IndexOf(' ');
                    if (space > 0)
                    {
                        dict.Add(kvp.Key.Substring(0, space) + kvp.Key.Substring(space).ToLowerInvariant(), kvp.Value);
                    }
                }

                ModeUI = modeUI == SrmDocument.DOCUMENT_TYPE.none ? SrmDocument.DOCUMENT_TYPE.proteomic : modeUI; // SrmDocument.DOCUMENT_TYPE.none would be unusual, but would still mean no translation

                // Handle keyboard accelerators where possible: P&eptide => Mol&ecule
                var set = new HashSet <KeyValuePair <string, string> >();

                foreach (var kvp in dict)
                {
                    var pep = kvp.Key.ToLowerInvariant();
                    var mol = kvp.Value.ToLowerInvariant();
                    foreach (var c in pep.Distinct().Where(c => Char.IsLetterOrDigit(c) && mol.Contains(c)))
                    {
                        var positionP = pep.IndexOf(c);
                        var positionM = mol.IndexOf(c);
                        // Prefer to map "Peptide &List" to "Molecule &List" rather than "Mo&lecule List"
                        if (Char.IsUpper(kvp.Key[positionP]))
                        {
                            var positionU = kvp.Value.IndexOf(kvp.Key[positionP]);
                            if (positionU >= 0)
                            {
                                positionM = positionU;
                            }
                        }

                        var amp = @"&";
                        set.Add(new KeyValuePair <string, string>(kvp.Key.Insert(positionP, amp), kvp.Value.Insert(positionM, amp)));
                    }
                    set.Add(kvp);
                }

                // Add localized versions, if any
                // NB this assumes that localized versions of Skyline are non-western, and don't attempt to embed keyboard accelerators in control texts
                var currentUICulture = Thread.CurrentThread.CurrentUICulture;
                var cultureNames     = (extender == null) // This is only true in the case where we're constructing our static object
                    ? new[] { @"zh-CHS", @"ja" }  // Culture can change in lifetime of a static object in our test system, so include all
                    : new[] { currentUICulture.Name };

                foreach (var cultureName in cultureNames)
                {
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureName);
                    var setL10N = new HashSet <KeyValuePair <string, string> >
                    {
                        new KeyValuePair <string, string>(Resources.PeptideToMoleculeText_Peptide,
                                                          Resources.PeptideToMoleculeText_Molecule),
                        new KeyValuePair <string, string>(Resources.PeptideToMoleculeText_Peptides,
                                                          Resources.PeptideToMoleculeText_Molecules),
                        new KeyValuePair <string, string>(Resources.PeptideToMoleculeText_Protein,
                                                          Resources.PeptideToMoleculeText_Molecule_List),
                        new KeyValuePair <string, string>(Resources.PeptideToMoleculeText_Proteins,
                                                          Resources.PeptideToMoleculeText_Molecule_Lists),
                        new KeyValuePair <string, string>(Resources.PeptideToMoleculeText_Peptide_Sequence,
                                                          Resources.PeptideToMoleculeText_Molecule),
                        new KeyValuePair <string, string>(Resources.PeptideToMoleculeText_Modified_Sequence,
                                                          Resources.PeptideToMoleculeText_Molecule),
                        new KeyValuePair <string, string>(Resources.PeptideToMoleculeText_Modified_Peptide_Sequence,
                                                          Resources.PeptideToMoleculeText_Molecule),
                        new KeyValuePair <string, string>(Resources.PeptideToMoleculeText_Peptide_List,
                                                          Resources.PeptideToMoleculeText_Molecule_List),
                        new KeyValuePair <string, string>(Resources.PeptideToMoleculeText_Ion_charges,
                                                          Resources.PeptideToMoleculeText_Ion_adducts)
                    };
                    foreach (var kvp in setL10N.Where(kp => !dict.ContainsKey(kp.Key))) // Avoids adding not-yet-translated keys
                    {
                        set.Add(kvp);
                        set.Add(new KeyValuePair <string, string>(kvp.Key.ToLower(), kvp.Value.ToLower()));
                        // As protection, just in case the Hiragana "Pe" ever shows up in our translations again
                        if (cultureName.Equals(@"ja") && kvp.Key.Contains(jaPeKatakana))
                        {
                            set.Add(new KeyValuePair <string, string>(kvp.Key.Replace(jaPeKatakana, jaPeHiragana), kvp.Value));
                        }
                    }
                }
                Thread.CurrentThread.CurrentUICulture = currentUICulture;

                // Sort so we look for longer replacements first
                var list = set.ToList();

                list.Sort((a, b) => b.Key.Length.CompareTo(a.Key.Length));
                TRANSLATION_TABLE         = new List <KeyValuePair <string, string> >(list);
                InUseKeyboardAccelerators = new HashSet <char>();
                ToolTip           = null;
                HandledComponents = extender == null ? new Dictionary <IComponent, ModeUIExtender.MODE_UI_HANDLING_TYPE>() : extender.GetHandledComponents();
            }
Beispiel #2
0
            // For all controls in a form, attempt to take a string like "{0} peptides" and return one like "{0} molecules" if doctype is not purely proteomic
            public static void TranslateForm(Form form, SrmDocument.DOCUMENT_TYPE modeUI, ModeUIExtender extender = null)
            {
                if (form != null)
                {
                    var mapper = new PeptideToMoleculeTextMapper(modeUI, extender);
                    form.Text = mapper.TranslateString(form.Text); // Update the title

                    // Find a tooltip component in the form, if any
                    var tips = (from field in form.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                                where typeof(Component).IsAssignableFrom(field.FieldType)
                                let component = (Component)field.GetValue(form)
                                                where component is ToolTip
                                                select component as ToolTip).ToArray();
                    mapper.ToolTip = tips.FirstOrDefault();

                    mapper.FindInUseKeyboardAccelerators(form.Controls);

                    mapper.Translate(form.Controls); // Update the controls
                }
            }
Beispiel #3
0
            // For all items in a menu, attempt to take a string like "{0} peptides" and return one like "{0} molecules" if menu item is not purely proteomic
            // Update keyboard accelerators as needed
            public static void TranslateMenuItems(ToolStripItemCollection items, SrmDocument.DOCUMENT_TYPE modeUI, ModeUIExtender extender)
            {
                var mapper = new PeptideToMoleculeTextMapper(modeUI, extender);

                if (items != null)
                {
                    mapper.FindInUseKeyboardAccelerators(items);
                    var activeItems = new List <ToolStripItem>();
                    for (int i = 0; i < items.Count; i++)
                    {
                        var item = items[i];
                        ModeUIExtender.MODE_UI_HANDLING_TYPE handlingType;
                        if (!mapper.HandledComponents.TryGetValue(item, out handlingType))
                        {
                            handlingType = ModeUIExtender.MODE_UI_HANDLING_TYPE.auto;
                        }

                        bool isActive;
                        switch (modeUI)
                        {
                        case SrmDocument.DOCUMENT_TYPE.proteomic:
                            isActive = handlingType != ModeUIExtender.MODE_UI_HANDLING_TYPE.small_mol &&
                                       handlingType != ModeUIExtender.MODE_UI_HANDLING_TYPE.small_mol_only &&
                                       handlingType != ModeUIExtender.MODE_UI_HANDLING_TYPE.mixed_only;
                            break;

                        case SrmDocument.DOCUMENT_TYPE.small_molecules:
                            isActive = handlingType != ModeUIExtender.MODE_UI_HANDLING_TYPE.proteomic &&
                                       handlingType != ModeUIExtender.MODE_UI_HANDLING_TYPE.mixed_only;
                            break;

                        case SrmDocument.DOCUMENT_TYPE.mixed:
                            isActive = handlingType != ModeUIExtender.MODE_UI_HANDLING_TYPE.small_mol_only;
                            break;

                        default:
                            isActive = false;
                            Assume.Fail(@"unknown UI mode");
                            break;
                        }

                        if (isActive)
                        {
                            activeItems.Add(item);
                        }
                        item.Visible = isActive;
                    }
                    mapper.Translate(activeItems); // Update the menu items that aren't inherently wrong for current UI mode
                }
            }
Beispiel #4
0
 public ModeUIAwareFormHelper(ModeUIExtender modeUIExtender)
 {
     _modeUIExtender = modeUIExtender;
 }