From 8d927418e8edf3c541003276ad9068b84a0ce0ab Mon Sep 17 00:00:00 2001 From: Kuba Brecka Date: Mon, 8 Apr 2013 22:54:08 +0200 Subject: [PATCH] move i18n of UserActions to JS-side and make sentences translatable --- .../javascripts/discourse/models/user.js | 3 +- .../discourse/models/user_action.js | 65 +++++++++++++++++++ .../templates/user/stream.js.handlebars | 26 +------- app/models/user_action.rb | 60 +---------------- config/locales/client.en.yml | 29 ++++++++- config/locales/server.en.yml | 25 ------- 6 files changed, 97 insertions(+), 111 deletions(-) diff --git a/app/assets/javascripts/discourse/models/user.js b/app/assets/javascripts/discourse/models/user.js index 1d0dc5fa470..d09b186bd73 100644 --- a/app/assets/javascripts/discourse/models/user.js +++ b/app/assets/javascripts/discourse/models/user.js @@ -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) }); diff --git a/app/assets/javascripts/discourse/models/user_action.js b/app/assets/javascripts/discourse/models/user_action.js index 207bf362e66..610ebda590f 100644 --- a/app/assets/javascripts/discourse/models/user_action.js +++ b/app/assets/javascripts/discourse/models/user_action.js @@ -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 + ">", ""); + s = s.replace("", ""); + } + 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 = ''; + 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(), diff --git a/app/assets/javascripts/discourse/templates/user/stream.js.handlebars b/app/assets/javascripts/discourse/templates/user/stream.js.handlebars index 4c53bfb4830..af55af78c54 100644 --- a/app/assets/javascripts/discourse/templates/user/stream.js.handlebars +++ b/app/assets/javascripts/discourse/templates/user/stream.js.handlebars @@ -7,31 +7,7 @@ {{unbound title}} - {{#unless description}} - - {{#if isPM}} - - {{i18n user.stream.sent_by}} - {{else}} - {{i18n user.stream.posted_by}} - {{/if}} - - {{/unless}} - {{personalizedName name usernamePath="username"}} - {{#if description}} - {{unbound description}} - {{#if isPostAction}} - - {{#if reply_to_post_number}} - #{{unbound reply_to_post_number}} - {{else}} - {{i18n user.stream.the_topic}} - {{/if}} - - {{else}} - {{personalizedName view.parentView.parentView.user.name usernamePath="view.parentView.parentView.user.username"}} - {{/if}} - {{/if}} + {{unbound descriptionHtml}}

{{{unbound excerpt}}} diff --git a/app/models/user_action.rb b/app/models/user_action.rb index a7278a48a92..052c8f15fcf 100644 --- a/app/models/user_action.rb +++ b/app/models/user_action.rb @@ -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 diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 7e3a237ebe4..276f275cec8 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -48,8 +48,35 @@ en: saving: "Saving..." saved: "Saved!" - user_action_descriptions: + user_action: + user_posted_topic: "{{user}} posted the topic" + you_posted_topic: "You posted the topic" + user_replied_to_post: "{{user}} replied to {{post_number}}" + you_replied_to_post: "You replied to {{post_number}}" + user_replied_to_topic: "{{user}} replied to the topic" + you_replied_to_topic: "You replied to the topic" + + user_mentioned_user: "{{user}} mentioned {{another_user}}" + user_mentioned_you: "{{user}} mentioned you" + + posted_by_user: "Posted by {{user}}" + posted_by_you: "Posted by you" + sent_by_user: "Sent by {{user}}" + sent_by_you: "Sent by you" + + 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" diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 572a9705232..ecce6479727 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -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}"