[C#] Localized Ressources

Started by
1 comment, last by Plerion 13 years, 11 months ago
Hey! In my project im using a localized stringtable. The problem now is that it creates folders like de-de and en-us in the applicationdirectory. Basically id like to have those folders in another folder called data where all my other files are located. Is there a way to tell the compiler that it should create those folders in another directory relative to the application? Greetings Plerion
Advertisement
Hi,

There are two problems you are facing: first you want to control where the resource assemblies are saved. Second, you need to tell the application from where to load the resources.

The latter is easy to achieve: you can add an app.config file to your project and define in there which paths the .NET runtime should probe in order to find resource assemblies. For example, if you want to move the de-de, en-us etc. folders to a subfolder Content below the application directory, you would add the following configuration in app.config:

<?xml version="1.0" encoding="utf-8" ?><configuration>  <runtime>    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">      <probing privatePath="Content"/> <!-- also search Content folder for assemblies -->    </assemblyBinding>  </runtime></configuration>


You could add a post-build event to your project that moves the assemblies from the default directory (below the app directory) to your desired directory. Paired with the configuration above you'd be done.


If you are familiar with MSBuild, the next paragraphs might be interesting for you. Technically, it is possible to control where the resource assemblies are created. I don't know of any switch in the Visual Studio user interface to modify this behavior. However, the .NET SDK itself support the necessary options.

So, if you compile everything by hand or if you use the msbuild system (which is preferable) you can achieve what you want without copying files in the post build event. The next paragraphs describe where the necessary setting is buried in the standard msbuild system.

The default build process is mainly defined in the .targets file in the framework directory. For C# projects, Microsoft.Common.targets and Microsoft.CSharp.targets are the files of interest. Resource compilation is controlled in Microsoft.Common.targets.

Satellite assemblies (that is, localized resource assemblies) are generated by first compiling resx files into .resource files. This is done in the GenerateResource task of the CoreResGen target. The next step is to link the resource files into assemblies. This is done using the AL task in the GenerateSatelliteAssemblies target. The OutputAssembly attribute of the AL task defines where the satellite assemblies should be saved. Its default value is $(IntermediateOutputPath)%(Culture)\$(TargetName).resources.dll.

Andre Loker | Personal blog on .NET
Hey!

Thanks, that helped a lot. I added the line to the app.config and now it loads it from Data. About copying thats not a problem, ill just copy it when i have another release.

Greetings
plerion

This topic is closed to new replies.

Advertisement