I use Properies.Resources.resx file in my project for alternate values with russian language (Resources.ru.resx), but for some reason class Resources returns only defualt values.
I was strugling for two days with that problem and realised that I can add MyProject\bin\Debug\ru\AssemblyName.resources.dll as reference to my project and so I can use russian values. It resolves my problem for now, but I think that there should be some normal solution to the problem.
//it works with reference, but does not without it
Resources.Culture = System.Globalization.CultureInfo.GetCultureInfo("ru");
TaskDialog.Show("Info", Properties.Resources.IsRoomName);
//returns "Имя комнаты"
As I understand, it should be done by itself but works only with already builded dll.
Related
I would like to localize the VirtualKey for Control.
Currently there is code like this:
using Windows.System;
var message = "Press " + VirtualKey.Control.ToString() + " + D for deletion!";
Is there an API which can be used to have the following?
Press Control + D for deletion! (on an English system)
Press Steuerung + D for deletion! (on a German system)
As VirtualKey is an enumeration, you cannot translate a member of an enumeration directly, but you can use it as the key for resource files, building up a localization system:
I followed this tutorial to build up a simple localization system on a Windows 8.1 Application; these are the steps I followed (summed-up)
Create a folder called "Strings"
Inside the folder, create a folder for the default language you want to support and name it accordigly (see a full list of possible codes here)
Create a Resources.resw file in that folder
Add the strings you want to add - If you want to support the use of VirtualKey.xxx.ToString(), I strongly suggest you to use the same identifiers of the enumeration; for instance: if you want to translate the control character, call the new resource "Control" - (this is what I did):
Copy and paste that folder for a number of times equal to the number of languages you want to support (I did it twice):
Edit the Resource.resx file accordingly to the language you're translating to.
In code, refer to each translated string with
var loader = new Windows.ApplicationModel.Resources.ResourceLoader();
loader.GetString(VirtualKey.xxx.ToString());
That's what I did on a TextBlock in MainPage.xaml:
var loader = new Windows.ApplicationModel.Resources.ResourceLoader();
this.txbCtrl.Text = String.Format("Press {0}-Z to undo", loader.GetString(VirtualKey.Control.ToString()));
And that's the result for the language it-IT:
To test it for other languages, I followed the tutorial, even if I don't like it:
Open the Control Panel and go to Clock, Language, and Region > Language
Note that the language that was displayed when the app was ran is the top language listed that is, on my system, Italian.
To test the app with another language, select the language in the list and click Move up until it is at the top. Then run the app.
NB: If you do not have all three of these languages on your machine, add the missing ones by clicking Add a language and adding them to the list.
In my case I have italian listed for first and English (UK) as the second one, but if I swap them:
and run again the application, this is the result
and if I add de-DE:
with these resource files:
en-GB:
and it-IT:
and de-DE:
Notice that I've called the English Control key "Control" and the Italian one "Ctrl"; it works perfectly
I Hope this helped.
If you have more questions, just ask!
LuxGiammi
EDIT: this is a solution, even though I recognize this is not a good one (anyway, it's the best I could think of). Anyway, as stated here for WinForm applications, it is not necessary to do it because evrybody would understand you if you use the defualt names for the keys (i.e., the one in the enumeration, just like you're doing now).
EDIT2: this solution, however, sets everything up for a future "full" localization for your application. This way half of the effort is made at the beginning of the developemnt process.
this is really my first time asking here, but I'm facing a problem which I'm sure of I'm missing something simple.
I wrote a C# class library with a function that returns a List>. The function itself works fine if used from a console application that I created to test the DLL.
The ending part of the function is:
/* Sorto i risultati in base al ranking */
List<KeyValuePair<string, double>> SortedList = new List<KeyValuePair<string, double>>(matchIndexList_rank);
SortedList.Sort(delegate(KeyValuePair<string, double> firstPair,
KeyValuePair<string, double> secondPair)
{
return (-1) * firstPair.Value.CompareTo(secondPair.Value);
}
);
stopWatch.Stop();
response.success = true;
response.executionTime = stopWatch.ElapsedMilliseconds;
response.resultListKVPStrDoub = SortedList;
return response;
Here for example the double part of the first value in the list is 15.5796761265999 (sorry cannot include pictures yet!)
The problem rises if I include the DLL in a ASP.NET MVC Application that uses the function. In this case the double part of the List> is returned without the decimal part.
The corresponding value to the one presented above is returned as 155796761265999.0
Here is a little bit of code where i get the wrong value returned:
searchEngine.Core.search search = new searchEngine.Core.search(HttpContext.Current.Server.MapPath("~/index_" + oIdentity.comapanyId.ToString()));
searchEngine.Core.genericResponse response = search.doSearch(searchStr);
double maxScore = response.resultListKVPStrDoub.Count > 0 ? response.resultListKVPStrDoub[0].Value : 1;
In example maxScore get the 155796761265999.0 value, but also every other value in the List suffers the same problem.
If I inspect the variables from both sides Visual Studio 2013 states that the type is indeed a double.
Both projects are developed on the same machine which also is my test environment. I use .Net Framework 4.0 on both projects and all build parameters seems to be equal on both projects.
I'm sure I'm missing something simple, but just can't get.
I'd appreciate any help.
Thanks
Lorenzo
Hi, problem solved ! (sorry but I can't yet answer by own question before 8 hours from posting)
Thanks to Radu Pascal that put me on the right way: normally I always add a class library to a solution to debug it, but in this case I had it developed for another project and it was working fine so I ignored the obviuos.
Thanks to Marc Gravell, he spotted the problem: even if I forgot it at the base of my search engine there is a file based index. There is where i reed my values from text and there is where I do the reading wrong.
The real mystery is that the same DLL used in the console application used for testing and even on another ASP.NET MVC on the same machine works fine. I solved using double.parse instead of Convert.ToDouble and imposing an invariant culture.
idfDict[idItem] = double.Parse(s.Split('|')[1], System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.NumberFormatInfo.InvariantInfo);
Thanks to all!
Lorenzo
Hi, problem solved !
Thanks to Radu Pascal that put me on the right way: normally I always add a class library to a solution to debug it, but in this case I had it developed for another project and it was working fine so I ignored the obviuos.
Thanks to Marc Gravell, he spotted the problem: even if I forgot it at the base of my search engine there is a file based index. There is where i reed my values from text and there is where I do the reading wrong.
The real mystery is that the same DLL used in the console application used for testing and even on another ASP.NET MVC on the same machine works fine. I solved using double.parse instead of Convert.ToDouble and imposing an invariant culture.
idfDict[idItem] = double.Parse(s.Split('|')[1], System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.NumberFormatInfo.InvariantInfo);
Thanks to all!
Lorenzo
Currently I'm finishing my very first iPhone application with MonoTouch. Localization through the "*.lproj" folders works as expected.
Having an UIWebView that displays some user guidelines, I'm populating this one with the LoadHtmlString() method. (I.e. no internet connection is required).
Since the text is a bit longer, I do not want it to be placed inside the "Localizable.strings" file but being swapped out to a completely separate file (as I'm doing it for Windows .NET applications, too):
In the above screenshot, I would have one "help.html" file inside each language folder and call the LoadHtmlString method to read from the appropriate file in a way that would be similar to NSBundle.MainBundle.LocalizedString.
My question:
Is it possible to have per-language files and access them from within a MonoTouch application?
Follow-up to Dimitris' solution
Based on Dimitris' solution, I solved it by this code:
var localizedHtmlFile = NSBundle.MainBundle.PathForResource("help", "html");
var text = File.ReadAllText(localizedHtmlFile);
helpTextView.LoadHtmlString (text, null);
Yes, of course it is possible. You can get the path of the localized file like this:
string localizedHtmlFile = NSBundle.MainBundle.PathForResource("help", "html");
You can use the PathForResource method for various different types of resources (PDFs, images, etc.). The first parameter is the file name and the second one is its extension. Check the other overload of the PathForResource method for more options.
I need to make a form that have to suport two languages (for now).
the two languages dont have the same look and also half of the form is not a like but it still have some similiarity beetwen them.
what is the best way to deal with this problem in a case of two different languages and above?
by the way, the program language that I use is C#.
10x
First, configure your project default language, you do this in the Project Properties, in the Assembly Information dialog, there's a "Neutral Language" setting at the bottom.
Set this to be your "default" language, the "main" language if you wish.
Then, make sure the form as it is now, is in that language.
To start translating and changing the form to comply with a different language, first set the "Localizable" property of the form to true, and then change the Language property to your second (or third, fourth, etc.) language.
Once you have changed that, you can start making changes. Make sure you don't delete items on the form, instead just set them invisible. Deletion is done for all languages, but invisible will thus only be set for the current language.
Keep switching back and forth between the languages to make adjustments.
To test your program in a specific language, execute this at the start of your Main method:
Thread.CurrentThread.CurrentCulture = new CultureInfo("code of that other language");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("code of that other language");
For instance, to test it with "Norwegian, Bokmål" language, which is my main language, the code you would use would be "nb-NO". To find the code you need to use, once you've changed the language of your form to the language you want to localize for, and saved, a new file will be added to the solution explorer with the right name.
For instance, for Form1, the following files will be present:
Form1.cs
Form1.designer.cs
Form1.nb-NO.resx <-- here's the localized content
Form1.resx
Now, having done this, there's plenty of other things you need to be aware of when making a localized application, I suggest you go read other questions on SO and on the web with more information, like these:
Best practice to make a multi language application in C#/WinForms?
Parsing DateTime on Localized systems
Pluralising and Localizing strings in C#
How do I best localize an entire app to many different languages?
I think a single global resource file for each language is better than having a resource file for each form for each language.
I recently did globalization of a winforms app with the help of the following link "Globalization of Windows Applications in 20 Minutes". Here are the steps for simplicity :
Create a text file say "data.en-US.txt" in the solution (<filename>.<culture string>.txt) in the format below :
Hello=Hello
Welcome=Welcome
This file "data.en-US.txt" is a resource text file in the English language.
Create a similar file in your desired language say German, so its filename will be data.de-DE.txt
Hello=Halo
Welcome=Willikomen
The first column are the labels which will be used as keys for referring the text.
Create a "data-en-US.resource" file out of the text files by using the command "resgen.exe data.en-US.txt",
Do this for all language files. Now in your solution you have 2 (or more, depending on the languages you want to support) data.<culture string>.resource files
Click all the .resource files and set "Build Action" property to "Content".
Click all the .resource files and set "Copy to Output Directory" property to "Copy Always / Copy If new".
Get the localized string using the code below in any of the forms :
string label = "Hello";
string cultureString = "de-DE" // [or "en-US"]
string strResourcesPath = Application.StartupPath;
CultureInfo ci = new CultureInfo(cultureString); ['cultureString' represents your desired language]
ResourceManager rm = ResourceManager.CreateFileBasedResourceManager("data", strResourcesPath , null); ["data" is the first part of the filename]
var answer = rm.GetString(label, ci); // here ans="Halo" as we selected de-DE
I'm messing around with some windows functions using p/invoke. Occasionally, I get an error code that is not ERROR_SUCCESS (such an odd name).
Is there a way to look these up within the program? Forexample, if I get error 1017. Can I tell the user
The system has attempted to load or
restore a file into the registry, but
the specified file is not in a
registry file format.
(ERROR_NOT_REGISTRY_FILE: 0x3F9)
Instead of
Error Code: 1017
I'm not sure if there's a niifty .NET wrapper, but you could call the FormatMessage API using P/Invoke.
See this answer for how it would normally be called from native code. Though the question refers to grabbing error codes from HRESULTs, the answer also applies for retreiving codes from the regular OS error codes coming from GetLastError/GetLastWin32Error).
EDIT: Thanks Malfist for pointing me to pinvoke.net, which includes alternative, managed API:
using System.ComponentModel;
string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;
Console.WriteLine(errorMessage);
You could take the defines from winerror.h at Rensselaer Polytechnic Institute, and put them into an Enum:
public enum Win32ErrorCode : long
{
ERROR_SUCCESS = 0L,
NO_ERROR = 0L,
ERROR_INVALID_FUNCTION = 1L,
ERROR_FILE_NOT_FOUND = 2L,
ERROR_PATH_NOT_FOUND = 3L,
ERROR_TOO_MANY_OPEN_FILES = 4L,
ERROR_ACCESS_DENIED = 5L,
etc.
}
Then if your error code is in a variable error_code you would use :
Enum.GetName(typeof(Win32ErrorCode), error_code);
I landed on this page while in search of a managed alternative to calling FormatMessage through P/Invoke.
As others have said, there is no way to get those capitalized, underscored names, short of looking them up in winerror.h, which I have seen reproduced online in various places where I landed in the course of searching for information about resolving specific status codes. A quick Google search, for winerror.h, itself, uncovered a page, at Rensselaer Polytechnic Instutute, where someone has helpfully extracted the #define statements from it.
Looking at it gave me an idea; I think there may be a way to get there, working from the source code of winerror.h, which I have, as part of the Windows Platform SDK that ships with every recent version of Microsoft Visual Studio.
Right now, I am in the middle of sorting out a pressing issue in the .NET assembly that brought me to this page. Then, I'll see what I can cobble together; this kind of challenge is right up my alley, and somebody threw down a gauntlet.
Yes there's a function that does that but I don't remember what it is. In the mean time, you can use the error lookup tool (Tools->Error Lookup) to see what a particular code means from within Visual Studio.