-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DEV: Added compatibility with the Glimmer Post Menu (#173)
- Loading branch information
Showing
8 changed files
with
216 additions
and
45 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
assets/javascripts/discourse/components/post-menu/toggle-translation-button.gjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import Component from "@glimmer/component"; | ||
import { action } from "@ember/object"; | ||
import { inject as service } from "@ember/service"; | ||
import DButton from "discourse/components/d-button"; | ||
import concatClass from "discourse/helpers/concat-class"; | ||
import { popupAjaxError } from "discourse/lib/ajax-error"; | ||
|
||
export default class ToggleTranslationButton extends Component { | ||
static shouldRender(args) { | ||
return args.post.can_translate; | ||
} | ||
|
||
@service modal; | ||
@service translator; | ||
|
||
get isTranslating() { | ||
return this.args.post.isTranslating; | ||
} | ||
|
||
get isTranslated() { | ||
return this.args.post.isTranslated; | ||
} | ||
|
||
get title() { | ||
if (this.isTranslating) { | ||
return "translator.translating"; | ||
} | ||
|
||
return this.isTranslated | ||
? "translator.hide_translation" | ||
: "translator.view_translation"; | ||
} | ||
|
||
@action | ||
hideTranslation() { | ||
this.args.post.isTranslated = false; | ||
this.args.post.isTranslating = false; | ||
this.translator.clearPostTranslation(this.args.post); | ||
} | ||
|
||
@action | ||
toggleTranslation() { | ||
return this.args.post.isTranslated | ||
? this.hideTranslation() | ||
: this.translate(); | ||
} | ||
|
||
@action | ||
async translate() { | ||
const post = this.args.post; | ||
post.isTranslating = true; | ||
|
||
try { | ||
await this.translator.translatePost(post); | ||
post.isTranslated = true; | ||
} catch (error) { | ||
this.translator.clearPostTranslation(this.args.post); | ||
post.isTranslated = false; | ||
popupAjaxError(error); | ||
} finally { | ||
post.isTranslating = false; | ||
} | ||
} | ||
|
||
<template> | ||
<DButton | ||
class={{concatClass | ||
"post-action-menu__translate" | ||
(if this.isTranslated "translated") | ||
}} | ||
...attributes | ||
@action={{this.toggleTranslation}} | ||
@disabled={{this.isTranslating}} | ||
@icon="globe" | ||
@label={{if @showLabel this.title}} | ||
@title={{this.title}} | ||
/> | ||
</template> | ||
} |
60 changes: 60 additions & 0 deletions
60
assets/javascripts/discourse/components/translated-post.gjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import Component from "@glimmer/component"; | ||
import { inject as service } from "@ember/service"; | ||
import { htmlSafe } from "@ember/template"; | ||
import ConditionalLoadingSpinner from "discourse/components/conditional-loading-spinner"; | ||
import i18n from "discourse-common/helpers/i18n"; | ||
|
||
export default class TranslatedPost extends Component { | ||
static shouldRender(args) { | ||
return args.post.isTranslated || args.post.isTranslating; | ||
} | ||
|
||
@service siteSettings; | ||
|
||
get loading() { | ||
return this.post.isTranslating; | ||
} | ||
|
||
get isTranslated() { | ||
return this.post.isTranslated; | ||
} | ||
|
||
get post() { | ||
return this.args.outletArgs.post; | ||
} | ||
|
||
get translatedText() { | ||
return this.post.translatedText; | ||
} | ||
|
||
get translatedTitle() { | ||
return this.post.translatedTitle; | ||
} | ||
|
||
<template> | ||
<div class="post-translation"> | ||
<ConditionalLoadingSpinner | ||
class="post-translation" | ||
@condition={{this.loading}} | ||
@size="small" | ||
> | ||
<hr /> | ||
{{#if this.translatedTitle}} | ||
<div class="topic-attribution"> | ||
{{this.translatedTitle}} | ||
</div> | ||
{{/if}} | ||
<div class="post-attribution"> | ||
{{i18n | ||
"translator.translated_from" | ||
language=this.post.detectedLang | ||
translator=this.siteSettings.translator | ||
}} | ||
</div> | ||
<div class="cooked"> | ||
{{htmlSafe this.post.translatedText}} | ||
</div> | ||
</ConditionalLoadingSpinner> | ||
</div> | ||
</template> | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import Service from "@ember/service"; | ||
import { ajax } from "discourse/lib/ajax"; | ||
|
||
export default class TranslatorService extends Service { | ||
async translatePost(post) { | ||
const response = await ajax("/translator/translate", { | ||
type: "POST", | ||
data: { post_id: post.id }, | ||
}); | ||
|
||
post.detectedLang = response.detected_lang; | ||
post.translatedText = response.translation; | ||
post.translatedTitle = response.title_translation; | ||
} | ||
|
||
clearPostTranslation(post) { | ||
post.detectedLang = null; | ||
post.translatedText = null; | ||
post.translatedTitle = null; | ||
} | ||
} |
17 changes: 0 additions & 17 deletions
17
assets/javascripts/discourse/templates/components/translated-post.hbs
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
assets/javascripts/discourse/templates/connectors/post-after-cooked/translate.hbs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters