I have been using .resx files for localisation of text in my MVC3 app quite happily for a while now following these instructions, but have now got to a point where i need to do localisation of images as well.
the .resx editor in VS2010 will let you add an image as a resource easily enough, it just dumps it in a folder called Resources in the same directory as the .resx file, and i can reference the image in the same way as strings in my views by calling #ResXfileName.ResourceName, but that returns a System.Drawing.BitMap object, i was hoping it would just give me the path to the image so i could shove it in am image tags src attribute, but i can't work out how to get the path at all!
Solution 1: locale folder structure
The best (and easiest) way would be to add a new HtmlHelper extension method that returns locale name (or LCID) and you can use it with anything afterwards (images, scripts etc.). The best way is of course to have localised files in such folder structure:
/images
/en-US
image1.png
/en-GB
image1.png
/es-ES
image1.png
And this is how you would use it then:
<img src="/images/#Html.Locale/backgrounds/back.png" />
This is an example of such extension method.
public MvcHtmlString Locale(this HtmlHelper helper)
{
// in case you're setting UI culture
return MvcHtmlString.Create(System.Threading.Thread.CurrentThread.CurrentUICulture.Name);
}
If this simplification doesn't work in your case and you need specific call for images, then create a UrlHelper method called Image or File where you provide it server app relative path with some tokens that get replaced with your locale name or LCID.
Solution 2: Resource file links
You can also put image paths inside a resource file and use those values in your views to provide correct paths for your images (or other files for that matter).
public MvcHtmlString Resource(this UrlHelper helper, string classKey string resourceKey)
{
return MvcHtmlString.Create(helper.ViewContext.HttpContext.GetGlobalResourceObject(classKey, resourceKey));
}
And then in your view use it as:
<img src="#Url.Resource("images", "header")" />
Related
Currently I retrieve the file path from XAML in this way:
var myFile = System.IO.Path.Combine(Package.Current.InstalledLocation.Path, fileImage.File);
Because in XAML the file name is e.g. icon.png and not icon.scale-150.png it doesn't find the image, when I want to read it into a stream. How can I retrieve the correct file name/path from the scale-based asset?
It has to take the general path and convert it into a scale-based path. Is there a method for this? I can't find one.
If the icon.png is located in the Assets folder, you can just retrieve the URI by using the ms-appx protocol, in this way:
var file = new Uri("ms-appx:///Assets/icon.png");
This will retrieve the icon with the right scale, based on the device.
I'm trying to display an XML feed in a custom Joomla 2.5 component's view/layout, but the XML is rendered as a regular layout inside the site's HTML template. How can I display the XML without any template HTML code?
(The trick to include tmpl=component in the URL from this related question doesn't help, there's still some HTML output from the template that ruins the XML.)
I would prefer a solution that only involves code changes in my custom component, like in Symfony when you call the method setLayout(false).
The only solution I have found is to create a file in the current template folder, e.g. "xml.php", and put only this in the file:
<?php
$document = JFactory::getDocument();
$document->setMimeEncoding('text/xml');
echo '<?xml version="1.0" encoding="UTF-8" ?>';
?>
<jdoc:include type="component" />
Then I can append tmpl=xml to the URL.
[edit]
My bad, I made an assumption and you know what that gets you.
Joomla! 1.6->2.5 you can create an alternate output format for an existing view by:
calling the view with a format parameter attached e.g. &format=json
creating a matching view class file e.g. view.json.php that can sit alongside the standard view.html.php file for you view.
The view.yourformat.php file can use your existing controllers and template files in the normal fashion.
Don't forget to add either &tmpl=component or &tmpl=raw to your query string so modules etc don't load as well.
tmpl=raw won't load the html body surrounds or template, only the main component.
[/edit]
From Joomla! 1.6 onward (including 2.5) there is built in support for controller formats ie. you create a controller for the output format you want.
Normally a controller would be named for each view:
/components/mycomp/controllers/myview.php
A XML version of the controller would be name:
/components/mycomp/controllers/myview.xml.php
A JSON version would be:
/components/mycomp/controllers/myview.json.php
To call a particular format version of a controller you simply add &format=theformatyouwant to the URL parameters, so in your case &format=xml
This is discussed in this document from 1.6 days - I used it as a basis for several of our components that have JSON and ics requirements.
This issue drove me crazy a couple of times.
After much frustration, the simplest solution is the one suggested by cppl. In your query
string put the following variables:
format=yourcustomformat
view=viewname
Let say you want json output from a view called json.
Create a veiw folder with the name of your view
json
And a file inside that folder called
view.json.php
Then in your url string you include the following url parameters seperated by the & symbol:
index.php?option=com_mycomponent&format=json&view=json
cppl is correct that this loads a non-html view. However you don't have to put the tmpl parameter in at least in 2.5. If the view name is not view.html.php then 2.5 seems to not include the assigned site template in the response. I think because the view is not veiw.html.php it assumes raw output and does not include the template. I tested this with both an ajax call and a direct url call to the view and in both cases all I got back was the component output. Yeah!
If someone knows where this issue is well documented by the Joomla folks please post!
I need to be able to place images in a path relative to the view, but I'm not sure how to accomplish this, because I don't have a very good grasp of MVC routing:
If I have a route like this:
routes.MapRoute(
"Parameter",
"{controller}/{action}/{lang}/{prod}",
new { controller = "Manuals", action = "Product", lang = "en-US", prod = "productname" }
And then I try to use a relative path for the images:
<embed class="images src="#Url.Content("images/sampleimage.svg")"></embed>
This gives me a path (if I inspect it in the web inspector) that looks like this:
src="http://localhost/Manuals/Product/en-US/images/sampleimage.svg"
which is not where the image is. It is located physically at "http://localhost/Views/Manuals/en-US/productname/images/sampleimage.svg"
So basically, the Views folder, and then no folder like the action name (Product). So how can I use paths relative to the view in this environment? The action becomes part of the url, but there is no actual folder named like the action method, and also the images will be under the Views folder which is not part of the url here... Also, the last part of the url in the route (productname) does not seem to show up at all in the image path, it seems to be interpreted as an id or something because I use only one action method to serve the views based on parameters lang and prod?
Is there no way to place images like this, and use relative paths like "images/imagename.svg" or the like? Or am I just misunderstanding the routing in MVC?
I need to do it like this to simplify adding content which is massive and the result of XSL transformations...
EDIT: Sorry, I had some of the urls incorrect as to the results I got. Should be fixed now to reflect what I have.
I'm not sure whether you understand how the framework is supposed to work.
My first point is regarding the conventions. The convention is that you store your views in the Views folder. You may then have DisplayTemplates, EditTemplates and Shared for your display/edit templates or shared partial views. This is a convention that MVC developers are used to, so if you start placing something else in these folders, you are going to confuse other developers, and eventually yourself.
What are you trying to achieve? Are you after URLs in some specific format? One of the options that you have, is to implement action that will return your image. Another alternatives are URL re-writing or routing to static files: Using ASP.NET routing to serve static files
Use a controller action to get the images from DB or disk.
Here is an example of getting images from a folder called "ImageFolder" in the site root.
public virtual FilePathResult GetImage(string imageId){
var path = Path.Combine(Server.MapPath("~"), "ImageFolder", imageId + ".svg");
return new FilePathResult(path, "image/jpg");
}
You could use something like that to retrieve the sample images for your products.
In ASP.NET MVC you don't rely in folder hierarchy to navigate your site. The framework has some folder convention, but they are not used for routing.
For static images, you can place them in the Content/images folder.
i am making a game and i put some images on my ide/images folder but program gives null pointer exception error.my code for loading imageicon is like this
ImageIcon ac=new ImageIcon(getClass().getResource("images/actor.jpg"));
ImageIcon ac1=new ImageIcon(getClass().getResource("images/actor_shoot.jpg"));
//some codes
Image n=ac.getImage();
//some codes
i tried but couldn't find,whats your suggestions?thanks.
Try with an absolute path ? ("/images/actor.jpg") ?
Is your folder /images resides in same diretory in which your class is?
i.e., is your class is also in /ide folder?
Check the path. Are the files really in the images folder?
Are they called what you say there are called?
Are they the right case, if you're on a case-sensitive file system (in other words that actor.jpg and ACTOR.JPG are two different files)?
Are the images a resource? (Also look here.)
Try doing step-by-step what is described here (NetBeans documentation).
I have a Views folder that has a pageBaseType of Class1 specified in the web.config. In my Areas/MyProject/Views folder I have a web.config that specifies pageBaseType Class2. MyProject is incompatible with class1.
There is a Views/Shared/EditorTemplates/String.cshtml file that's needed everywhere else in the system except for MyProject. Because String.cshtml uses Class1, and MyProject can't it causes errors.
Is there a good way to prevent the inheritance of the EditorTemplates folder in MyProject? I can copy paste the entire contents of EditorTemplates into MyProject/Views/Shared, and that stops it, but I don't want to have to remember to copy files every time we add a new cshtml to EditorTemplates.
What is it that your string editor template does?
If you need to render a specific template for a model property you can use the [UIHint] attribute or the [DataType] attribute. It would be better to do this if you don't intend for it to be used globally. Note that you will need to change the template name to something unique e.g. MyStringTemplate.cshtml.
Based on your comment, that you wish the editor templates to be global but not use them in your Area, my suggestion is to not use #Html.EditorFor(...) in your views, and instead create the form fields explicitly e.g. #Html.TextboxFor(...).