I try to make my application multilingual. For this I have created two resource files:
Resource.de-DE.resx
Resource.en-US.resx
Both are set to public and contain the same keyword and translation. I can also access the keywords in the backend -> see image.
Unfortunately my program does not load the files when I change the language.
My steps:
set the default Resources.resx to public.
copied this file twice
renamed file to (see above)
keywords+translation entered and checked
project created again
included on the Button-Content by:
<Button x:Name="Home_Button" Style="{DynamicResource Menuebutton}" Content="{x:Static p:Resources.Menue_home}" Click="Home_Click"/>
access of the object set by:
xmlns:p="clr-namespace:VS_Launcher.Properties"
include in the MainWindow.xaml.cs by:
CultureInfo myCultureInfo = new CultureInfo("en-US");
CultureInfo.DefaultThreadCurrentCulture = myCultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = myCultureInfo;
Thread.CurrentThread.CurrentCulture = myCultureInfo;
Thread.CurrentThread.CurrentUICulture = myCultureInfo;
In another project I did the same thing. But there the solution worked. What is my error?
Resource Files
Directory structure
Are you sure the file names of alternative language resources (excluding culture-locale) are identical with default one? If the default one is Resources.resx, alternative ones must be like:
Resources.de-DE.resx
Resources.en-US.resx
I'm trying to localize my app, I've created several Resources.[culture].resx but program ignores them all and always loads default Resources.resx.
I have this code in App construtor (and in OnStartup event)
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
My resources are called Resources.en-US.resx, Resources.cs-CZ.resx and Resources.resx
Code in WPF is
xmlns:p="clr-namespace:WpfThermalLabelEditorApp.Properties"
Title="{x:Static p:Resources.Title}"
This should load en-US localization but it doesn't. Loads default. I tried to delete AssemblyInfo and even putting <UICulture> tags into csproj
Not sure if this is the answer, but FWIW: I always set both CurrentCulture and CurrentUICulture like so;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(languageAbbreviation);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(languageAbbreviation);
And be sure to double check the abbreviation you use is the same for all references. That should fix your problem.
EDIT1: Make sure your resource files are set to public, the access modifier should be somewhere near the top when you're viewing the resource file. Double check this says public and rebuild. Also, to call your resource file key do so like this:
<TextBlock xmlns:local="clr-namespace:WpfApplication2" Text="{x:Static local:Resource1.myname}"/>
Where...
"WpfApplication2" is replaced with your project name
"Resource1" is replace with the name of your resource file (without culture id, e.g. for your case it would just be Resources)
"myname" is replaced with the key you want
I came across the same problem, the solution was to clean and rebuild the project. No need to create new project.
Well, after 2 days I've solved my problem. My solution was correct. I have no idea what was wrong but I've created a new project, moved all my data to a new one and suddenly everything worked as a charm.
Here is a few usefull links:
WPF localization and globalization
Localization tutorial
Localization tutorial 2
I am having issues with the already mentioned NuGet package. I have created a folder named Resources, where I have inserted the following files:
Strings.en.resx
Strings.sk.resx (my default language)
I am using Prism, where my MainWindow (plain container) is located under root window and all the other pages are located under Views folder.
I have added the following to my MainPage view:
xmlns:lex="http://wpflocalizeextension.codeplex.com"
lex:LocalizeDictionary.DesignCulture="en"
lex:LocalizeDictionary.OutputMissingKeys="True"
lex:ResxLocalizationProvider.DefaultAssembly="eManagement"
lex:ResxLocalizationProvider.DefaultDictionary="Strings"
and here is an example of a TextBlock:
<TextBlock Text="{lex:Loc Key=Greetings}"/>
The problem is, that if I place the Strings.resx file in the root folder, the control is translated without a problem. But when I want to keep my translations under a Resources folder (meaning that english translation will be under Resources/Strings.en.resx), it suddenly does not work.
What do I need to change in order to make it work?
I was missing the following line:
lex:InheritingResxLocalizationProvider.DefaultDictionary="Strings"
Plus in the code-behind I had to manually set the culture as otherwise my culture was always invariant
#region Language Selection
var culture = new CultureInfo(Properties.Settings.Default.LanguageSettings);
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
WPFLocalizeExtension.Engine.LocalizeDictionary.Instance.Culture = Thread.CurrentThread.CurrentCulture;
#endregion
This has solved the problem.
I'm having a few troubles with getting resources dynamically as I get "Missing manifest" errors. I looked up a few possible causes and did what was written there but so far nothing worked.
Currently I have this situation:
The resx file I'm trying to access is: "Resources/Messages.resx" (thus in a nonstandard folder.
The code I'm using is this:
ResourceManager resourceManager = new ResourceManager("Resources.Messages", this.GetType().Assembly);
resourceManager.GetString("ResourceText" + MessageType + "Subject")
with messageType being a string. On the second line I get the error message.
The status of the resource file is this:
Build Aciton: Embedded Resource
Custom Tool: PublicResXFileCodeGenerator
Access Modifier Public
The resfile has 2 variants: Messages.res and Messages.de.resx with the same names for each row and also the same general properties (the "GetString" also definitively tries to access the correct name).
So my question is what I'm doing wrong there and what can I do to correct this problem?
Found the problem. For new ResourceManager not only the namespace of the resourcesfiles has to be given but also the default namespace. Thus if the application has a default namespace of: MyApplication.MyServerApp then instead of "Resources.Messages" one must put in: "MyApplication.MyServerApp.Resources.Messages" leading to the following functioning sourcecode:
ResourceManager resourceManager = new ResourceManager("MyApplication.MyServerApp.Resources.Messages", this.GetType().Assembly);
resourceManager.GetString("ResourceText" + MessageType + "Subject")
I have created an assembly and later renamed it.
Then I started getting runtime errors when calling:
toolsMenuName = resourceManager.GetString(resourceName);
The resourceName variable is "enTools" at runtime.
Could not find any resources
appropriate for the specified culture
or the neutral culture. Make sure
"Jfc.TFSAddIn.CommandBar.resources"
was correctly embedded or linked into
assembly "Jfc.TFSAddIn" at compile
time, or that all the satellite
assemblies required are loadable and
fully signed.
The code:
string resourceName;
ResourceManager resourceManager = new ResourceManager("Jfc.TFSAddIn.CommandBar", Assembly.GetExecutingAssembly());
CultureInfo cultureInfo = new CultureInfo(_applicationObject.LocaleID);
if(cultureInfo.TwoLetterISOLanguageName == "zh")
{
CultureInfo parentCultureInfo = cultureInfo.Parent;
resourceName = String.Concat(parentCultureInfo.Name, "Tools");
}
else
{
resourceName = String.Concat(cultureInfo.TwoLetterISOLanguageName, "Tools");
}
toolsMenuName = resourceManager.GetString(resourceName); // EXCEPTION IS HERE
I can see the file CommandBar.resx included in the project, I can open it and can see the "enTools" string there. It seems that either resources are not included into assembly or resource are included but .NET cannot resolve the name.
I think simpler solution would be to create separate resources file for each language.
As far as this case is concerned check if the assembly containing resources has the default namespace set to the same text (Project->Properties->Default namespace; in VS)
Check as well if the resx file has a property BuildAction set to "Embedded resource"
Sounds similar to an issue we had. The namespace was incorrect in the resource file's designer. I fixed it by manually re-running the custom-tool on the resx file.
Right click your.resx, and click Run Custom Tool.
I'm sure you've already got the answer, but just in case:
You can view your ManifestResourceName by calling
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames()
Check that Manifest name and your name in GetString() calling are identical.
Also, be sure you have correct namespace in designer.resx file:
namespace Jfc.TFSAddIn {
...
global::System.Resources.ResourceManager temp =
new global::System.Resources.ResourceManager(
"Jfc.TFSAddIn.CommandBar", typeof(CommandBar).Assembly);
...
}
Open resx file properties: "Build Action" should be "Embedded Resource"
For me, the source of the problem was naming the rex files starting with a number:
20160216_tranlation.resx
I had to add an underscore _ before the resx file name when calling GetGlobalResourceObject:
public static string getResource(string key)
{
return HttpContext.GetGlobalResourceObject("_20160216_tranlation", key).ToString();
}
I corrected the namespace in designer file (Resources.Designer.cs) in ResourceManager static property & it worked for me.
See the code below:
[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("XYZAssembly.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
I added a temporary class within my Form.cs while (testing || debugging) that caused this exception to be thrown. The Form.resx file (Name || Resource ID) was modified to the temporary class name instead of the Form class name. This caused the issue for me. I (corrected || alleviated) this by creating a separate file for my temporary class in the project.
One Solution is to change the property of resx file from content to Embedded Resource and Build it.Sure this time u vil get
I have encountered this issue in Xamarin.Forms, when I tried to the rename the project, the resources could not be loaded anymore with the same stated error text.
To fix the problem I had to modify the .csproj by a text editor, and change the logical name of the embedded resource.
<EmbeddedResource Include="Localization\TextResources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>TextResources.Designer.cs</LastGenOutput>
<LogicalName>YourNewNamespaceName.TextResources.resources</LogicalName>
<SubType>Designer</SubType>
</EmbeddedResource>
Also watch out for the autogenerated class when you rebuild it, the namespace stated in there might change.
Hope it helps someone that went into the same situation.
Got this error when I added a class ABOVE the partial form class in my Windows forms app.
It went away when I moved the class BELOW the partial form class.
This answer solved the problem for me! GetGlobalResourceObject