Skip to content

Commit

Permalink
move i18n of UserActions to JS-side and make sentences translatable (…
Browse files Browse the repository at this point in the history
…take 3)
  • Loading branch information
kubamracek committed Apr 18, 2013
1 parent 4bd4e4e commit d355e42
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 111 deletions.
3 changes: 2 additions & 1 deletion app/assets/javascripts/discourse/models/user.js
Expand Up @@ -370,6 +370,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;
}));

Expand Down Expand Up @@ -429,7 +430,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)
});
Expand Down
71 changes: 71 additions & 0 deletions app/assets/javascripts/discourse/models/user_action.js
Expand Up @@ -8,6 +8,77 @@
**/
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 sameUser = (this.get('username') === Discourse.get('currentUser.username'));

if (action === null || actions.indexOf(action) >= 0) {
if (this.get('isPM')) {
icon = '<i class="icon icon-envelope-alt" title="{{i18n user.stream.private_message}}"></i>';
if (sameUser) {
sentence = Em.String.i18n('user_action.sent_by_you', { userUrl: this.get('userUrl') });
} else {
sentence = Em.String.i18n('user_action.sent_by_user', { user: this.get('name'), userUrl: this.get('userUrl') });
}
} else {
if (sameUser) {
sentence = Em.String.i18n('user_action.posted_by_you', { userUrl: this.get('userUrl') });
} else {
sentence = Em.String.i18n('user_action.posted_by_user', { user: this.get('name'), userUrl: this.get('userUrl') });
}
}
} else if (action === ua.NEW_TOPIC) {
if (sameUser) {
sentence = Em.String.i18n('user_action.you_posted_topic', { userUrl: this.get('userUrl'), topicUrl: this.get('replyUrl') });
} else {
sentence = Em.String.i18n('user_action.user_posted_topic', { user: this.get('name'), userUrl: this.get('userUrl'), topicUrl: this.get('replyUrl') });
}
} else if (action === ua.POST || action === ua.RESPONSE) {
if (this.get('reply_to_post_number')) {
if (sameUser) {
sentence = Em.String.i18n('user_action.you_replied_to_post', { post_number: '#' + this.get('reply_to_post_number'),
userUrl: this.get('userUrl'), postUrl: this.get('postUrl') });
} else {
sentence = Em.String.i18n('user_action.user_replied_to_post', { user: this.get('name'),
post_number: '#' + this.get('reply_to_post_number'), userUrl: this.get('userUrl'), postUrl: this.get('postUrl') });
}
} else {
if (sameUser) {
sentence = Em.String.i18n('user_action.you_replied_to_topic', { userUrl: this.get('userUrl'),
topicUrl: this.get('replyUrl') });
} else {
sentence = Em.String.i18n('user_action.user_replied_to_topic', { user: this.get('name'),
userUrl: this.get('userUrl'), topicUrl: this.get('replyUrl') });
}
}
} else if (action === ua.MENTION) {
if (sameUser) {
sentence = Em.String.i18n('user_action.you_mentioned_user', { user: this.get('target_name'),
user1Url: this.get('userUrl'), user2Url: this.get('targetUserUrl') });
} else {
if (this.get('target_username') === Discourse.get('currentUser.username')) {
sentence = Em.String.i18n('user_action.user_mentioned_you', { user: this.get('name'),
user1Url: this.get('userUrl'), user2Url: this.get('targetUserUrl') });
} else {
sentence = Em.String.i18n('user_action.user_mentioned_user', { user: this.get('name'),
another_user: this.get('target_name'), user1Url: this.get('userUrl'), user2Url: this.get('targetUserUrl') });
}
}
} else {
Ember.debug("Invalid user action: " + action);
}

return new Handlebars.SafeString(icon + " " + sentence);
}).property(),

targetUserUrl: (function() {
return Discourse.Utilities.userUrl(this.get('target_username'));
}).property(),

userUrl: (function() {
return Discourse.Utilities.userUrl(this.get('username'));
}).property(),
Expand Down
Expand Up @@ -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}}}
Expand Down
62 changes: 3 additions & 59 deletions app/models/user_action.rb
Expand Up @@ -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')
Expand All @@ -57,9 +57,6 @@ def self.stats(user_id, guardian)
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
Expand All @@ -83,6 +80,7 @@ def self.stream(opts={})
builder = SqlBuilder.new("
SELECT
t.title, a.action_type, a.created_at, t.id topic_id,
a.user_id AS target_user_id, au.name AS target_name, au.username AS target_username,
coalesce(p.post_number, 1) post_number,
p.reply_to_post_number,
pu.email ,pu.username, pu.name, pu.id user_id,
Expand All @@ -94,6 +92,7 @@ def self.stream(opts={})
JOIN posts p2 on p2.topic_id = a.target_topic_id and p2.post_number = 1
JOIN users u on u.id = a.acting_user_id
JOIN users pu on pu.id = COALESCE(p.user_id, t.user_id)
JOIN users au on au.id = a.user_id
/*where*/
/*order_by*/
/*offset*/
Expand Down Expand Up @@ -126,7 +125,6 @@ def self.stream(opts={})

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"]
Expand All @@ -141,60 +139,6 @@ def self.stream(opts={})
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
Expand Down
30 changes: 29 additions & 1 deletion config/locales/client.en.yml
Expand Up @@ -49,8 +49,36 @@ en:
saving: "Saving..."
saved: "Saved!"

user_action_descriptions:
user_action:
user_posted_topic: "<a href='{{userUrl}}'>{{user}}</a> posted <a href='{{topicUrl}}'>the topic</a>"
you_posted_topic: "<a href='{{userUrl}}'>You</a> posted <a href='{{topicUrl}}'>the topic</a>"
user_replied_to_post: "<a href='{{userUrl}}'>{{user}}</a> replied to <a href='{{postUrl}}'>{{post_number}}</a>"
you_replied_to_post: "<a href='{{userUrl}}'>You</a> replied to <a href='{{postUrl}}'>{{post_number}}</a>"
user_replied_to_topic: "<a href='{{userUrl}}'>{{user}}</a> replied to <a href='{{topicUrl}}'>the topic</a>"
you_replied_to_topic: "<a href='{{userUrl}}'>You</a> replied to <a href='{{topicUrl}}'>the topic</a>"

user_mentioned_user: "<a href='{{user1Url}}'>{{user}}</a> mentioned <a href='{{user2Url}}'>{{another_user}}</a>"
user_mentioned_you: "<a href='{{user1Url}}'>{{user}}</a> mentioned <a href='{{user2Url}}'>you</a>"
you_mentioned_user: "<a href='{{user1Url}}'>You</a> mentioned <a href='{{user2Url}}'>{{user}}</a>"

posted_by_user: "Posted by <a href='{{userUrl}}'>{{user}}</a>"
posted_by_you: "Posted by <a href='{{userUrl}}'>you</a>"
sent_by_user: "Sent by <a href='{{userUrl}}'>{{user}}</a>"
sent_by_you: "Sent by <a href='{{userUrl}}'>you</a>"

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"
Expand Down
25 changes: 0 additions & 25 deletions config/locales/server.en.yml
Expand Up @@ -544,31 +544,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}"
Expand Down

0 comments on commit d355e42

Please sign in to comment.