Ejemplo n.º 1
0
        /// <summary>
        /// Returns number of string resources in specified ResX file
        /// </summary>
        private int GetResourcesCountIn(ResXProjectItem item)
        {
            item.Load();
            int result = item.GetAllStringReferences(false).Count;

            item.Unload();

            return(result);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Removes all data from specified ResX file
 /// </summary>
 private void ClearFile(ResXProjectItem target)
 {
     try {
         target.Load();
         target.Data.Clear();
         target.Flush();
         target.Unload();
     } catch (Exception) { }
 }
        /// <summary>
        /// Add string data from given ResX file to the list of data for translation
        /// </summary>
        private void AddDataForTranslation(GlobalTranslateProjectItem item, List <AbstractTranslateInfoItem> data)
        {
            string path = item.ProjectItem.GetFullPath();

            if (RDTManager.IsFileOpen(path))                              // file is open
            {
                object docData = VLDocumentViewsManager.GetDocData(path); // get document buffer
                if (docData is ResXEditor)                                // document is opened in ResX editor -> use custom method to get string data
                {
                    ResXEditor editor = (ResXEditor)docData;
                    editor.UIControl.AddForTranslation(data);
                }
                else     // document is opened in original VS editor
                {
                    IVsTextLines lines = VLDocumentViewsManager.GetTextLinesForFile(path, false);
                    string       text  = VLDocumentViewsManager.GetTextFrom(lines); // get plain text of ResX file

                    ResXResourceReader reader = null;

                    BufferTranslateInfoItem prev  = null;
                    BufferTranslateInfoItem first = null;

                    try {
                        reader = ResXResourceReader.FromFileContents(text);
                        reader.UseResXDataNodes = true;

                        // add all string resources to the list
                        // items are linked like a linked-list, allowing ApplyTranslation to work
                        foreach (DictionaryEntry entry in reader)
                        {
                            ResXDataNode node = (entry.Value as ResXDataNode);
                            if (node.HasValue <string>())
                            {
                                BufferTranslateInfoItem translateItem = new BufferTranslateInfoItem();
                                translateItem.ResourceKey         = entry.Key.ToString();
                                translateItem.Value               = node.GetValue <string>();
                                translateItem.Filename            = path;
                                translateItem.Applied             = false;
                                translateItem.GlobalTranslateItem = item;
                                translateItem.Prev         = prev;
                                translateItem.IVsTextLines = lines;

                                data.Add(translateItem);
                                prev = translateItem;
                                if (first == null)
                                {
                                    first = translateItem;
                                }
                            }
                            else
                            {
                                item.NonStringData.Add(node);
                            }
                        }
                        if (first != null)
                        {
                            first.Prev = prev;
                        }
                    } finally {
                        if (reader != null)
                        {
                            reader.Close();
                        }
                    }
                }
            }
            else     // file is closed
            {
                ResXProjectItem resxItem = ResXProjectItem.ConvertToResXItem(item.ProjectItem, item.ProjectItem.ContainingProject);
                resxItem.Load();
                loadedResxItems.Add(resxItem);

                // add string data from ResX file
                resxItem.AddAllStringReferencesUnique(data);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Validates given row and updates its error messages
        /// </summary>
        protected override void Validate(DataGridViewKeyValueRow <CodeStringResultItem> row)
        {
            object dest            = row.Cells[DestinationColumnName].Value;
            bool   existsSameValue = false;

            if (dest == null)   // no destination file was selected
            {
                row.ErrorMessages.Add(NoDestinationFileError);
            }
            else
            {
                row.ErrorMessages.Remove(NoDestinationFileError);

                ResXProjectItem resxItem = resxItemsCache[dest.ToString()];
                if (!resxItem.IsLoaded)
                {
                    resxItem.Load();                                                                          // load the ResX file
                    VLDocumentViewsManager.SetFileReadonly(resxItem.InternalProjectItem.GetFullPath(), true); // lock it
                    loadedItems.Add(resxItem);
                }

                string key   = row.Key;
                string value = row.Value;

                CONTAINS_KEY_RESULT keyConflict = resxItem.GetKeyConflictType(key, value, true); // get conflict type
                switch (keyConflict)
                {
                case CONTAINS_KEY_RESULT.EXISTS_WITH_SAME_VALUE:
                    row.ErrorMessages.Remove(DuplicateKeyError);
                    existsSameValue = true;
                    break;

                case CONTAINS_KEY_RESULT.EXISTS_WITH_DIFF_VALUE:
                    row.ErrorMessages.Add(DuplicateKeyError);
                    break;

                case CONTAINS_KEY_RESULT.DOESNT_EXIST:
                    row.ErrorMessages.Remove(DuplicateKeyError);
                    break;
                }

                string originalValue = (string)row.Cells[KeyColumnName].Tag;
                ((DestinationKeyValueConflictResolver)ConflictResolver).TryAdd(originalValue, key, row, resxItem, row.DataSourceItem.Language);
                if (originalValue == null)
                {
                    row.Cells[KeyColumnName].Tag = key;
                }
            }

            row.UpdateErrorSetDisplay(); // update error messages

            if (row.ErrorMessages.Count == 0)
            {
                if (existsSameValue)   // set background color according to conflict type
                {
                    row.DefaultCellStyle.BackColor = ExistingKeySameValueColor;
                }
                else
                {
                    row.DefaultCellStyle.BackColor = Color.White;
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Executed on every change in window form
        /// </summary>
        private void ValidateData()
        {
            bool   existsFile = comboBox.Items.Count > 0; // there's at least one possible destination file
            string errorText  = null;
            bool   ok         = true;

            if (!existsFile)   // no destination file
            {
                ok        = false;
                errorText = "Project does not contain any useable resource files";
            }
            else
            {
                ResXProjectItem item = comboBox.SelectedItem as ResXProjectItem;
                if (!item.IsLoaded)
                {
                    item.Load();
                    VLDocumentViewsManager.SetFileReadonly(item.InternalProjectItem.GetFullPath(), true);
                }
                resultItem.DestinationItem = item;

                bool isKeyEmpty            = string.IsNullOrEmpty(keyBox.Text);
                bool isValidIdentifier     = keyBox.Text.IsValidIdentifier(resultItem.Language);
                bool hasOwnDesigner        = (item.DesignerItem != null || item.HasImplicitDesignerFile) && !item.IsCultureSpecific();
                bool identifierErrorExists = false;

                // determine whether current key name is valid
                switch (SettingsObject.Instance.BadKeyNamePolicy)
                {
                case BAD_KEY_NAME_POLICY.IGNORE_COMPLETELY:
                    identifierErrorExists = isKeyEmpty;     // only empty keys are invalid
                    break;

                case BAD_KEY_NAME_POLICY.IGNORE_ON_NO_DESIGNER:
                    identifierErrorExists = isKeyEmpty || (!isValidIdentifier && hasOwnDesigner);     // empty keys and invalid identifiers in ResX files with their own designer file
                    break;

                case BAD_KEY_NAME_POLICY.WARN_ALWAYS:
                    identifierErrorExists = isKeyEmpty || !isValidIdentifier;     // empty keys and invalid identifiers
                    break;
                }

                if (!identifierErrorExists)   // identifier ok - check for key name conflicts
                {
                    keyConflict = item.GetKeyConflictType(keyBox.Text, valueBox.Text, true);

                    Color backColor = Color.White;
                    switch (keyConflict)
                    {
                    case CONTAINS_KEY_RESULT.EXISTS_WITH_SAME_VALUE:     // key already exists and has the same value - ok
                        backColor = EXISTING_KEY_COLOR;
                        break;

                    case CONTAINS_KEY_RESULT.EXISTS_WITH_DIFF_VALUE:     // key exists with different value - error
                        errorText             = "Key is already present and has different value";
                        existingValueBox.Text = item.GetString(keyBox.Text);
                        backColor             = ERROR_COLOR;
                        break;

                    case CONTAINS_KEY_RESULT.DOESNT_EXIST:     // key doesn't exists - ok
                        backColor = Color.White;
                        break;
                    }

                    overwriteButton.Visible  = keyConflict == CONTAINS_KEY_RESULT.EXISTS_WITH_DIFF_VALUE;
                    inlineButton.Visible     = keyConflict == CONTAINS_KEY_RESULT.EXISTS_WITH_DIFF_VALUE;
                    existingValueBox.Visible = keyConflict == CONTAINS_KEY_RESULT.EXISTS_WITH_DIFF_VALUE;
                    existingLabel.Visible    = keyConflict == CONTAINS_KEY_RESULT.EXISTS_WITH_DIFF_VALUE;

                    keyBox.BackColor   = backColor;
                    valueBox.BackColor = backColor;
                }
                else
                {
                    errorText          = "Key is not a valid identifier";
                    keyBox.BackColor   = ERROR_COLOR;
                    valueBox.BackColor = Color.White;
                }

                ok = !identifierErrorExists && !overwriteButton.Visible;

                referenceText.ClassPart = item.Class;
                referenceText.KeyPart   = keyBox.Text;

                if (string.IsNullOrEmpty(item.Namespace))   // no namespace was found in designer file - error
                {
                    ok        = false;
                    errorText = "Cannot reference resources in this file, missing namespace";
                }
                else
                {
                    if (!usingBox.Checked || resultItem.MustUseFullName)   // force using full reference
                    {
                        referenceText.NamespacePart = item.Namespace;
                    }
                    else
                    {
                        referenceText.NamespacePart = null;
                    }
                }

                referenceLabel.Text = resultItem.GetReferenceText(referenceText);
            }

            okButton.Enabled = ok;
            if (ok)
            {
                errorLabel.Text = string.Empty;
            }
            else
            {
                errorLabel.Text = errorText;
            }
        }