예제 #1
0
        GlobalizationDataSet ReadResourcesFromDatabase(IDbDriver dataSource, string tableName)
        {
            AppendToLog("Reading database");


            GlobalizationDataSet ds = new GlobalizationDataSet();

            if (string.IsNullOrEmpty(tableName))
            {
                // default table name
                tableName = ds.CulturalResources.TableName;
            }

            DataTable dt = dataSource.GetTableData(tableName);

            try
            {
                DataTableReader reader = new DataTableReader(dt);
                ds.CulturalResources.BeginLoadData();
                ds.CulturalResources.Load(reader);
                ds.CulturalResources.EndLoadData();
            }
            catch (Exception ex)
            {
                throw new GeneralException("Import database format is invalid.", ex);
            }

            return(ds);
        }
예제 #2
0
        /// <summary>
        /// ImportLanguage()
        /// </summary>
        /// <param name="applicationPath"></param>
        /// <param name="targetCulture"></param>
        /// <param name="dataSource"></param>
        /// <param name="tableName"></param>
        /// <param name="callback"></param>
        public static void ImportLanguage(string applicationPath, string targetCulture, IDbDriver dataSource, string tableName, AppendLogCallback callback)
        {
            ResourceTool instance = new ResourceTool(applicationPath, callback);

            GlobalizationDataSet ds = instance.ReadResourcesFromDatabase(dataSource, tableName);

            instance.CompileSatelliteAssemblies(targetCulture, ds);
        }
예제 #3
0
        /// <summary>
        /// Extract text resources from assemblies in specified folder
        /// </summary>
        /// <param name="applicationPath">Path of application.</param>
        /// <param name="sourceCulture">Culture code of the source.</param>
        /// <param name="targetCulture">Culture code of the target.</param>
        /// <param name="translator">Translation component.</param>
        /// <param name="exportFilePath">Path of file to export.</param>
        /// <param name="callback">Address pointer for <see cref="Epi.Windows.Localization.AppendLogCallback"/>.</param>
        public static void ExportLanguage(string applicationPath, string sourceCulture, string targetCulture, ITranslator translator, string exportFilePath, AppendLogCallback callback)
        {
            ResourceTool.dbFactory = Epi.Data.DbDriverFactoryCreator.GetDbDriverFactory(Epi.Configuration.AccessDriver);
            ResourceTool instance = new ResourceTool(applicationPath, callback);

            //IDbDriver db = DatabaseFactoryCreator.CreateDatabaseInstanceByFileExtension(exportFilePath);
            OleDbConnectionStringBuilder dbCnnStringBuilder = new OleDbConnectionStringBuilder();

            dbCnnStringBuilder.DataSource = exportFilePath;
            IDbDriver db = dbFactory.CreateDatabaseObject(dbCnnStringBuilder);

            GlobalizationDataSet ds = instance.ExtractResourcesToDataSet(sourceCulture, targetCulture, translator);

            instance.WriteResourcesToDatabase(ds, db);
        }
