I'm trying to globalize an mvc application (french and english), here's what I did:
1) Created an App_LocalResources folder and added 2 files:
Resources.resx (will contain french) and
Resources.en.resx (will contain english).
Then I added one resource in each file with the same name (just to test) and modified the access modifier to Public (for each file)
2) Specified the french culture in global.asax
protected void Session_Start()
{
string userLanguage = Request.UserLanguages[0];
string cultureName = CultureHelper.GetImplementedCulture(userLanguage);
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultureName);
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
HRP.App_LocalResources.Resources.Culture = Thread.CurrentThread.CurrentCulture;
}
CultureHelper is a helper I found online, it determines the correct name of the culture (credits to the author): http://pastebin.com/JkhjJg4N
3) Added a test string to display in my view:
<p><span class="Title">#HRP.App_LocalResources.Resources.EmployeesTitle</span></p>
And the exception is:
[MissingManifestResourceException: Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "HRP.App_LocalResources.Resources.resources" was correctly embedded or linked into assembly "HRP" at compile time, or that all the satellite assemblies required are loadable and fully signed.]
Please help.
I solved it thanks to a friend's tip:
the problem was that the .resx files were builded as Content so all I needed to do was:
Right click on your ResourceFile -> Properties -> Change the "Build Action" property from "Content" to "Embedded Resource"
I have done a small example showing how your code should work out.
Set the build action of the resources to embedded resource
Enter custom tool PublicResXFileCodeGenerator
Afterwards your resources will be available within your code
razor file
<p>#NAMESPACE.App_LocalResources.YOUR_RESOURCE_FILE.RESOURCE_NAME</p>
I have used another custom tool as K. Scott Allen stated
Resx Files Outside Of Special Resource Directories resx properties in MVC
If you add a resx file to any other folder in an MVC project or
class library, the resx is automatically set to be embedded into the
project’s output assembly - this is good. The IDE also assigns the
resx a custom tool of ResxCodeFileGenerator to generate a strongly
typed wrapper - this is good. The generated class is internal by
default – this is bad. The assembly created for a view (by ASP.NET)
won’t be able to use the internal class because it is in a different
assembly – the project assembly compiled in Visual Studio.
So if you add your resource files to the special folders - anything goes right. If you choose to place it on another path it would fail (unless you use the custom tool).
Related
I am using a custom theme and load it as .tssp file in my Program.cs at the moment (which works):
ThemeResolutionService.LoadPackageFile("data\\myThemeName.tssp");
But instead I want to load the .tssp file as embedded resource. Therefore I added the file as resource file under Resources.resx and put the following method in my Program.cs:
ThemeResolutionService.LoadPackageResource("myProject.Properties.Resources.myThemeName");
But the following error occurs:
Specified resource does not exist in the provided assembly.
The path within quotation marks should be correct as the IDE finds it when I remove the quotation marks. I also use different images as embedded resource under the same ressource path which works.
Is this Telerik method bugged or am I doing something wrong?
The ThemeResolutionService class exposes two static methods that allow you to load a theme package:
LoadPackageResource: This method loads a theme package file that is contained in the project as an EmbeddedResource. This is the preferable method for loading a theme package since the resource path to the package is not changed when the application is deployed. The path construction is DefaultProjectNamespace.ThemeFolder.ThemePackageFile. The ThemeFolder part should only be used if the package is contained in a folder under the main project directory and if the project programming language is C#. In VB.NET project you do not need to include ThemeFolder part even if the package file is contained in a folder.
ThemeResolutionService.LoadPackageResource("SamplesCS.CustomTheme.tssp");
public RadForm1()
{
InitializeComponent();
ThemeResolutionService.LoadPackageResource("CustomThemeResourcePackage.Resources.FluentDarkModified.tssp");
ThemeResolutionService.ApplicationThemeName = "FluentDarkModified";
}
And the result:
LoadPackageFile: This method loads a file from a specified directory on the system. Depending on how the directory is defined (full or relative), the path to the package may change when the application is deployed on another machine.
ThemeResolutionService.LoadPackageFile(#"C:\CustomTheme.tssp");
I'm trying to use the ResourceManager in a C# class, but don't know what to substitute for the basename when creating a new instance of the ResourceManager class.
I have a separate project that contains the resource files which is referenced by my project above named as the following:
Resources.resx
Resources.es-ES.resx
If I have a code snippet as follows, what should I substitute for the basename when I want to use the English or Spanish version of the resources?
ResourceManager RM = new ResourceManager(basename,Assembly.GetExecutingAssembly());
I tried the approach suggested by Tom, but am getting the infamous error
Could not find any resources appropriate for the specified culture or the neutral culture.
My solution has two projects where project YeagerTech is a web application and project YeagerTechResources contains all the resources. Project YeagerTech has a reference to YeagerTechResources.
I have the following syntax when creating my Resource Manager:
ResourceManager RM = new ResourceManager("YeagerTechResources.Resources",
Assembly.GetExecutingAssembly());
Obviously, it's incorrect.
The resource files in the YeagerTechResources project have their BuildAction set to Embedded Resource.
The name of my resource files are: Resources.resx and Resources.es-ES.resx.
If someone can simply tell me the exact syntax to use based on my project and resource file names when instantiating the resource manager, I would greatly appreciate it...
I've done everything I can think of to resolve this and can't....
This is my last attempt to resolve it here...
ResourceManager RM = new ResourceManager("YeagerTechResources.Resources", Assembly.GetExecutingAssembly());
sb.Append(RM.GetString("RegisterThanks"));
I am getting the following error after executing the code above:
Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "YeagerTechResources.Resources.resources" was correctly embedded or linked into assembly "YeagerTech" at compile time, or that all the satellite assemblies required are loadable and fully signed.
I am able to use the resources in the HTML markup with absolutely no issues, but when coming to the C# code in the Controller, I keep on getting the above error.
Any help for the exact syntax I need based on my projects would be greatly appreciated.
There's surprisingly simple way of reading resource by string:
ResourceNamespace.ResxFileName.ResourceManager.GetString("ResourceKey")
It's clean and elegant solution for reading resources by keys where "dot notation" cannot be used (for instance when resource key is persisted in the database).
The quick and dirty way to check what string you need it to look at the generated .resources files.
Your .resources are generated in the resources projects obj/Debug directory. (if not right click on .resx file in solution explorer and hit 'Run Custom Tool' to generate the .resources files)
Navigate to this directory and have a look at the filenames. You should see a file ending in XYZ.resources. Copy that filename and remove the trailing .resources and that is the file you should be loading.
For example in my obj/Bin directory I have the file:
MyLocalisation.Properties.Resources.resources
If the resource files are in the same Class library/Application I would use the following C#
ResourceManager RM = new ResourceManager("MyLocalisation.Properties.Resources", Assembly.GetExecutingAssembly());
However, as it sounds like you are using the resources file from a separate Class library/Application you probably want
Assembly localisationAssembly = Assembly.Load("MyLocalisation");
ResourceManager RM = new ResourceManager("MyLocalisation.Properties.Resources", localisationAssembly);
This SO answer might help in this case.
If the main project already references the resource project, then you could just explicitly work with your generated-resource class in your code, and access its ResourceManager from that. Hence, something along the lines of:
ResourceManager resMan = YeagerTechResources.Resources.ResourceManager;
// then, you could go on working with that
ResourceSet resourceSet = resMan.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
// ...
According to the MSDN documentation here, The basename argument specifies "The root name of the resource file without its extension but including any fully qualified namespace name. For example, the root name for the resource file named "MyApplication.MyResource.en-US.resources" is "MyApplication.MyResource"."
The ResourceManager will automatically try to retrieve the values for the current UI culture.
If you want to use a specific language, you'll need to set the current UI culture to the language you wish to use.
in priciple it's the same idea as #Landeeyos. anyhow, expanding on that response:
a bit late to the party but here are my two cents:
scenario:
I have a unique case of adding some (roughly 28 text files) predefined, template files with my WPF application. So, the idea is that everytime this app is to be installed, these template, text files will be readily available for usage. anyhow, what I did was that made a seperate library to hold the files by adding a resource.resx. Then I added all those files to this resource file (if you double click a .resx file, its designer gets opened in visual studio). I had set the Access Modifier to public for all. Also, each file was marked as an embedded resource via the Build Action of each text file (you can get that by looking at its properties). let's call this bibliothek1.dll
i referenced this above library (bibliothek1.dll) in another library (call it bibliothek2.dll) and then consumed this second library in mf wpf app.
actual fun:
// embedded resource file name <i>with out extension</i>(this is vital!)
string fileWithoutExt = Path.GetFileNameWithoutExtension(fileName);
// is required in the next step
// without specifying the culture
string wildFile = IamAResourceFile.ResourceManager.GetString(fileWithoutExt);
Console.Write(wildFile);
// with culture
string culturedFile = IamAResourceFile.ResourceManager.GetString(fileWithoutExt, CultureInfo.InvariantCulture);
Console.Write(culturedFile);
sample:
checkout 'testingresourcefilesusage' # https://github.com/Natsikap/samples.git
I hope it helps someone, some day, somewhere!
I went through a similar issue.
If you consider your "YeagerTechResources.Resources", it means that your Resources.resx is at the root folder of your project.
Be careful to include the full path eg : "project\subfolder(s)\file[.resx]" to the ResourceManager constructor.
I'm trying to add globalization support to my C# application.
According to MSDN, there should be one embedded resource file for neutral culture and satellite DLLs with resource files for other cultures.
I've created 2 satellite DLLs without any problems and got my app to automatically load right one using ResourceManager. But I can't embed default neutral culture resource file into my executable. When I remove all satellite DLLs or set culture to some culture I don't have satellite DLL for, I get exception "Could not find any resources appropriate for the specified culture or the neutral culture." when application attempts to create ResourceManager.
It looks like VS 2008 does not include my .resource file into main assembly. I've tried different ways to get resource file embedded: compiling it by resgen.exe from text file and adding it to the project; changing its name to add second .resources extension; creating .resx file with same name; etc. And I still don't see the way to get resource file embedded and used by ResourceManager - I'm having same exception.
What is the right way to add default neutral culture resource file to application in VS 2008 ?
Ok, I've solved this problem by little 'hack'.
I've compiled resource file using resgen.exe, as described in MSDN, then added it to the project, renamed it to "resources" and changed build action from "None" to "Embedded resource".
Looks like VS 2008 adds "." as prefix to resource file name. So if u will name your resource file ".resources" it won't work cuz actually VS will name it "..resources".
in .Net the resource files are using the .resx extension. To have it 'embedded' in your project, make sure the "Custom Tool" in the properties page of the .resx file is using the "ResXCodeGenerator"
Hope this helps,
Had a basic WinForm question: By default a resx file is created for every form or user control (along with the designer.cs). This resx works fine for all the controls and the text added to the controls via the UI.
I was wondering if I could use the same resx to add strings which have to be used programmatically and based on conditions, attached to the controls? Will the resx get overridden in any case and this custom strings be removed?
What is the best practice to follow in this case?
There's a strange problem with the string resources in the Resources.resx file. There's no obvious way that I ever discovered how to create a new resource table for another language with the IDE. It can be done by hand though. Follow these steps:
Project + Properties, Resource tab,
add the strings you want to use in
your program
Start Windows Explorer and navigate
to your project's Properties folder
Copy and paste the Resources.resx
file
Rename it to the culture you want to
use. For example:
Resources.fr-FR.resx
Back to VS, click the Show All Files
icon in the Solution Explorer window
Expand the Properties node, the new
resource file should be visible
Right-click it and select "Include
in project"
Select it, in the Properties window
set Custom Tool =
"ResXFileCodeGenerator"
Verify that Build Action is set to
"Embedded Resource"
Build your program. You should get a new folder in your project's bin\Debug directory with the satellite assembly, named projectname.resources.dll. This satellite assembly contains both the localized strings and any localized resource from the forms. Test that it works with code like this:
public Form1() {
System.Threading.Thread.CurrentThread.CurrentUICulture =
System.Globalization.CultureInfo.GetCultureInfo("fr-FR");
InitializeComponent();
textBox1.Text = Properties.Resources.String1;
}
The auto-generated ones get overwritten (I'm using 2005), so no I would not use the same file. I would suggest creating a separate area in your project for .resx files like this. Usually I create one .resx per form, matching the name, of course.
Edit: Here is a more detailed answer I gave recently to organize the file/folder structure when localizing your app with .resx files.
Or you can try this:
Add this code in main();
using System.Resources;
ResXResourceWriter rw = new ResXResourceWriter("Resources.de-DE.resx");
rw.AddResource("String1", "de");
rw.Close();
...
repeat for more files
Go to the bin directory and move the XML (resx) file(s) to perhaps the property folder.
Go to Step 5-9 above in nobugz post.
I have added multi-language using the short article below.
When you add for example German language you will have these files:
formMain.resx
formMain.de-DE.resx
formMain.Designer.cs
formMain.cs
In first file you will have resources for neutral language, like strings, images, ..
So now you need to add also resources for strings used in code. Add a new resource file and name it formMain.Strings.resx
Then i will enter name, value pair for every string that should be translated. When you add resource file then it is automatically typed because another file with name formMain.Strings.Designer.cs is automatically regenerated on every close of resx designer.
Add another resource with name formMain.Strings.de-DE.resx. Add the same Name key's from previous resource, and just change the Value with coresponding german words. Now to access created resource from the source it will be like this.
MessageBox.Show(formMain_Strings.SameStringName);
However, I have changed my to Thai language. Everything works fine when I run my app in VS.
However, as soon as I add a setup project and install on the clients machine it won't change the language to Thai and just keeps to the default language.
So I have added the resource files and the th-TH dll to the project setup. And I still get the same problem.
Packaging file 'Lang.Strings.resx'...
Packaging file 'MultiLanguage.resources.dll'...
Packaging file 'MultiLanguage.exe'...
Packaging file 'Lang.Strings.th-TH.resx'...
As everything works fine when running in visual studio. Is there something I need to do to get it to run once its been installed. All the properties for each of the file I have keep the default.
Many thanks,
=========
static void Main()
{
System.Threading.Thread.CurrentThread.CurrentUICulture =
new System.Globalization.CultureInfo("th-TH");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
I found the answer
Click the Setup Project in Solution Explorer and then click Add\Project Output\ . From dialog select the project for which you want to include localization (satelite) assemblies and then select Localized resources.
After the installation in the folder that I install to, I have the th-TH folder which includes the satellite assembly.
Thanks,
Try adding in this at the app's startup (if it's not there):
Thread.CurrentThread.CurrentUICulture = CultureInfo.CurrentCulture;
Here is a short article discussing some of the options of how to make this work, and options for selecting the locale at runtime.
Edit after comments:
Make sure your satellite assembly is in the appropriate place, and built correctly. From that article I referenced:
"When .NET runtime starts an application it looks for a possible satellite assembly file. A satellite assembly file is a resource only assembly file that has .resources.dll extension instead of .exe ir .dll (if the main assembly is a library). Satellite assembly files always locate on a language specific sub directory of the applciation's main directory. If application file is Converter.exe then the Japanese satellite assembly file is ja\Converter.resources.dll."
There are a few things that you should check here. Check the name of the assembly. Also, make sure it's in the proper location. In your case, it should be in a th-TH subdirectory with the appropriate name under your executable. If it's there, it should be found and used properly.
Here is another good source of information about this topic.