.Net resource files in separate language folders - c#

Is it possible to have resource files in separate folders for each language as shown below? I'd like all my English files in an "en" folder and all my Spanish files in an "es" folder. I have tried using the resources with both files in the main Resources folder (no subfolders), and it works correctly. However, when I have the files in separate "en" and "es" folders like in the image, I always get the English text even if my browser is set to display Spanish.
I've included file properties for each resource file along with the code I'm using to retrieve the value
var value = Language.Resources.ResourceManager.GetString(resourceName.Trim());
The auto-generated Resources.Designer.cs file includes the following code for the ResourceManager property. Is that first parameter "ClientInterface.Resources.en.Resources" the reason why it's always giving me English?
/// <summary>
/// Returns the cached ResourceManager instance used by this class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ClientInterface.Resources.en.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}

I see it like you need to write single Resource class manually and Custom Tool for Resources should be deleted.
And you need to write your own logic for receiving right ResourceManager.
This small sample without optimizations, but you can start with this.
The auto-generated Resources.Designer.cs file includes the following
code for the ResourceManager property. Is that first parameter
"ClientInterface.Resources.en.Resources" the reason why it's always
giving me English?
It's mostly because in autogenerated resource it is loading only english resource, but you need to write your logic, how to load your resource.
Example NamespaceToResources.Language.Resources
Or NamespaceToResources.Culture.Resources
In the next sample I'm loading Resource for current Culture
/// <summary>
/// Returns the cached ResourceManager instance used by this class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
return new global::System.Resources.ResourceManager($"ClientInterface.Resources.{Thread.CurrentThread.CurrentUICulture.Name.Replace("-", "_")}.Resource", typeof(Resource).Assembly);
}
}

Related

Source Generator - File path the source code is located

I have Project A that uses a DLL with Source Generator B. When the Source Generator runs, I need to know the file path that A was located.
My Source Generator implements ISourceGenerator. GeneratorExecutionContext doesn't seem to contain anything relevant to file path, as far as I can tell.
Is there any way to do this?
After a lot of mucking about in the properties of GeneratorExecutionContext I finally located the caller's file path somewhere. It's easiest to just put it in an Extension method.
/// <summary>Gets the file path the source generator was called from.</summary>
/// <param name="context">The context of the Generator's Execute method.</param>
/// <returns>The file path the generator was called from.</returns>
public static string GetCallingPath(this GeneratorExecutionContext context)
{
return context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.projectdir", out var result) ? result : null;
}

Using Resource files for things other than language