예제 #4
0
        void WriteResourcesToDatabase(GlobalizationDataSet ds, IDbDriver db)
        {
            AppendToLog("Please wait...");

            List <TableColumn> columns = new List <TableColumn>();

            columns.Add(new TableColumn("AssemblyName", GenericDbColumnType.String, 255, false));
            columns.Add(new TableColumn("ManifestResourceName", GenericDbColumnType.String, 255, false));
            columns.Add(new TableColumn("ResourceName", GenericDbColumnType.String, 255, false));
            columns.Add(new TableColumn("ResourceValue", GenericDbColumnType.StringLong, false));
            columns.Add(new TableColumn("ResourceType", GenericDbColumnType.String, 255, false));
            columns.Add(new TableColumn("ResourceVersion", GenericDbColumnType.String, 255, false));
            columns.Add(new TableColumn("SourceCultureName", GenericDbColumnType.String, 255, false));
            columns.Add(new TableColumn("SourceValue", GenericDbColumnType.StringLong, 255, false));

            //columns.Add(new TableColumn("CreationDate", GenericDbColumnType.String, 255, false));

            DbDriverInfo dbInfo = new DbDriverInfo();

            dbInfo.DBName             = "CulturalResources";
            dbInfo.DBCnnStringBuilder = new System.Data.OleDb.OleDbConnectionStringBuilder();
            dbInfo.DBCnnStringBuilder.ConnectionString = db.ConnectionString;
            dbFactory.CreatePhysicalDatabase(dbInfo);
//            db.CreateDatabase("CulturalResources");

            string tableName = ds.CulturalResources.TableName;

            db.CreateTable(tableName, columns);

            StringBuilder sb = new StringBuilder();

            sb.Append("insert into ");
            sb.Append(tableName);
            sb.Append(" (");
            for (int x = 0; x < columns.Count; x++)
            {
                sb.Append(columns[x].Name);
                if (x < columns.Count - 1)
                {
                    sb.Append(", ");
                }
            }
            sb.Append(") values ");
            sb.Append(" (");
            for (int x = 0; x < columns.Count; x++)
            {
                sb.Append("@");
                sb.Append(columns[x].Name);
                if (x < columns.Count - 1)
                {
                    sb.Append(", ");
                }
            }
            sb.Append(")");
            Query insertQuery = db.CreateQuery(sb.ToString());

            for (int x = 0; x < columns.Count; x++)
            {
                insertQuery.Parameters.Add(new QueryParameter("@" + columns[x].Name, DbType.String, columns[x].Name, columns[x].Name));
            }

            sb.Remove(0, sb.Length);

            sb.Append("update [").Append(tableName);
            sb.Append(StringLiterals.RIGHT_SQUARE_BRACKET).Append(StringLiterals.SPACE);
            sb.Append("set").Append(StringLiterals.SPACE);
            for (int x = 0; x < columns.Count; x++)
            {
                sb.Append(StringLiterals.LEFT_SQUARE_BRACKET);
                sb.Append(columns[x].Name);
                sb.Append(StringLiterals.RIGHT_SQUARE_BRACKET);
                sb.Append(StringLiterals.EQUAL);
                sb.Append("@NewValue").Append(StringLiterals.SPACE);
                sb.Append(", ");
            }
            sb.Append("where ");
            for (int x = 0; x < columns.Count; x++)
            {
                sb.Append(columns[x].Name).Append(StringLiterals.SPACE);
                sb.Append(StringLiterals.EQUAL);
                sb.Append("@OldValue");
                if (columns.Count > 1)
                {
                    sb.Append(" and");
                }
            }
            Query updateQuery = db.CreateQuery(sb.ToString());

            for (int x = 0; x < columns.Count; x++)
            {
                updateQuery.Parameters.Add(new QueryParameter("@NewValue", DbType.String, columns[x].Name, columns[x].Name));
                updateQuery.Parameters.Add(new QueryParameter("@OldValue", DbType.String, columns[x].Name, columns[x].Name));
                updateQuery.Parameters[1].SourceVersion = DataRowVersion.Original;
            }


            db.Update(ds.Tables[0], tableName, insertQuery, null);
        }
예제 #5
0
        /// <summary>
        /// CompileSatelliteAssemblies()
        /// </summary>
        /// <param name="targetCulture"></param>
        /// <param name="ds"></param>
        public void CompileSatelliteAssemblies(string targetCulture, GlobalizationDataSet ds)
        {
            //all columns are required except SourceValue

            string tempPath = Path.GetTempPath();

            applicationPath = applicationPath.Trim().ToLowerInvariant();

            if (applicationPath[applicationPath.Length - 1] != Path.DirectorySeparatorChar)
            {
                applicationPath += Path.DirectorySeparatorChar;
            }

            this.AppendToLog(SharedStrings.INSTALLING_LANGUAGE, targetCulture);

            GlobalizationDataSet.CulturalResourcesDataTable cultureTable = ds.CulturalResources;//.SelectFilter("CultureName='" + targetCulture + "'");

            GlobalizationDataSet.CulturalResourcesDataTable assembliesTable = cultureTable.SelectDistinct("AssemblyName");
            foreach (GlobalizationDataSet.CulturalResourcesRow assemblyRow in assembliesTable)
            {
                List <string> resourceList = new List <string>();

                GlobalizationDataSet.CulturalResourcesDataTable resourceTable = ds.CulturalResources.SelectFilter("AssemblyName='" + assemblyRow.AssemblyName + "'").SelectDistinct("ManifestResourceName");
                foreach (GlobalizationDataSet.CulturalResourcesRow resourceRow in resourceTable)
                {
                    string resourceFilename;
                    resourceFilename = resourceRow.ManifestResourceName.Substring(0, resourceRow.ManifestResourceName.Length - "resources".Length);
                    resourceFilename = Path.Combine(tempPath, resourceFilename + targetCulture + ".resources");

                    try
                    {
                        using (ResourceWriter writer = new ResourceWriter(resourceFilename))
                        {
                            GlobalizationDataSet.CulturalResourcesDataTable itemTable = ds.CulturalResources.SelectFilter("AssemblyName='" + assemblyRow.AssemblyName + "'").SelectFilter("ManifestResourceName='" + resourceRow.ManifestResourceName + "'");
                            foreach (GlobalizationDataSet.CulturalResourcesRow itemRow in itemTable)
                            {
                                writer.AddResource(itemRow.ResourceName, itemRow.ResourceValue);
                            }
                            writer.Close();
                        }

                        resourceList.Add(resourceFilename);
                    }
                    catch
                    {
                        foreach (string tempResourceFile in resourceList)
                        {
                            try
                            {
                                File.Delete(tempResourceFile);
                            }
                            catch
                            {
                                //eat
                            }
                        }

                        // kickout
                        throw;
                    }
                }

                string assemblyFilename = targetCulture + Path.DirectorySeparatorChar + assemblyRow.AssemblyName + ".resources.dll";
                assemblyFilename = Path.Combine(applicationPath, assemblyFilename);

                this.AppendToLog("Compiling " + assemblyRow.AssemblyName + " satellite assembly.");

                if (!Directory.Exists(Path.GetDirectoryName(assemblyFilename)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(assemblyFilename));
                }
                else if (File.Exists(assemblyFilename))
                {
                    File.Delete(assemblyFilename);
                }

                CSharpCodeProvider prov = new CSharpCodeProvider();
                CompilerParameters p    = new CompilerParameters();
                p.EmbeddedResources.AddRange(resourceList.ToArray());
                p.GenerateInMemory = false;
                p.OutputAssembly   = assemblyFilename;

                // setup references
                p.ReferencedAssemblies.Add("System.dll");


                Assembly mainAssembly = Assembly.Load(assemblyRow.AssemblyName);

                string version = GetSatelliteContractVersion(mainAssembly).ToString();
                if (version != assemblyRow.ResourceVersion.Trim())
                {
                    // warning! resource mismatch
                }

                string code = "[assembly: System.Reflection.AssemblyVersion(\"" + version + "\")] " +
                              "[assembly: System.Reflection.AssemblyFileVersion(\"" + version + "\")] " +
                              "[assembly: System.Reflection.AssemblyCulture(\"" + targetCulture + "\")]";
                CompilerResults res = prov.CompileAssemblyFromSource(p, code);

                foreach (string tempResourceFile in resourceList)
                {
                    File.Delete(tempResourceFile);
                }

                if (res.Errors.HasErrors)
                {
                    throw new GeneralException(GetString(res.Output));
                }
            }
        }
