Use 2 languages on a single page - asp.net

Is it possible in ASP.NET MVC to use 2 different languages on a single page, each with it's own resource file?
So for example I want the header of the page to be in english and the content in german.
Now there should be two dropdowns to change the language, one for the header and one for the content. If I change the header language to french the content should still stay german and vice versa.
Is this possible or not?

Just an update if anyone is interested.
There is a way to solve this problem.
In the example below the culture initially is set to german, then changed to english for the partial, and in the end, reset to german again.
This results in the partial being rendered with the english language, whereas the rest of the page is rendered in german.
#{
var culture = Resources.Resources.Culture;
Resources.Resources.Culture = new System.Globalization.CultureInfo("en");
}
#Html.Partial("Control")
#{Resources.Resources.Culture = culture;}
#Resources.Resources.welcome
With a custom helper method this would certainly look a bit more elegant.

Related

My internal multilingual link point in wrong direction

Have some serious problem with links behaviour in my multilingual site (English (en) and Swedish (sv)). The default languages is English.
I have some nodes with a summery. In the summery I have internal links to my Swedish content. To link to page3 I use sv/page3.
I make a view with the summaries filtered on the language.
If I place this view as a front page all works fine but if I link the view from a translated Menu-link the will be sv/sv/page3.
Hope for help
Will adding slash sign at beginning help? Try using root relative paths instead of relative paths. So don't use sv/page3, but instead use /sv/page3

Encode the html string in Asp.net MVC

<%= Html.TextArea("text", "This is <b>Zeb</b>") %>
From the above statement what i expect is to show the string in textbox as:
This is Zeb.
But what i actually get is
This is <b> Zeb </b>
My string is not properly encoded. So please can someone tell me where is the problem?
I have read it from book Professional ASP.NET MVC 4. It's on page 100.
There is no problem, it is expected behavior. Here is a same question: ASP.NET MVC3, Html.TextAreaFor without encoding?
Your question has nothing to do with encoding. You are telling MVC (and the browser) to show a text area to edit that string, and that's what it's doing.
If you want an HTML editor with which your users can edit the HTML (while seeing it properly rendered), you should check this post:
What's the best WYSIWYG editor when using the ASP.NET MVC Framework?
Basically, you'll usually have to include the .js and .css of the selected editor, and activate it on the text area that you are currently sending to the broser. That will create an editor like the one you'll find in Wordpress, for example.
Edit:
Now that you have edited your question, I think you are misunderstanding what the book was referring to. When it says that your text has been encoded it means that the < > characters have been translated into & lt; and & gt; so that the browser doesn't treat them as "open HTML tag" and "close tag".
If you look at the source HTML in your browser, you'll see that the string has been properly encoded (as & lt; and & gt;).

aspx, response.write, image and link referencing

I've spent a while trying to find out whether what I want is possible.
I have 3 websites on different domains. Two are in English, one in French. We have one page in english, one in french which are identical apart from the text. These pages and relevant images (we'll call common content) are stored on a separate domain (reasons beyond my control) and use response-writefile to insert the content into the two english pages.
Got all that working fine. However, the images in these common pages have a path relative to domain on which they are stored, which means when the pages are written into the main pages, the images dont show. I understand why and can get around it by putting in the full path of the image.
I would prefer not to go through every single page changing the image path, is there any way of the server knowing or being told that the image is relative to the common content and not the rendered page?
I wouldn't have thought so, but it would save my day if there was!
Further explanation:
Relative path of image:
abc.png
Path of common content file:
http://domain1.com/CommonContent/123.html
Code in final pages (domain2.com/english.html):
<% Response.WriteFile("/CommonContent/123.html"); %>
Rendered path of image (what I don't want):
http://domain2.com/abd.png
Ideal path of image in rendered page: ie, what I want to happen:
http://domain1.com/CommonContent/abd.png
what you can do is use WebClient class to get the page content
String URI = "http://domain1.com/CommonContent/123.html";
WebClient webClient = new WebClient();
Stream stream = webClient.OpenRead(URI);
String request = reader.ReadToEnd();
then you perform a string replace, here I'm not sure what can match, maybe something like this can match:
request.Replace("<src=", "<src=http://domain1.com/CommonContent/")
then you render this string to the browser.
You can make Application Settings having the path for each domain on each application, and then maybe add a function that will be in charge of writing the full path of the pictures. Also you can make an HTTP Module to address this issue as well as a Generic Handler that will receive all requests for images and load them from different domains/applications.
Good luck!

drupal language switcher unaware of the aliases? switches to node/XX/ instead of LANG/text

I have a big problem with the language switcher,
I have a multi language site that has every article in english and spanish , spanish being the default
so if one article is named :
domain.com/mi-articulo then the english one will be domain.com/en/my-article (using autopath is creating these aliases)
now this works fine if I manualy change the url.. but if I use the language switcher ..the english corespondent it's switches without aliases and Lang prefix
so instead of domain.com/en/my-article it finds domain.com/node/47 so as I have the menu and other things translated it makes drupal unaware of the language switched..
here are the screenshots to better understand what's happening (wierd behaviour)
ver1. spanish
ver2. english
now if i click on english ..look what happends (see url and content like drupal is not aware that this content is nor spanish nor english...because the language switcher doesn't find the aliases)
I had the same problem until I set the default 'Language domain'
in admin/settings/language/edit/en. Now the module is working as expected.
I stumbled upon the same issue today.
All nodes had been imported from another site. All translations were imported, but not the relationship between them, so if you edit a node and check the translations, you may notice that your node doesn't have any given translation.
If this is your issue, you may use the functionality to add existing nodes as translations.
I have the same problem with the language switcher; just cannot get it to do what it's supposed to do. I ended up effectively writing a very simple version of it by adding a block and inserting the following html:
<ul>
<li class="first en">English</li>
<li class="last fr">Français</li>
</ul>
It is limited in that it always takes you back to the homepage after a switch but once that's done, links to other nodes continue correctly with /fr in the url.

Should I replace string patterns in asp.net mvc using a custom viewengine?

Have an ASP.NET MVC site that is localized. The localization functionality adds the two digit language ID to the URL, e.g. /es/Page. If no language Id is found in the URL, the site switches to the user's browser culture. All's good. However, the site's hyperlinks, a mixture of hard-coded href tags, actionlinks, etc., don't include the base language ID, so when clicking through the site the set culture is lost, and the site reverts to the user's browser culture.
My (lazy) thought is to replace all href values, that don't point to an external site, with the localized URL (e.g. include the /es/). Otherwise, all site links will need to be updated to include the culture code.
Is this just plain dumb? Or, reasonable, and should be done using a custom view engine, or some other approach?
My answer is (currently):
The app has a base controller, there I've added:
if (PathLanguageCode == "" && requestContext.HttpContext.Session["LanguageCode"] != null && requestContext.HttpContext.Request.RequestType == "GET")
{
requestContext.HttpContext.Response.Redirect("/" + requestContext.HttpContext.Session["LanguageCode"] + requestContext.HttpContext.Request.RawUrl);
}
This example doesn't show how the PathLanguageCode variable is defined, but it should at least suffice to show how this can be handled centrally, without replacing string values.
The one downside to this approach, that I can see, is that the site really isn't friendly for search engines, etc., since we end up doing a lot of redirects.
If you would like you can look at my approach that we took. It talks about putting the culture in the URL. Hope it helps!
Cultured View Engine for MVC

Resources