2

For my translations I'm making use of embedded .resx files. Some of these translations I wish to override with another .resx file, which is not embedded (ex. ~/App_Localresources/translations.en-US.resx).

The purpose of this is that after the application is compiled and deployed, a user could change the .resx file manually to override some of the embedded translations.

Is there a way to use the normal ResourceManager for this? (in .NET 4)

Thank you for tips

M4N
  • 94,805
  • 45
  • 217
  • 260
peter
  • 83
  • 1
  • 7

1 Answers1

3
    public class ResxResourceManager : System.Resources.ResourceManager {
        public ResxResourceManager(string baseName, string resourceDir) {
            Type[] paramTypes = new Type[] { typeof(string), typeof(string), typeof(Type) };
            object[] paramValues = new object[] { baseName, resourceDir, typeof(ResXResourceSet) }; 

            Type baseType = GetType().BaseType;

            ConstructorInfo ci = baseType.GetConstructor(
                BindingFlags.Instance | BindingFlags.NonPublic,
                null, paramTypes, null);

            ci.Invoke(this, paramValues);
        }

    protected override string GetResourceFileName(CultureInfo culture) {
        string resourceFileName = base.GetResourceFileName(culture);
        return resourceFileName.Replace(".resources", ".resx");
    }
}
peter
  • 83
  • 1
  • 7
  • 5
    how do you use this? can you give an example? – Jaguar Jan 28 '11 at 13:04
  • Thanks, i was looking for this for a while. Example of use `ResxResourceManager rm = new ResxResourceManager("NameOfResourceFile", "PathToResourceDir"); rm.GetString(name,System.Globalization.CultureInfo.GetCultureInfo("en-US"));` – tsukimi Nov 06 '12 at 08:47
  • This code seems very dangerous. It is using refelction to call a second base constructor on itself. And that constructor is public (and has been since .NET 2: maybe it wasn't in .NET 1 or 1.1?). – Richard Jul 04 '14 at 14:56