How to get available locales for i18n in SailsJS - internationalization

How is it possible to find all the available locales from a template in SailsJS.
I want to make a dropdown menu in my layout.ejs with all the possible languages declared in the /config/i18n but I don't want to specify the supported languages in another file.
I don't know if it's possible, I've seen only solutions on Rails but I'm stuck on Sails... I haven't found availableLocales field or anything similar.

You can access the available locales in your controller via sails.config.i18n.locales. You could then send that array down to your view as a local:
res.view('myView', {locales: sails.config.i18n.locales});
and in your view, loop through them with something like:
<select name="locale">
<% locales.forEach(function(locale) { %>
<option value="<%=locale%>"><%=locale%></option>
<% }); %>
</select>

Related

Is it possible to pass object as parameters in a web user control markup?

I am building a web page that lists products, using aspx webform. For that, in a user control corresponding to my list, I am looping over my products and injecting one new user control by product:
foreach (Product p in this.Products)
{
ucProductItem.product = p;
%>
<uc:ucProductItem runat="server" ID="ucProductItem" />
<%
}
%>
This works fine and I am OK with that... BUT not totally because I find this quite ugly and messy; I don't like the mixing of markup and code in the template, and I try to use markup as much as possible (and I have this problem all over my project).
Thus, I would like to pass the product p to the new ucProductItem user control via the markup, and I tried naturaly something like:
<uc:ucProductItem runat="server" ID="ucProductItem" product=p />
I know this is possible for primitive types such as strings and integers, but I can not figure out how to do it with objects.
Is that possible ? And how ?

How do I display content from a Wiki page in another page in redmine?

I'm trying to display content from one wiki page in redmine in my welcome/index file.
I've tried linking pages with [[foo]] (which I have found out only works in the wiki itself) and !{{include(projectname:stuff)}}, but when I try to use this, my redmine only displays it as plain text.
<div class="splitcontentright">
<div class="news box">
<h3><%=("News")%></h3>
!{{include(MyWikiPage:MyArticle))}}
<%= call_hook(:view_welcome_index_right, :projects => #projects) %>
</div>
</div>
Is this the wrong method? Is there another way to achieve what I'm trying to do?
After skimming trough redmines code I found the solution.
Instead of writing
!{{include(MyWikiPage:MyArticle)}}
you'll have to write:
<%= textilizable "{{include(Project:Article)}}" %>
this calls the textilizable function which let's you use wiki macros from wherever you want.

Using content_for and yield_content in Ramaze

Sinatra has sinatra/contrib to asist with this, what's the equivalent in Ramaze? I'm using Erubis by the way. Also, a quick Google search shows up really old pages that insist setting variables in the controllers and using them in the views.
Edit 1:
Taken from the gem documentation (http://www.sinatrarb.com/contrib/content_for.html)
You call content_for, generally from a view, to capture a block of markup giving it an identifier:
# index.erb
<% content_for :some_key do %>
<chunk of="html">...</chunk>
<% end %>
Then, you call yield_content with that identifier, generally from a layout, to render the captured block:
# layout.erb
<%= yield_content :some_key %>
I don't think Ramaze can do this natively. But you could quite easily do this manually, write a helper to do this, or even fill-in a Hash instance.
You might also want to look at partials if you need to render small chunks of HTML in loops.
You could also combine render_partial, store results in a hash, and yield it's content in the layout.
If the use case is something like rendering a sidebar, you probably want to write a helper so you take the logic out of your views.
A trivial example is here : https://github.com/Ramaze/ramaze/wiki/Adding-a-dynamic-sidebar-in-a-layout

rendering Ruby statements inside of rails views?

So in my DB I want stored a ruby/HTML statement in the Database Table such as -
p This site is owned from 2000 - #{Time.now.year} by Acme Widget Co. /p
Or let's say that I want people to be able to include in other code snippits - e.g.
A client wants to run in a show view in the middle of a paragraph some magical partial such as #{render 'my_magical_code'}
On the view I have this being rendered as <%= raw(#page.content) %>
But its like a double rendering and rails will just put on the page #{render 'my_magical_code'} or #{Time.now.year}
So can / how do I solve this?
Thanks!
There are work arounds but I think the most proper way to solve your problem is to store your information in a better way. Add a migration to your model with a year_founded and company rows. You can then call these in your views rather than storing the HTML. Eg. This site is owned from <%= #object.year_founded %> to <%= Time.now.year %> by <%= #object.company %>

Rails + Haml: Returning from a partial will somehow remove the form tag

This is lengthy, yes, I apologize. But I want to be clear because this issue is so odd.
I have a terms of service modal that comes up whenever a user has not accepted our terms of service (TOS). For the purposes here, javascript is turned off and what is rendered is strictly html/css. So the page, if thought of in 3 layers, is: bottom layer = page they came to see (i.e. an event page), middle layer: a semi-opaque overlay, top (primary/visible) layer: a modal with the terms of service agreement form in it. This form is simply an accept checkbox and submit button.
I noticed that the TOS was not working properly, but at seemingly random times. Then I noticed that it was broken but only on my event page (/event/foo) even though the same partial is responsible for showing the terms of service agreement no matter where they show up on the site. So for any other page, like /group/bar, the same TOS modal would show up and would work fine.
I then realized that the problem was that the form tag was missing from my HTML! Just gone.
So, taking a step back, the (HAML) code in question is simply:
%div#accept_tosC
%b Before form_for
- form_for #current_user do |form|
%b After form_for
%div#tosC= render :partial => 'general/terms'
%div.left
= render :partial => 'shared/user/tos_form_element'
%div.right
= image_submit_tag "/images/buttons/submit_100x20.png", :id => 'submit', :name => 'submit'
For our /events/foo page, the generated HTML look like this:
<div id="accept_tosC">
<b>before form_for</b>
<div style="margin: 0pt; padding: 0pt;"><input type="hidden" value="put" name="_method"><input type="hidden" value="44c2bf7a64fc59baa3fc7129167f0e2c3e96abb6" name="authenticity_token"></div>
<b>after form_for</b>
The obvious question here is if 'Before form_for' and 'After form_for' make it into the document, why doens't the form tag form_for creates?
For a different page, say /groups/foo, we get exactly what we would expect:
<div id="accept_tosC">
<b>before form_for</b>
<form method="post" id="edit_user_595" class="edit_user" action="/users/595"><div style="margin: 0pt; padding: 0pt;"><input type="hidden" value="put" name="_method"><input type="hidden" value="44c2bf7a64fc59baa3fc7129167f0e2c3e96abb6" name="authenticity_token"></div>
<b>after form_for</b>
I tracked it down to a single partial well inside the code for the "bottom" layer (the page they requested, not the TOS overlay). This partial in question may or may not be viewable to any given individual so we have to check if the user can view this page. The result of that is held in the variable can_view:
:ruby
#some processing to set page info and can_view
return unless can_view
%div#statsC
...and so on...
This is what my code looked like and the form tag did not render. By making the following change, the form element tag showed up as expected for all pages:
:ruby
#some processing to set page info and can_view
- if can_view
%div#statsC
...and so on...
So here is the question: Why would returning from a partial prevent a form element tag from becoming part of the document?
The short answer is that templates work in mysterious ways, and return is just generally not safe to use within them.
The long answer is that, behind the scenes, templates are compiled into Ruby methods that build up a string of HTML code and return that code. I'm guessing that by returning without returning the proper code, you're making something go haywire and discard the string that the form_for is concatenating to.

Resources