Hi I need to support different languages in my components, so I would like to ask a question regarding this.
This is from another my topic, but might be useful here as well:
I have some pre-conditions that I should mention:
Right now the application is old-fashioned multi-page Java application that uses JSP. What we are doing right now is migrating some pieces that we can to canjs. It means that components that I create should have an ability to be used in canjs pages (stache) and in just plain HTML pages with plain JS (or jQuery).
I’m using canjs 2.2.9 I know that its pretty old, but unfortunately this is something that I can’t change in the nearest future.
I created small component (still WIP, but can represent my question):
https://dl.dropboxusercontent.com/u/25491580/can-test/index.html
(Sorry it’s not JSBin because there are different files, folders so it was easier to share in that way, but I can probably post it on Github if its easier than checking sources in devtools)
Basically this is textarea that should count amount of characters, change height and react when character limit exceeds (twitter / facebook like).
I would like to have an ability to use translated properties inside templates. Such properties can contain string with params like: Hello ${username} bla bla bla ${author}
. Such strings may have different word order depends on language. Straightforward solution for me is to have some helper like:
{{#l20n `Hello ${username} bla bla bla ${author}` username author}}
This should work on templates side. But I’m stucked in understanding how I should get strings to use and where to store them…and how to use them in can-component
Previously we used some custom JSP
tags that just render correct strings into HTML that servlet returns us (it returns HTML
not JSON
). So we had no issues with translations because it was done on server-side.
What I think might work is to have some ajax
servlet that returns me JSON
object that contains map with key - value pairs. After I put response to can-component's viewModel
in order to have an access there and pass values to helper above. If I have multiple instances of components on the same page I also should implement some client-side translations storage in order to check this store before making request maybe we already requested this prop.
But such approach looks quite complicated and I have to put translated strings into components view model which seems incorrect to me. Because this isn’t data, this is text.
For example I need to support translations here:
<super-input placeholder="Congratulate this person ..." limit="500"></super-input>
<button can-click="addComment">Add Comment</button>
or here:
<div class="commentList" style="height: {{expanderHeight}}px">
{{#each comments}}
<comment-item comment="{.}" is-userpic-enabled="{{isUserpicEnabled}}" is-profile-enabled="{{isProfileEnabled}}"></comment-item>
{{/each}}
</div>
{{#moreThanShowLimit}}
<div class="commentList-actionPanel">
<button class="a-btn a-btn--secondary" can-click="{toggleExpander}">
<span class="a-btn-text">{{#isCollapsed}}View All Congratulations{{else}}Hide All Congratulations{{/isCollapsed}}</span>
</button>
</div>
{{/moreThanShowLimit}}
Maybe someone had real production experience and can help me with this ?