How To Make Multi Language Web and set the value into Resource.resx from database

130 Views Asked by At

I want to translate my Web Page Into Different language , And the value of the word are in the data-base ,

How to Set the resource value from data-base table , every value in different table

1

There are 1 best solutions below

1
On

If i get it right, you want to populate your resources files dynamically from your DataBase. You can achieve that by creating the .resx files in your project, the next step would be add/update its content using the code below:

public static void AddOrUpdateResource(string key, string value)
        {
            var path = @"YourResourceFilePath.resx";
            var resx = new List<DictionaryEntry>();
            using (var reader = new ResXResourceReader(path))
            {
                resx = reader.Cast<DictionaryEntry>().ToList();
                var existingResource = resx.Where(r => r.Key.ToString() == key).FirstOrDefault();
                if (existingResource.Key == null && existingResource.Value == null) // NEW!
                {
                    resx.Add(new DictionaryEntry() { Key = key, Value = value });
                }
                else // MODIFIED RESOURCE!
                {
                    var modifiedResx = new DictionaryEntry() { Key = existingResource.Key, Value = value };
                    resx.Remove(existingResource);  // REMOVING RESOURCE!
                    resx.Add(modifiedResx);  // AND THEN ADDING RESOURCE!
                }
            }
            using (var writer = new ResXResourceWriter(path))
            {
                resx.ForEach(r =>
                {
                    // Again Adding all resource to generate with final items
                    writer.AddResource(r.Key.ToString(), r.Value.ToString());
                });
                writer.Generate();
            }
        }

Then you can call this method and pass the key-value from your DataBase.