Пример #1
0
 private void ExportAndroidCommandExecute(object sender, EventArgs e)
 {
     ThreadHelper.ThrowIfNotOnUIThread();
     try
     {
         var projectItem = GetCurrentProjectItem();
         if (projectItem.Name.EndsWith(".resw") &&
             projectItem.Properties.Item("FullPath").Value is string filepath)
         {
             var reswContent = File.ReadAllText(filepath);
             var androidFile = AndroidXMLConverter.ReswToAndroidXML(ReswParser.Parse(reswContent), true);
             using (var saveFileDialog = new SaveFileDialog()
             {
                 FileName = Path.GetFileNameWithoutExtension(projectItem.Name) + ".xml"
             })
             {
                 if (saveFileDialog.ShowDialog() == DialogResult.OK)
                 {
                     androidFile.Save(saveFileDialog.FileName);
                 }
             }
             return;
         }
     }
     catch
     { }
     VsShellUtilities.ShowMessageBox(
         package,
         "Can't convert this resw file",
         "ReswPlus",
         OLEMSGICON.OLEMSGICON_INFO,
         OLEMSGBUTTON.OLEMSGBUTTON_OK,
         OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
 }
Пример #2
0
        private static int ReswToAndroidCommand(ReswToAndroidParameters parameters)
        {
            if (!File.Exists(parameters.Input))
            {
                Console.WriteLine($"The file {parameters.Input} doesn't exist");
                return(-1);
            }

            var reswContent = File.ReadAllText(parameters.Input);
            var resw        = ReswParser.Parse(reswContent);

            if (resw == null)
            {
                Console.WriteLine($"Can't parse the resw file: {parameters.Input}");
                return(-1);
            }
            var androidXML = AndroidXMLConverter.ReswToAndroidXML(resw, parameters.SupportPluralization);

            if (androidXML == null)
            {
                Console.WriteLine($"Error during the conversion of the file: {parameters.Input}");
                return(-1);
            }
            androidXML.Save(parameters.OutputFilePath);
            return(0);
        }
Пример #3
0
        private StronglyTypedClass Parse(string content, string defaultNamespace, bool isAdvanced)
        {
            var namespaceToUse   = ExtractNamespace(defaultNamespace);
            var resourceFileName = Path.GetFileName(_resourceFileInfo.Path);
            var className        = Path.GetFileNameWithoutExtension(_resourceFileInfo.Path);
            var reswInfo         = ReswParser.Parse(content);

            var projectNameIfLibrary = _resourceFileInfo.ParentProject.IsLibrary ? _resourceFileInfo.ParentProject.Name : null;

            //If the resource file is in a library, the resource id in the .pri file
            //will be <library name>/FilenameWithoutExtension
            var resouceNameForResourceLoader = string.IsNullOrEmpty(projectNameIfLibrary) ?
                                               className : projectNameIfLibrary + "/" + className;


            var result = new StronglyTypedClass()
            {
                IsAdvanced  = isAdvanced,
                ClassName   = className,
                Namespaces  = namespaceToUse,
                ResoureFile = resouceNameForResourceLoader
            };

            var stringItems = reswInfo.Items
                              .Where(i => !i.Key.Contains(".") && !(i.Comment?.Contains(TagIgnore) ?? false)).ToArray();

            if (isAdvanced)
            {
                //check Pluralization
                var itemsWithPluralOrVariant = reswInfo.Items.GetItemsWithVariantOrPlural();
                var basicItems = stringItems.Except(itemsWithPluralOrVariant.SelectMany(e => e.Items)).ToArray();

                foreach (var item in itemsWithPluralOrVariant)
                {
                    if (item.SupportPlural)
                    {
                        var idNone      = item.Key + "_None";
                        var hasNoneForm = reswInfo.Items.Any(i => i.Key == idNone);

                        var singleLineValue = _regexRemoveSpace.Replace(item.Items.FirstOrDefault().Value, " ").Trim();

                        var summary = $"Get the pluralized version of the string similar to: {singleLineValue}";

                        PluralLocalization localization;
                        if (item.SupportVariants)
                        {
                            localization = new PluralVariantLocalization()
                            {
                                Key              = item.Key,
                                Summary          = summary,
                                SupportNoneState = hasNoneForm,
                            };
                        }
                        else
                        {
                            localization = new PluralLocalization()
                            {
                                Key              = item.Key,
                                Summary          = summary,
                                SupportNoneState = hasNoneForm,
                            };
                        }
                        if (item.Items.Any(i => i.Comment != null && i.Comment.Contains(Deprecated_TagStrongType)))
                        {
                            _logger?.LogError($"{Deprecated_TagStrongType} is no more supported, use {TagFormat} instead. See https://github.com/rudyhuyn/ReswPlus/blob/master/README.md");
                        }
                        var commentToUse =
                            item.Items.FirstOrDefault(i => i.Comment != null && _regexStringFormat.IsMatch(i.Comment));

                        ManageFormattedFunction(localization, commentToUse?.Comment, basicItems, resourceFileName);

                        result.Localizations.Add(localization);
                    }
                    else if (item.SupportVariants)
                    {
                        var singleLineValue = _regexRemoveSpace.Replace(item.Items.FirstOrDefault().Value, " ").Trim();
                        var summary         = $"Get the variant version of the string similar to: {singleLineValue}";
                        var commentToUse    = item.Items.FirstOrDefault(i => i.Comment != null && _regexStringFormat.IsMatch(i.Comment));

                        var localization = new VariantLocalization()
                        {
                            Key     = item.Key,
                            Summary = summary,
                        };

                        ManageFormattedFunction(localization, commentToUse?.Comment, basicItems, resourceFileName);

                        result.Localizations.Add(localization);
                    }
                }

                stringItems = basicItems;
            }

            if (stringItems.Any())
            {
                foreach (var item in stringItems)
                {
                    var singleLineValue = _regexRemoveSpace.Replace(item.Value, " ").Trim();
                    var summary         = $"Looks up a localized string similar to: {singleLineValue}";

                    var localization = new RegularLocalization()
                    {
                        Key     = item.Key,
                        Summary = summary,
                    };

                    if (isAdvanced)
                    {
                        ManageFormattedFunction(localization, item.Comment, stringItems, resourceFileName);
                    }
                    result.Localizations.Add(localization);
                }
            }

            return(result);
        }