Global Resources translation doesn't work - c#

I am trying to implemented App_GlobalResources for my application to support multi-language.
I've created an AppGlobalResources folder, inside two files:
WebResources.resx
WebResources.de.resx
In each one, I put a TestString name which equals to 'test' in the default one and 'German' in the de one.
In the Page_Load I put the following:
System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("de");
When I call:
txtTest.Text = WebResources.TestString;
I get the default text 'test' and not 'German' as it supposes to.
The file properties for both resx files are:
Build Action: Embedded Resource
Custom Tool: PublicResXFileCodeGenerator
Update:
I checked the WebResources.de.designer.cs and it's empty, unlike the WebResources.Deisgner.cs which has code generated for it.
Developing a .NET 4.6.1 web project using Visual Studio Community edition 2017.

The problem was that the second resx file didn't have the right build action and custom tool set. Once I changed it, it works.

Related

Localizing Xamarin Forms

I am trying to localize a Xamarin Forms app. I followed all the steps included here: https://developer.xamarin.com/guides/xamarin-forms/localization/
However, in the Android Emulator, after changing the default language, the strings do not get displayed, they remain the default language. On the device the app doesn't even launch.
Steps taken so far:
1) tried breakpoint (BP) in the main launcher, it skipped the BP and entered the main page
2) tried BP on the Localize class, it doesn't reach it
3) tried BP on the Device.OS check, skips it.
The resource files in Visual Studio are the following:
AppResources.resx
AppResources.fr.resx
AppResources.es.resx
Anyone ever succeeded localizing a Xamarin Forms app before ? I need some assistance with this as Visual Studio does not raise any exception ...
UPDATE 1 :
The resource files in Visual Studio are the following:
AppResources.resx
AppResources.fr_FR.resx
AppResources.es_ES.resx
UPDATE 2:
After recent suggestions and modifications I have the following exception: Markup extension not found.
This is disturbing as the markup extension class is there.
Doesn't work in DEBUG mode (Android only)
If the translated strings are working in your RELEASE Android builds
but not while debugging, right-click on the Android Project and select
Options > Build > Android Build and ensure that the Fast assembly
deployment is NOT ticked. This option causes problems with loading
resources and should not be used if you are testing localized apps.
SOLVED:
The problem was in the declaration of the extension. In a PCL project, the following took place:
a) the TranslateExtension class is located in a subfolder named Pages
b) initial declaration was
xmlns:languages="clr-namespace:MyApp;assembly=MyApp"
c) the following declaration solved the problem:
xmlns:languages="clr-namespace:MyApp.Pages;assembly=MyApp"
along with language code naming conventions of the .resx files as mentioned above
2021 UPDATE
All the current answers are not valid anymore! You need to add resources. Right click your xamarin forms project (Not xamarin android or ios) => Go to project properties ==> Resources ==> Add Resource.
This will create a "Properties" Folder and a resources.resx file which you can delete if you want to.
Then follow the official microsoft documentation by creating a AppResources.resx and other language files (such as AppResources.ar.resx file for arabic). All these files MUST BE in the properties folder.
Then, in your XAML page, declare the namespace as:
xmlns:res="clr-namespace:YourProject.Properties"
Then simply use:
<Label Text="{x:Static res:AppResources.Address}" >
This will use the corresponding Language files depending on what language the device is using. If the language is not supported it will use the defulat file (AppResources.res)
FIUH!!! Wasted one hour to solve this!!!

How to add a Setup project through Visual Studio DTE?

I am trying to develop a template Visual Studio 2008 solution making use of the IWizard interface. So far I have been able to successfully add multiple projects to the solution. I am now looking to put in a Web Setup project into the solution which will already have the Project Output from a particular project specified.
I read that a .vdproj file cannot be part of a template, so looking at any other alternate options available.
One option that I tried but did not work was to save the contents of the .vdproj file within the wizard and write it out after the solution gets generated. I am wondering if I can use the Visual Studio ExecuteCommand to add a Project and add the Output Group in the setup project without displaying the UI.
Anyone tried this before?
I found the answer.
The method of saving the contents of the .vdproj file within the wizard works. However, before writing out the content to a file and adding it as a project, the GUID of the project which would be used in the Setup project (Project Output or Content Files) has to be replaced in the saved text.
Visual Studio adds a unique project GUID once the project gets added to the solution, and this GUID has to be read using the Visual Studio SDK and replaced in the actual .vdproj file text.

Creating default set-ups for commonly used code in Visual Studio 2010?

