Exemplo n.º 1
0
        /// <summary>
        ///     Writes the contents of a resx file (as key-value pairs) to project using the Android directory hierarchy and
        ///     resource file standards.
        /// </summary>
        /// <param name="project"></param>
        /// <param name="strings"></param>
        /// <param name="sourceFile"></param>
        public static void WriteToTarget(
            ProjectInSolution project,
            IDictionary <string, string> strings,
            FileInfo sourceFile
            )
        {
            // Generate the XML representation of the data
            var resourceType = sourceFile.GetResourceType();

            // Bool resources must be lowercase ('true' not 'True')
            if (resourceType == ResourceType.Boolean)
            {
                strings = strings.ToDictionary(item => item.Key, item => item.Value.ToLower());
            }

            // Create Android resource XML
            var content = GetXmlContent(resourceType, strings);

            // Setup output path and file name
            var outputFileName = $"{resourceType}s.xml";
            var inputFileName  = Path.GetFileNameWithoutExtension(sourceFile.Name);

            // Put translations in their appropriate directories (e.g. '/values-es/strings.xml')
            var valuesDir = "values/";

            if ((resourceType == ResourceType.String) && !inputFileName.Equals("strings"))
            {
                valuesDir = $"values-{inputFileName}/";
            }

            // Write to file, creating directories as necessary
            var targetDir = Path.Combine(project.ContainingDirectory(), $"resources/{valuesDir}");

            FileHandler.WriteToFile(targetDir, outputFileName, content);
        }
        /// <summary>
        ///     Writes the contents of a resx file (as key-value pairs) to the passed project using the iOS directory hierarchy and
        ///     a series of static C# classes.
        /// </summary>
        /// <param name="project">The iOS project to write to</param>
        /// <param name="strings">The resx file's contents as a key-value store</param>
        /// <param name="sourceFile">The resx file's information</param>
        public static void WriteToTarget(
            ProjectInSolution project,
            IDictionary <string, string> strings,
            FileInfo sourceFile
            )
        {
            // Prepare to write to iOS resources directory
            var targetDir    = Path.Combine(project.ContainingDirectory(), "resources/");
            var nameSpace    = project.ProjectName + ".Resources";
            var resourceType = sourceFile.GetResourceType();

            // Translate the resx loaded resources into their associated iOS resource type
            string staticFields;

            switch (resourceType)
            {
            case ResourceType.String:
                BuildStringResource(strings, sourceFile, targetDir);
                return;

            case ResourceType.Color:
                strings = strings.ToDictionary(
                    item => item.Key,
                    item => $"FromHexString(\"{item.Value.EscapeString()}\")"
                    );

                staticFields = StaticClassHandler.GenerateStaticClassContent(
                    strings,
                    resourceType.StaticType(ProjectType.Ios)
                    );
                staticFields += Config.ColorConverterCode;
                break;

            case ResourceType.Boolean:
                // C# bools are only lowercase ('true', 'false'; not 'True', 'False')
                strings      = strings.ToDictionary(item => item.Key, item => item.Value.ToLower());
                staticFields = StaticClassHandler.GenerateStaticClassContent(strings, resourceType.StaticType());
                break;

            default:
                staticFields = StaticClassHandler.GenerateStaticClassContent(strings, resourceType.StaticType());
                break;
            }

            var className = $"{resourceType}s";
            var content   = StaticClassHandler.GenerateStaticClass(nameSpace, className, staticFields);

            FileHandler.WriteToFile(targetDir, $"{className}.cs", content);
        }