I have a control which is localized. I'm using the VS Net designer and end up with MyControl.cs, MyControl.Designer.cs, MyControl.resx and some MyControl.xx.resx for the non-default languages.
Some of the strings that are displayed are dynamic and I need to call string.Format on them (eg I have "Hello, would you like to {0} today?", and I could format it using "read a book" or "watch TV"). How should I handle those dynamic strings?
For now I added the string to format in a label in the designer, but I don't know where to put the replacement strings. I could create an independent resx file just for them, but it feels like a mess just for a few strings. I could also have an independent resx file for all such strings throughout my application, but then it's like a potpourri and it sounds messy too.
The cleanest way would be to simply add those strings to the existing MyControl.resx and MyControl.xx.resx, but I dont know:
if it's safe (ie whether the VS Net designer will mess with them, just like I shouldn't manually modify the InitializeComponent method)
how to access those strings (in InitializeComponent I see how it's being used for a control resources.ApplyResources(this.myLabel, "myLabel"), but can I have a string which is not related to a control, and could I avoid using magic strings?)
I think you are looking for ResourceManager. The link also has an example for your case
Related
My code requires bunch of large constant values. The definition would be much easier to read if thousands separators could be used in declarations. Is there any better way to declare the weight variables than conversion of a string (i.e. Convert.ToDouble("1,987,123.456"))?
Note: there are many questions/answers related output format, but no luck identifying anything for the C# code itself.
If you can use Visual Studio 2017, there's a new feature in c# 7.0 exactly for this:
C# 7.0 allows _ to occur as a digit separator inside number literals
So, you can use:
1_987_123.456;
But those are constants. Don't put them inside a string because they will be taken as part of it (not that there's any reason to hard-code a double inside a string, anyway).
I just spotted this line in an old Windows Forms app (created by the designer):
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
it seems to be using ResourceManager.GetObject() in order to get the icon. My question is regarding the significance of the $ prefixing this. There is no mention of the dollar symbol in the docs.
Does the dollar have a special meaning (reflection possibly?) or merely just to do with the implementation of GetObject()?
In addition where is the icon actually stored?
This is a pretty standard trick used in .NET, the compiler uses it too when it needs to generate a name for a auto-generated class or field. Using a character like $ ensures that there can never be a name collision with an identifier in the program.
There isn't much chance of that when you only program in C#, this is a keyword. But certainly in other languages. You could for example create a VB.NET Winforms project, drop a button on the form and name it "this". When you localize the form, the button's Text property source appears as:
<data name="this.Text" xml:space="preserve">
<value>Button1</value>
</data>
That would be a name collision with the form's Text property resource if it didn't put the $ in front of it. Not until you program in a language that permits $ in an identifier name anyway. None of the standard VS languages do.
Yet another detail is that you'll have trouble referencing that button from C# code. There's an escape hatch for that as well, you can use #this in your code. The # prefix makes sure that the compiler doesn't recognize it as a keyword but just a plain identifier.
The WinForms designer actually dumps quite a bit of hidden items like this into the resource file (.resx) assocaited with each form in order to support, mostly, internationalization (though other designer meta-data is there as well). While text and icons may be obvious, even layout information can be there. I suppose those German words are pretty long so when internationalizaing the form you may actually need to change label widths.
The $ I would assume is a way to make sure the designer-added resources don't conflict with user resources.
It turns out that the icon is stored within the corresponding .resx file and was actually defined with the dollar prefix $this.Icon. This would imply that the dollar doesn't have a special meaning at all.
I have a C# application that I need to convert to support English and Spanish, is there a semi easy way to add that in and be able to add other languages later on?
Yes! It's called resource (.resx) files. What you do is this:
Change the Localizable property of your localizable forms to true. This will make the designer fetch text and other properties from the .resx files instead of hard-coding them.
Create your program in one language, let's say English.
Next, change all your forms to another language like so:
Change the Language property of the form to the other language, let's say Spanish.
Change the text on all your controls. The designer will automatically generate a new .resx file for the language.
Swap back and forth as needed during development.
When publishing, go into your Assembly Settings and change the language. You can also change the language in code, I think.
And voilà! You're done!
You mark all your forms and controls as localizable. This will put all UI related text (labels etc.) in resource files. If you need to create strings in code then you use string resource files and look up the string by the resource key (e.g StringResource.Get("My_Message")). Then you can use a tool to translate all your resources. Typically you create a localized .dll for each language. We use Passolo for that but there are other tools around.
You can make a multilingual application in two ways:
By making the application Localizable, so when the user changes the culture of the device, the application will switch automatically to culture's UI if you added this language already to the supported languages in the application.
You can perform this by setting each form's Localizable property on the project to Localizable, and then changing the UI to the new culture.
By making a language option and a resource file (.resx) for each added language in your application, and depending on the selected language, you can load the images or the strings from selected language's resource file.
Without installing any 3rd party tool, APIs, or dll objects, I am able to utilize the App_LocalResources. Although I still use Google Translate for the words and sentences to be translated and copy and paste it to the file as you can see in one of the screenshots below (or you can have a person translator and type manually to add). In your Project folder (using MS Visual Studio as editor), add an App_LocalResources folder and create the English and other language (resx file). In my case, it's Spanish (es-ES) translation. See screenshot below.
Next, on your aspx, add the meta tags (meta:resourcekey) that will match in the App_LocalResources. One for English and another to the Spanish file. See screenshots below:
Spanish: (filename.aspx.es-ES.resx)
English: (filename.aspx.resx)
.
Then create a link on your masterpage file with a querystring that will switch the page translation and will be available on all pages:
<%--ENGLISH/SPANISH VERSION BUTTON--%>
<asp:HyperLink ID="eng_ver" runat="server" Text="English" Font-Underline="false"></asp:HyperLink> |
<asp:HyperLink ID="spa_ver" runat="server" Text="Español" Font-Underline="false"></asp:HyperLink>
<%--ENGLISH/SPANISH VERSION BUTTON--%>
.
On your masterpage code behind, create a dynamic link to the Hyperlink tags:
////LOCALIZATION
string thispage = Request.Url.AbsolutePath;
eng_ver.NavigateUrl = thispage;
spa_ver.NavigateUrl = thispage + "?ver=es-ES";
////LOCALIZATION
.
Now, on your page files' code behind, you can set a session variable to make all links or redirections to stick to the desired translation by always adding a querystring to urls.
On PageLoad:
///'LOCALIZATION
//dynamic querystring; add this to urls ---> ?" + Session["add2url"]
{
if (Session["version"] != null)
{
Session["add2url"] = "?ver=" + Session["version"]; //SPANISH version
}
else
{
Session["add2url"] = ""; // ENGLISH as default
}
}
///'LOCALIZATION
.
On Click Events sample:
protected void btnBack_Click(object sender, EventArgs e)
{
Session["FileName.aspx"] = null;
Response.Redirect("FileName.aspx" + Session["add2url"]);
}
I hope my descriptions were easy enough.
Does anyone know of a way to store arbitrary data in a RichTextBox without the user being able to see this data? The 2007 RTF specification includes annotations ("\atnid", "\atnauthor", "\annotation", etc.) but whenever I insert these into a RichTextBox's .Rtf, the annotations disappear (presumably because the RichTextBox doesn't support RTF annotations.) I have a related question about whether it is possible to store the information inside a Metafile image. Either of these solutions would be acceptable. TIA.
What I'm trying is something like this:
string objectXml = MySerialization.ToXml(object);
string commentRtfFragment = String.Format(#"{{\*\atnid MyApp}}{{\*\atnauthor MyApp}}{{\*\annotation {0}}}", objectXml);
string imageRtf = String.Format(#"{{\rtf1 {{\pict\wmetafile{0}\picw{1}\pich{2}\picwgoal{3}\pichgoal{4} {5}}}{6}}}",
PixelMappingMode.MM_ANISOTROPIC, picw, pich, picwgoal, pichgoal, imageHex, commentRtfFragment);
richTextBox.SelectedRtf = imageRtf;
Update: The application metadata ("annotations") must correspond with particular locations in the RTF. There will also be multiple annotations per RichTextBox (or RTF document if you like.) I also want the metadata to persist with the RTF. So while it would be possible to persist the metadata in a control.Tag, then I would have to take care of adding the information to the database myself, noting whenever the user edited the RTF and somehow determine the new location of the metadata after the edit.
I think the response with atandb will provide the right solution. You can use \v and \v0 to hide the data inbetween and access that hidden data as a specific data to that particular location.
I tried in the richtextbox and the rtf property supports that and it does not modify the rtf contents by skipping the control code. I had the same problem and I luckily ended up with this page and now I am able to have some annotation/comments like feature for any location in the rtf data.
Thank you very much Carl for your question and AtanDB for your answer.
I don't know if there's any special way for doing this for RTF documents, but if you just want to store some data in a control (any kind of Control) without showing it to the user, you could use the Tag property as can be seen here: Control.Tag
I think ho1 has the right idea. Control.Tag is an object so you could use a generic data structure like List, Hash, Dictionary, etc. to store your multiple annotations and store that in the Tag property.
The richtext control supports hidden words with \v and turns hidden off with \v0 and no, I have not confused them even though logically \v would stand for visible it does the opposite.
I am using MacVim to convert cs files to HTML. The convert function works fine. However, I don't like the default syntax highlighting for cs.
I understand that the cs syntax file in at /Applications/Vim/MacVim.app/Contents/Resources/vim/runtime/syntax/cs.vim, Maintainer by Anduin Withers. Not sure if there is any other way to substitute this one with a better syntax highlight file or update it with a a newer version?
You are probably looking for colorscheme files. Syntax files define which part of text (source code) is what, for example it identifies keywords, function names and variables and so on. Color scheme defines what color should each component get.
To change color try
:colo <name of colorscheme>
To cycle through existing schemes use tab:
:colo <TAB>
You can get new colorschemes (choose the ones you like and save as plain ascii files) and store them in your $HOME/.vim/colors directory which is searched by vim before the standard one that you mentioned above.