I am working on an MVC application that is effectively a piece of middleware to provide admin and reporting on an external BPM solution.
The users of the system aren't all for the same business function and may refer to fields as different things (e.g. division A calls a client a Client but division B calls them a Prospect). Is there a way I could have resource files for
Resources.divA.resx
Resources.divB.resx
Resources.divC.resx
From my googling, it appears it can only be used for localization.
You can use the ResourceManager class to help with this.
When you add some resources to a project, the IDE automatically adds a Resources.Designer.cs file to the project to manage the resources. The generated code looks something like this:
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("AssemblyName.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
This uses the ResourceManager constructor that looks up resources based on a combination of a root name and the current locale.
You can write similar code yourself, but compute a root name based on whether the the resources are for division A, B or C. So you would call it something like "AssemblyName.Resources.divA" instead of "AssemblyName.Properties.Resources".
You can then also have specific versions for each other supported language and they will be automatically loaded (if present) for the current culture. This is how it works for the IDE-managed resources.

SharedResourceDictionary: edit resource in Blend

To optimize my application, I create a SharedResourceDictionary. With this, I save about 250 mb of memory at run-time, so it works well.
My problem is in design-time, in Blend 4, I have no more access to my resource. To modify a template (witch is in my resource file), I usually right click on my control and I choose Edit Template --> Edit Current, like in this image:
I need to modify my template that way, it's 100x faster then to go in my resource file and find the good one... I have A LOT of resources...
My SharedResourceDictionary is implemented like this (found on the web):
public class SharedResourceDictionary : ResourceDictionary
{
/// <summary>
/// Internal cache of loaded dictionaries
/// </summary>
public static Dictionary<Uri, ResourceDictionary> _sharedDictionaries =
new Dictionary<Uri, ResourceDictionary>();
/// <summary>
/// Local member of the source uri
/// </summary>
private Uri _sourceUri;
/// <summary>
/// Gets or sets the uniform resource identifier (URI) to load resources from.
/// </summary>
public new Uri Source
{
get
{
if (IsInDesignMode)
return base.Source;
return _sourceUri;
}
set
{
_sourceUri = value;
if (!_sharedDictionaries.ContainsKey(value))
{
// If the dictionary is not yet loaded, load it by setting
// the source of the base class
base.Source = value;
// add it to the cache
_sharedDictionaries.Add(value, this);
}
else
{
// If the dictionary is already loaded, get it from the cache
MergedDictionaries.Add(_sharedDictionaries[value]);
}
}
}
/// <summary>
/// Check if we are in Blend to prevent error
/// </summary>
public bool IsInDesignMode
{
get
{
return
(bool)
DependencyPropertyDescriptor.FromProperty(DesignerProperties.IsInDesignModeProperty,
typeof(DependencyObject)).Metadata.DefaultValue;
}
}
}
Someone have an idea if there is a solution for this? I really want to keep this shared dictionary due to the memory improvement, but I also want to modify my resource easily...
Any clue will be appreciated. Thank you.
EDIT:
Doing this (SharedResourceDictionary), I also have an issue with static resource:
Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.
And when I change them to dynamic resource it works, or if I change the SharedResourceDictionary by a simple ResourceDictionary it also works. The project can compile but I cannot edit the resource without a modification as I said.
Are you compiling for .NET 4?
You might be hitting the bug where it fails to scan your ResourceDictionaries properly.
There's talk that you have to add your ResourceDictionaries at the the App.xaml level (in Application.Resources) level, and add a dummy default style.
This might not work for you as you seem to be adding your resources in your control.
http://blogs.windowsclient.net/rob_relyea/archive/2010/04/26/my-staticresource-reference-worked-differently-with-wpf-4-rc-than-it-does-with-wpf-4-rtm.aspx
Adding a Merged Dictionary to a Merged Dictionary
Trouble referencing a Resource Dictionary that contains a Merged Dictionary
http://connect.microsoft.com/VisualStudio/feedback/details/553528/defaultstylekey-style-not-found-in-inner-mergeddictionaries
Actually it is problematic to use this feature. =(
Maybe in the future things will get better, do not know if will help, but you can try a different implementation:
WPF SharedResourceDictionary
(but do not guarantee it will work)
I gave up using the SharedResourceDictionary because of problems at design time, only when the project is 100% completed I'll be back to use.

The type 'InversionOfControl.IOC' exists in both 'InversionOfControl.dll' and 'InversionOfControl.dll'

I have a project called InversionOfControl. That project has a class called IOC. It looks like this:
/// <summary>
/// This class is used to expose the unity container to the application.
/// </summary>
public class IOC
{
/// <summary>
/// The actual unity container for the app.
/// </summary>
public static IUnityContainer Container { get; set; }
}
All my projects that need to resolve unity injections have a reference to this class. Lately I have started getting this error:
Value cannot be null.
Parameter name: container
on normal resolves:
ITransmitModel transmitModel = IOC.Container.Resolve<ITransmitModel>();
When I try to inspect IOC.Container, the value in the watch window is:
The type 'InversionOfControl.IOC' exists in both 'InversionOfControl.dll' and 'InversionOfControl.dll'
I have looked in my output folder and there is only one InversionOfControl.dll file. (I even cleaned my solution and double checked the folder was empty. I then rebuilt and checked again that there is only one.)
Why does it think there are two dlls called InversionOfControl.dll?
A way to check what module is loaded at run-time is "Debug->Windows->Modules" and check out where the DLL in question is loaded from. Make sure you are not loading 2 assemblies with different versions.
As a next step I would do search for all files with this name to see if there are suspicious copies ("cd /d c:\ & dir /s /b InversionOfControl.dll").

ResourceManager not picking up correct resx file

I am having an issue getting the correct resource file entries and need some help ... here is my scenario
I have a series of projects that are a part of a large reporting solution that follow this format
Plugin.****Report
Reference (folder)
DataAccessLayer (folder)
DataSets (folder)
DataWrappers (folder)
Localization (folder)
*.cs
Where * is the name of the report I am going to generate
The *.cs has an export statement so that I can pick it up using MEF (not sure if this is relevant, but thought it worth mentioning). The namespace in *.cs is CompanyName.DataShaper.Plugin.*Report. Due to the project name, and location of the files, I changed the default namespace for each project to be CompanyName.DataShaper.Plugin.*Report (it was just Plugin.*Report before my change).
Now for the problem .. inside of *.cs I am instantiating a ResourceManager. That looks like this ...
_resourceManager =
new ResourceManager("CompanyName.DataShaper.Plugin.*Report.Localization.*Report",
Assembly.GetExecutingAssembly());
inside my resx designer file I have the following ...
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("CompanyName.DataShaper.Plugin.*Report.Localization.*Report", typeof(*Report).Assembly);
resourceMan = temp;
}
return resourceMan;
}
My call to the resource manager looks like this ...
string resourceString = _resourceManager.GetString(resourceKey, _currrentCultureInfo);
where _currentCultureInfo is a a correct CultureInfo object.
I have 4 different resx files in my Localization folder for each project, (****Report.resx, ****Report.fr-ca.resx, ****Report.en-gb.resx, ****Report.de-de.resx).
When I make the call to the resource manager, I always get the entry from the .resx ... never any of the other language files, regardless of the CultureInfo object I pass into the call. What have I messed up?
--> Quick update to my original question, but this does appear to be MEF related. I instantiated my class the old fashioned way (new *Report()) and made the localization call and it works fine
OK, I figured this out .. I am getting the .resx file because it is truly embedded into the main assembly. The other files are getting built into separate dlls for each language, I then need to copy them into the same folder that I build my aggregate container from, my resource manager then sees all languages.

Categories

Resources