move i18n of UserActions to JS-side and make sentences translatable

This commit is contained in:
Kuba Brecka 2013-04-08 22:54:08 +02:00
parent 02bab415bd
commit 8d927418e8
6 changed files with 97 additions and 111 deletions

View file

@ -380,6 +380,7 @@ Discourse.User = Discourse.Model.extend({
var stat = Em.Object.create(s);
stat.set('isPM', stat.get('action_type') === Discourse.UserAction.NEW_PRIVATE_MESSAGE ||
stat.get('action_type') === Discourse.UserAction.GOT_PRIVATE_MESSAGE);
stat.set('description', Em.String.i18n('user_action_groups.' + stat.get('action_type')));
return stat;
}));
@ -438,7 +439,7 @@ Discourse.User.reopenClass({
found = true;
if (!g[k]) {
g[k] = Em.Object.create({
description: Em.String.i18n("user_action_descriptions." + k),
description: Em.String.i18n("user_action_groups." + k),
count: 0,
action_type: parseInt(k, 10)
});

View file

@ -8,6 +8,71 @@
**/
Discourse.UserAction = Discourse.Model.extend({
descriptionHtml: (function() {
var action = this.get('action_type');
var ua = Discourse.UserAction;
var actions = [ua.LIKE, ua.WAS_LIKED, ua.STAR, ua.EDIT, ua.BOOKMARK, ua.GOT_PRIVATE_MESSAGE, ua.NEW_PRIVATE_MESSAGE];
var icon = "";
var sentence = "";
var fill = function(s, links) {
for (var key in links) {
var link = links[key];
s = s.replace("<" + key + ">", "<a href='" + link + "'>");
s = s.replace("</" + key + ">", "</a>");
}
return s;
};
var sameUser = this.get('username') === Discourse.get('currentUser.username');
var params;
if (action === null || actions.indexOf(action) >= 0) {
params = { u: this.get('userUrl') };
if (this.get('isPM')) {
icon = '<i class="icon icon-envelope-alt" title="{{i18n user.stream.private_message}}"></i>';
if (sameUser) {
sentence = fill(Em.String.i18n('user_action.sent_by_you'), params);
} else {
sentence = fill(Em.String.i18n('user_action.sent_by_user', { user: this.get('name') }), params);
}
} else {
if (sameUser) {
sentence = fill(Em.String.i18n('user_action.posted_by_you'), params);
} else {
sentence = fill(Em.String.i18n('user_action.posted_by_user', { user: this.get('name') }), params);
}
}
} else if (action === ua.NEW_TOPIC) {
params = { u: this.get('userUrl'), t: this.get('replyUrl') };
if (sameUser) {
sentence = fill(Em.String.i18n('user_action.you_posted_topic'), params);
} else {
sentence = fill(Em.String.i18n('user_action.user_posted_topic', { user: this.get('name') }), params);
}
} else if (action === ua.POST || action === ua.RESPONSE) {
if (this.get('reply_to_post_number')) {
params = { u: this.get('userUrl'), t: this.get('postUrl') };
if (sameUser) {
sentence = fill(Em.String.i18n('user_action.you_replied_to_post', { post_number: '#' + this.get('reply_to_post_number') }), params);
} else {
sentence = fill(Em.String.i18n('user_action.user_replied_to_post', { user: this.get('name'), post_number: '#' + this.get('reply_to_post_number') }), params);
}
} else {
params = { u: this.get('userUrl'), t: this.get('replyUrl') };
if (sameUser) {
sentence = fill(Em.String.i18n('user_action.you_replied_to_topic'), params);
} else {
sentence = fill(Em.String.i18n('user_action.user_replied_to_topic', { user: this.get('name') }), params);
}
}
} else {
throw "Invalid user action: " + action;
}
return new Handlebars.SafeString(icon + " " + sentence);
}).property(),
userUrl: (function() {
return Discourse.Utilities.userUrl(this.get('username'));
}).property(),

View file

@ -7,31 +7,7 @@
<span class="title">
<a href="{{unbound postUrl}}">{{unbound title}}</a>
</span>
{{#unless description}}
<span class="type">
{{#if isPM}}
<i class="icon icon-envelope-alt" title="{{i18n user.stream.private_message}}"></i>
{{i18n user.stream.sent_by}}
{{else}}
{{i18n user.stream.posted_by}}
{{/if}}
</span>
{{/unless}}
<a class='name' href="{{unbound userUrl}}">{{personalizedName name usernamePath="username"}}</a>
{{#if description}}
<span class='type'>{{unbound description}}</span>
{{#if isPostAction}}
<a class="post-number" href="{{unbound replyUrl}}">
{{#if reply_to_post_number}}
#{{unbound reply_to_post_number}}
{{else}}
{{i18n user.stream.the_topic}}
{{/if}}
</a>
{{else}}
<span class="name">{{personalizedName view.parentView.parentView.user.name usernamePath="view.parentView.parentView.user.username"}}</span>
{{/if}}
{{/if}}
<span class="type">{{unbound descriptionHtml}}</span>
</div>
<p class='excerpt'>
{{{unbound excerpt}}}

View file

@ -36,7 +36,7 @@ class UserAction < ActiveRecord::Base
].each_with_index.to_a.flatten]
def self.stats(user_id, guardian)
results = UserAction.select("action_type, COUNT(*) count, '' AS description")
results = UserAction.select("action_type, COUNT(*) count")
.joins(:target_topic)
.where(user_id: user_id)
.group('action_type')
@ -53,9 +53,6 @@ class UserAction < ActiveRecord::Base
results = results.to_a
results.sort! { |a,b| ORDER[a.action_type] <=> ORDER[b.action_type] }
results.each do |row|
row.description = self.description(row.action_type, detailed: true)
end
results
end
@ -122,7 +119,6 @@ JOIN users pu on pu.id = COALESCE(p.user_id, t.user_id)
data.each do |row|
row["action_type"] = row["action_type"].to_i
row["description"] = self.description(row["action_type"])
row["created_at"] = DateTime.parse(row["created_at"])
# we should probably cache the excerpts in the db at some point
row["excerpt"] = PrettyText.excerpt(row["cooked"],300) if row["cooked"]
@ -137,60 +133,6 @@ JOIN users pu on pu.id = COALESCE(p.user_id, t.user_id)
data
end
def self.description(row, opts = {})
t = I18n.t('user_action_descriptions')
if opts[:detailed]
# will localize as soon as we stablize the names here
desc = case row.to_i
when BOOKMARK
t[:bookmarks]
when NEW_TOPIC
t[:topics]
when WAS_LIKED
t[:likes_received]
when LIKE
t[:likes_given]
when RESPONSE
t[:responses]
when POST
t[:posts]
when MENTION
t[:mentions]
when QUOTE
t[:quotes]
when EDIT
t[:edits]
when STAR
t[:favorites]
when NEW_PRIVATE_MESSAGE
t[:sent_items]
when GOT_PRIVATE_MESSAGE
t[:inbox]
end
else
desc =
case row.to_i
when NEW_TOPIC
then t[:posted]
when LIKE,WAS_LIKED
then t[:liked]
when RESPONSE,POST
then t[:responded_to]
when BOOKMARK
then t[:bookmarked]
when MENTION
then t[:mentioned]
when QUOTE
then t[:quoted]
when STAR
then t[:favorited]
when EDIT
then t[:edited]
end
end
desc
end
def self.log_action!(hash)
require_parameters(hash, :action_type, :user_id, :acting_user_id, :target_topic_id, :target_post_id)
transaction(requires_new: true) do

View file

@ -48,8 +48,35 @@ en:
saving: "Saving..."
saved: "Saved!"
user_action_descriptions:
user_action:
user_posted_topic: "<u>{{user}}</u> posted <t>the topic</t>"
you_posted_topic: "<u>You</u> posted <t>the topic</t>"
user_replied_to_post: "<u>{{user}}</u> replied to <t>{{post_number}}</t>"
you_replied_to_post: "<u>You</u> replied to <t>{{post_number}}</t>"
user_replied_to_topic: "<u>{{user}}</u> replied to <t>the topic</t>"
you_replied_to_topic: "<u>You</u> replied to <t>the topic</t>"
user_mentioned_user: "<u1>{{user}}</u1> mentioned <u2>{{another_user}}</u2>"
user_mentioned_you: "<u1>{{user}}</u1> mentioned <u2>you</u2>"
posted_by_user: "Posted by <u>{{user}}</u>"
posted_by_you: "Posted by <u>you</u>"
sent_by_user: "Sent by <u>{{user}}</u>"
sent_by_you: "Sent by <u>you</u>"
user_action_groups:
"1": "Likes Given"
"2": "Likes Received"
"3": "Bookmarks"
"4": "Topics"
"5": "Posts"
"6": "Responses"
"7": "Mentions"
"9": "Quotes"
"10": "Favorites"
"11": "Edits"
"12": "Sent Items"
"13": "Inbox"
user:
information: "User Information"

View file

@ -518,31 +518,6 @@ en:
most_recent_poster: "Most Recent Poster"
frequent_poster: "Frequent Poster"
user_action_descriptions:
bookmarks: "Bookmarks"
topics: "Topics"
likes_received: "Likes Received"
likes_given: "Likes Given"
responses: "Responses"
topic_responses: "Topic Responses"
posts: "Posts"
mentions: "Mentions"
quotes: "Quotes"
edits: "Edits"
favorites: "Favorites"
sent_items: "Sent Items"
inbox: "Inbox"
was_liked: "was liked"
liked: "liked"
bookmarked: "bookmarked"
posted: "posted"
responded_to: "replied to"
mentioned: "mentioned"
quoted: "quoted"
favorited: "favorited"
edited: "edited"
move_posts:
moderator_post:
one: "I moved a post to a new topic: %{topic_link}"