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.
Related
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
Problem
I am trying to implement a system in my program to switch language. I found out that i can use CultureInfo and ResourceManager to achieve that. I built up this code after a couple of hours having problem with the resource not found, finally i found and answer here on stackoverflow and i arranged the following code:
CultureInfo culture;
culture = CultureInfo.CreateSpecificCulture("it-IT");
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
Assembly resourceAssembly = Assembly.Load("MY ASSEMBLY NAME");
ResourceManager manager = Properties.Resources.ResourceManager;
string[] manifests = resourceAssembly.GetManifestResourceNames();
string manifest = manifests[0].Replace(".resources", string.Empty);
manager = new ResourceManager(manifest, resourceAssembly);
string greeting = String.Format("The current culture is {0}.\n{1}",
Thread.CurrentThread.CurrentUICulture.Name,
manager.GetString("HelloString"));
MessageBox.Show(greeting);
Since this is a really big program with a lot of pages, windows and usercontrols, i need to access the language from a lot of different files.
The code i posted above, should look into the root of my Solution and look for a file named it-IT.resx. it says that the current culture is it-IT but it doesn't write the value of HelloString, but it doesn't give any error so it's definitely a problem with Resource Manager but i don't know why it doesn't crash saying it doesn't find the resource. I am sure that inside the resx file there is a value called HelloString.
Is there a reason to load the types dynamically with Assembly.Load()? It is asking for trouble. Another way would be adding a static assembly reference.
This approach has many advantages:
the resource names can be accesses as property names, it is comfortable
the calling code uses always existing resources
in the calling code there are no references to non-existing resources
To be able to use resources from another assembly the resource access modifier needs to be set to public:
If the resources file is named RText, as in the example above, the value of a resource can retrieved from another project with:
var val = ProjectWithResourcesNamespace.RText.HelloString;
Trying to access a specific localised resource file such as WebResource.en.us which is located in my App_GlobalResources folder using the following code:
string resData = GetGlobalResourceObject("WebResource.en.us", "SomeResource").ToString();
but this keeps giving me the error below:
Could not find any resources appropriate for the specified culture or
the neutral culture. Make sure "Resources.WebResource.en-us.resources"
was correctly embedded or linked into assembly
"App_GlobalResources.bpqqrnv4" at compile time.
Any ideas ?
I tried the following Code
Page_Load(....)
{
/// note - i did NOT mention the culture when accesing my resourceFiles
Debug.WriteLineIf(
GetGlobalResourceObject("WebResource", "someResource")!=null,
GetGlobalResourceObject("WebResource", "someResource").ToString());
/// accessing a culture specific resource without changing Page Culture
CultureInfo yourCI = new CultureInfo("en-US");
Debug.WriteLine(
HttpContext.GetGlobalResourceObject(
"WebResource",
"someResource",
yourCI).ToString());
}
my Page directive
<% Page Culture="en-US" UICulture="en-US" ..... %>
My App_GlobalResources folder contains two files
WebResource.resx
WebResource.en-US.resx
Using this settings and code - my Debugger printed the value without any problems.
When removing WebResource.resx (my default ressource file) the same code throws an exception.
I would assume that you have to add a default resx file and remove the explicit culture notation in GetGlobalResourceObject(..., ..).
update: added some code to access specific resx culture file
see also MSDN
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