This is a pretty minor annoyance, but I noticed that every time I create a class in Visual Studio there are a few things that I would like to be automatically-generated for me. For instance, all of my classes start with a log4net declaration. It's only one line of code, but it's annoying having to find a class where I declared it in to copy/paste from.
In addition, there are multiple common 'usings' which I use, but that are not created by default.
Is it possible to setup VS to do this?
You just need to edit the default Visual Studio Class template. A walkthrough of the process can be found here.
From the File menu, select "Export Template".
You can create your own item/project templates. Here is a primer from MSDN:
http://msdn.microsoft.com/en-us/library/6db0hwky.aspx
Basically you'd create your own custom class template.
From MSDN:
Creating Project and Item Templates
How to: Manually Create Item Templates:
Create a project and project item.
Modify the project item until it is ready to be saved as a template.
As appropriate, modify the code file to indicate where parameter replacement should occur. For more information about parameter replacement, see How to: Substitute Parameters in a Template.
Create an XML file and save it by using a .vstemplate file name extension, in the same directory as your new item template.
Author the .vstemplate XML file to provide item template metadata. For more information, see the example in the following section.
Save the .vstemplate file and close it.
In Windows Explorer, select the files you want to include in your template, right-click the selection, click Send To, and then click Compressed (zipped) Folder. The files that you selected are compressed into a .zip file.
Copy the .zip file and paste it in the user item template location. In Windows Vista, the default directory is ..\Users\\Documents\Visual Studio 2010\Templates\ItemTemplates. For more information, see How to: Locate and Organize Project and Item Templates.
Although not exactly what you are asking for, one option is to declare code snippets. This will allow you to leave the standard class alone (not change the template) and add in the pieces that you require without having to find a class with it. However, if all your classes for every project you create requires the same basic set of code then Templates are the way to go.
C# Code Snippets
How to use them can be found How to Use Snippets

Adding a TestSettings file to SolutionItems in VisualStudio

((EnvDTE80.Solution2)dte.Solution).GetProjectItemTemplate("Test Settings", "Test");
I need to add a new TestSettings object programmatically to a C# project in Visual Studio. To do this I need to get the path of the template file. The call to GetProjectItemTemplate should do this, but fails for this type of template. Other template types, for example, a CSharp class, work fine. Even custom types that I have registered work fine. But not the TestSettings type.
You can see the template in this zip file:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\Test\1033\TestSettings.zip
Any idea why this template type doesn't work?
You are not the only person to have issues with "test settings", a chap on the forum
below resorted to unloading, editing the project xml and reloading it in order to update
the settings...
Its a bit of a faff but it could be some solution..
http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/a1160459-23e3-4bd4-a672-d204eb9ffa4c/
In VS 2017 it is there under the solution.
Right click on Solution|Add Item...|Test Settings

How to add a jscript or html item template via an add-in in visual studio

I'm currenlty working on an add-in and require the functionality to add custom item templates to a project programatically.
I'm currently installing the templates (which are of a .zip format) in ..\Visual Studio 2010\Templates\ItemTemplates\.
From within the add-in I then call the following code:
string templateLocation = (solution as Solution2).GetProjectItemTemplate(templateName, language);
projectToAddTo.ProjectItems.AddFromTemplate(templateLocation, nameOfNewItem);
If the item template is for a C# file then if i use "CSharp" for the language variable and place the template in the Language Folder "Visual C#" or "Visual Web Developer\Visual C#" then this all works fine; the template is discovered and implemented correctly within Visual Studio.
However if I create a JScript or HTML template, the GetProjectItemTemplate method throws an exception if I supply the language as "JScript" or "HTML". I've tried placing the templates in both "ItemTemplates\JScript(HTML)" and "ItemTemplates\Visual Web Develerop\JScript(HTML)" with no luck.
Anyone have any ideas about what I'm doing wrong, what languages are supported for these custom item templates, what the language parameter and folder location should be?
From having looked into this a bit more it seems that the parameter Language in string Solution2.GetProjectItemTemplate(string TemplateName, string Language) is actually the language type of the project which the item template is targetting. Hence whether the actual item in the template is a java file or an html file, the project would still be CSharp or VisualBasic and so that is what should be passed in as the Language parameter.
Furthermore, in the location for custom item and project templates, it doesnt matter what the folder is called, be it Visual C#, Visual Basic, Visual Web Developer\JScript. The name of the folders just affects how it appears in the Add New Item Dialog and how its stored in the cache.
Also, the TemplateName parameter you specify when calling GetProjectItemTemaplate should have the full reletive path from beneath the ItemTemplates folder. Although it will work without it, if you have more than one template with the same name in different locations then you'll get the first one returned by VS.
My suggestion is to take an HTML or JS and use VS's own menu to export this as a template. Then, unzip the template and look for the language parameter in the .vstemplate file.

Categories