예제 #6
0
        GlobalizationDataSet ExtractResourcesToDataSet(string sourceCulture, string targetCulture, ITranslator translator)
        {
            int resourceCount = 0;

            GlobalizationDataSet ds = new GlobalizationDataSet();


            List <string> files = GetAssemblyFileList(applicationPath);

            foreach (string file in files)
            {
                int fileResourceCount = 0;

                GlobalizationDataSet.CulturalResourcesRow newrow;

                Assembly resourceAssembly = ResolveResourceAssembly(file, sourceCulture);

                // skip missing satellite assemblies
                if (resourceAssembly == null)
                {
                    continue;
                }

                string[] resourceNames = resourceAssembly.GetManifestResourceNames();
                foreach (string resourceName in resourceNames)
                {
                    this.AppendToLog("Processing resource '{0}'", resourceName);

                    string manifestResourceName = resourceName;

                    // trim off any culture specification from satellite manifest resource name
                    if (manifestResourceName.EndsWith("." + sourceCulture + ".resources", true, CultureInfo.InvariantCulture))
                    {
                        manifestResourceName  = manifestResourceName.Substring(0, manifestResourceName.Length - (1 + sourceCulture.Length + ".resources".Length));
                        manifestResourceName += ".resources";
                    }

                    if (manifestResourceName.EndsWith(".resources"))
                    {
                        using (ResourceReader reader = new ResourceReader(resourceAssembly.GetManifestResourceStream(resourceName)))
                        {
                            bool isSharedStrings = manifestResourceName.EndsWith("SharedStrings.resources");

                            IDictionaryEnumerator enumerator = reader.GetEnumerator();
                            while (enumerator.MoveNext())
                            {
                                if (isSharedStrings || IsTextResource(enumerator.Entry))
                                {
                                    fileResourceCount++;

                                    string assemblyName = resourceAssembly.GetName().Name;

                                    // discard any .resource suffix on the end (satellite assemblies)
                                    if (assemblyName.EndsWith(".resources", true, CultureInfo.InvariantCulture))
                                    {
                                        assemblyName = assemblyName.Substring(0, assemblyName.Length - ".resources".Length);
                                    }

                                    string sourceText = enumerator.Value.ToString();

                                    this.AppendToLog(SharedStrings.TRANSLATING, sourceText);

                                    string translatedText = translator.Translate(sourceCulture, targetCulture, sourceText);


                                    newrow = ds.CulturalResources.NewCulturalResourcesRow();
                                    newrow.AssemblyName         = assemblyName;
                                    newrow.ResourceVersion      = this.GetSatelliteContractVersion(resourceAssembly).ToString();
                                    newrow.ManifestResourceName = manifestResourceName;
                                    newrow.ResourceName         = enumerator.Key.ToString();
                                    newrow.ResourceValue        = translatedText;
                                    newrow.ResourceType         = enumerator.Value.GetType().FullName;
                                    newrow.SourceCultureName    = sourceCulture;
                                    newrow.SourceValue          = sourceText;

                                    ds.CulturalResources.AddCulturalResourcesRow(newrow);
                                }
                            }
                        }
                    }
                    else
                    {
                        // embedded resource does not end with .resources
                        continue;
                    }
                }


                this.AppendToLog(SharedStrings.EXTRACTION_COUNT, file, fileResourceCount);
                resourceCount += fileResourceCount;
            }

            this.AppendToLog(SharedStrings.EXTRACTION_COMPLETE, resourceCount);

            return(ds);
        }