Switch to the another language on the same page? - middleman

I am trying to add a language changer for Middleman and it's not generating the correct link. My default and root is English.
url_for("/#{current_page.path}", locale: :ja)
I would expect the equivalent for the current page in JA which is the same URL with JA prepended. Does anyone know how to fix this?

I'm a middleman beginner, but after doing a bunch of Googling it seems like this is a fairly common issue. I've tried to look through the middleman sources to see if I could find a solution, but I've been unable. I'm kinda disappointed about this, because it looks like middleman has first-class support for localizations. Being unable to easily link from one to another seems like a surprising omission.
What I've done is make a little helper that can swap localizations in paths, if needed.
def change_locale_in_path(path, locale)
locale_prefix = I18n.locale
path.gsub(/^#{locale_prefix}/, locale.to_s)
end
This isn't a great solution, though. It will need to be adjusted if you change the i18n :path, and won't work unless you mount_at_root: false. But, it worked well enough for me to move on. I really would love to see a better solution.
I found a few GitHub issues that seem to reference this problem. Here's one.

I am using the following helper to generate a URL for the current page in a different language. It was originally based on this gist, and then tweaked it a bit so that it works regardless of whether mount_at_root is used.
def current_url_for_locale(loc)
url_regex = /\A\/(?:(#{I18n.available_locales.join('|')})\/)?/
locale_root = url_for('/', locale: loc)
current_page.url.gsub(url_regex, '').blank? ?
locale_root :
current_page.url.gsub(url_regex, locale_root)
end

Related

What is the meaning of 'cimode' in react-i18next and why isn't it properly documented?

I started using react-i18next a few days ago and I am very satisfied with it. However, I've been seeing this 'cimode' language here and there, in some posts and while debugging, but have no clue what it means. I've searched all over, I believe, and can't find any documentation on it.
In my particular case, I am generating some boilerplate code in a new website and created a demo page to show how to use localization in the website. I am generating toggle language buttons from the languages I set on the whitelist and, to my surprise, I have a 'cimode' button. I know I can filter it out and I will, but I would like to know what it should be used for and maybe to see better documentation for it in https://react.i18next.com/.
From my understanding, CIMODE is used for testing to consistently return the translation key instead of the variant value.
It seems rather hidden on the FAQ.

silverstripe dynamic backend for flexible frontend

I'm searching for a while, but didn't find a practical answer for the following problem:
An Articlepage in the Frontend can contain different types of sections. For example a block text section and than a text section with left photo an at the end an wallpaper photo.
Now I want to implement this in my backend dynamically, so that I can choose, which type of section and -even more important- how many sections my article should contain.
In my recent project I've solved the problem as an Articlesection with a Dropdown, wich contains the different section styles. And one article hat xy Artilesections as childpages. In the frontend I included all Childpages of the Article - so to speak the Articlesections, where I've checked with if conditions, which sectionsystyle I have in this Section and here I styled the section as I want.
For example Artilce1 has 2 Childpages: Articlesection1 and Artilcesection2.
In AS1 I have choose in the dropdown: right text with left photo and check with if conditions, to style the section. The same with AS2, that is for example a wallpaper photo.
This semiprofessional solution worked, but there are some bugs with displaying the Article correct in the backend, because I have to declare the Articlesections as Includes and not native childpages. And in generell this don't seems like the developers of silverstripe want the users to do it this way..
Are there better ways to implement a dynamically changing backend, so I can decide, which and how much sections I have in the frontend.
Take a look at https://github.com/sheadawson/silverstripe-blocks. There's quite a few addons that offer similar functionality as Blocks such as Elemental. The following blog post might also be useful too - https://www.silverstripe.org/blog/silverstripe-strips/.
Thank you for your reply, but there is a problem, because I don't work a lot with the terminal and the composer. In principle I want to install sheadawsons silverstripe-block. I have installed the composer and run the command:
"composer require sheadawson/silverstripe-blocks" it seems like it installed that correctly, because I became no errors, but what then? With dev/build -to refresh the database- happened nothing and the following instructions in the readmy chanced nothing. Should I copy any files in my Sivlerstripe web folder or what else..? I'm a bit desperate, because there is such a poor documentation.
Tank you

Need help in configuring correct paths for DOH

I'm trying to get DOH testing working for my company's upcoming platform, but I can't figure out the right combination of paths. Our file structure looks like this:
/dojo15
/release
/dojo
/dojo
dojo.js
dojoExt.js
/dojo
dojo.js
/util
/doh
/js
mainLib.js
/tests
base.js
This was created by our contractors, so unfortunately there are some things I have to figure out as I'm going along.
The "release" directory is what is used on our pages.
The "util" directory doesn't exist in our release directory. (I'd like to avoid having to make a copy of it there.)
I'm not entirely sure what they did with dojoExt.js there, but it seems to be required. I was able to make a simple "hello world" sort of test, but once I did dojo.require(js.mainLib), I got an error that dojo.behavior.add is not a method. So it seems to be they made a stripped-down dojo.js and put other required code in dojoExt.js.
My guess is that DOH is using the dojo in dojo15/dojo, but I need to include dojo15/release/dojo/dojo/dojoExt.js. I've tried every combination of dojoUrl, testModule, registerModulePath I can think of...also saw "boot" and "path" from other SE questions, even though I can't see in runner.html where they would even be used, but I tried throwing them in to see if they'd magically help anyway.
At the moment I don't really have the option to move code around, so I'm hoping to work this out with files where they currently are.
Did you try the bits I suggested over at a related question? We use the bits I detailed there to pull together a completely custom layout of dojo + util. Take a look at both boot and dojoUrl. I think it's runner.js that does most of the cunning stuff, rather than runner.html.
Is the problem that you can't arrange for dojoExt.js to get loaded? How does your other code arrange for it to get loaded? Is tests/base.js your test module?

Accessing model attribute from css/less file with Rails 3

The project I'm working on requires me to adapt the size of the elements on screen according to per-user setting. We're using Twitter bootstrap, so my first idea was to toy with the #basefont value, and it seems to do the trick.
However, I don't know how to access the user setting from the .less file. I tried using erb with .less.erb, but it looks like I don't have access to any code in my application.
Is there a way to get the value I'm looking for from the .less file, or - even better - a proper way to do this ?
Thanks for your time.
EDIT
Since I need to get the value at runtime, the way I tried won't work anyway, though I'm still interested on an answer. The only way I see to do what I want is to add a class according to the user setting. Again, I'd be glad to have alternatives.
I opted for css3 transform (and vendors implementation) property. It does the job, but seems to hit the GPU quite a lot. So still open to other answers. :)

Jade - way to add dynamic includes

I'd like to do something like the following within a jade template.
include page-content/#{view.template}
As this won't work I have ended up with.
-if(view.path==="/")
include ../page_content/home
-else if(view.path==="/login/")
include ../page_content/login
-else if(view.path==="/join/")
include ../page_content/join
-else if(view.path==="/user/")
include ../page_content/user
ad nauseum
I asked TJ whether it was possible, he replied
unfortunately no, they're compile-time includes, which is somewhat necessary for a few technical reasons that I wont get into but we may eventually need to add a dynamic alternative
I'm wondering if anyone has come up with any alternatives, for example using view helpers.
I'm stuck with a big config file to generate the views - and the if-else statements in the template , I know are going to come back and haunt me. :)
If this is possible using another engine, like ejs or mustache, I'd love to know.
Any ideas much appreciated.
Feels like way too much logic in the view to me. Seems like the best way to do this would be through a dynamicHelper or possibly a mixin