Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: translations #941

Merged
merged 2 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/API/Akkoma/Translation.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public class Tuba.API.AkkomaTranslation : Entity {
public string text { get; set; default = ""; }
public string detected_language { get; set; default = ""; }

public static AkkomaTranslation from (Json.Node node) throws Error {
return Entity.from_json (typeof (API.AkkomaTranslation), node) as API.AkkomaTranslation;
}
}
2 changes: 1 addition & 1 deletion src/API/Akkoma/meson.build
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
sources += files(
'Source.vala',
'Translation.vala',
)
1 change: 1 addition & 0 deletions src/API/Attachment.vala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class Tuba.API.Attachment : Entity, Widgetizable {
set { this.t_preview_url = value; }
get { return (this.t_preview_url == null || this.t_preview_url == "") ? url : t_preview_url; }
}
public string? tuba_translated_alt_text { get; set; default = null; }

public File? source_file { get; set; }

Expand Down
2 changes: 2 additions & 0 deletions src/API/Instance.vala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class Tuba.API.Instance : Entity {
public API.Pleroma.Instance? pleroma { get; set; default = null; }
public Gee.ArrayList<Rule>? rules { get; set; }

public bool tuba_can_translate { get; set; default=false; }

public override Type deserialize_array_type (string prop) {
switch (prop) {
case "languages":
Expand Down
14 changes: 14 additions & 0 deletions src/API/InstanceV2.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class Tuba.API.InstanceV2 : Entity {
public class Configuration : Entity {
public class Translation : Entity {
public bool enabled { get; set; default = false; }
}
public Translation translation { get; set; default = null; }
}

public Configuration configuration { get; set; default = null; }

public static InstanceV2 from (Json.Node node) throws Error {
return Entity.from_json (typeof (API.InstanceV2), node) as API.InstanceV2;
}
}
1 change: 1 addition & 0 deletions src/API/PollOption.vala
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
public class Tuba.API.PollOption : Entity {
public string? title { get; set; }
public int64 votes_count { get; set; default=0; }
public string? tuba_translated_title { get; set; default = null; }
}
2 changes: 2 additions & 0 deletions src/API/Status.vala
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public class Tuba.API.Status : Entity, Widgetizable {

public Tuba.Views.Thread.ThreadRole tuba_thread_role { get; set; default = Tuba.Views.Thread.ThreadRole.NONE; }
public bool tuba_spoiler_revealed { get; set; default = settings.show_spoilers; }
public bool tuba_translatable { get; set; default = false; }

// public string clean_content {
// get {
Expand All @@ -108,6 +109,7 @@ public class Tuba.API.Status : Entity, Widgetizable {
return _language;
}
set {
if (value != null) tuba_translatable = true;
_language = value ?? settings.default_language;
}
}
Expand Down
43 changes: 43 additions & 0 deletions src/API/Translation.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
public class Tuba.API.Translation : Entity {
public class Poll : Entity {
public class Option : Entity {
public string title { get; set; default = ""; }
}
public string id { get; set; default = ""; }
public Gee.ArrayList<Option>? options { get; set; default = null; }

public override Type deserialize_array_type (string prop) {
switch (prop) {
case "options":
return typeof (Option);
}

return base.deserialize_array_type (prop);
}
}

public class Attachment : Entity {
public string id { get; set; default = ""; }
public string description { get; set; default = ""; }
}

public string content { get; set; default = ""; }
public string spoiler_text { get; set; default = ""; }
public Poll? poll { get; set; default = null; }
public Gee.ArrayList<Attachment>? media_attachments { get; set; default = null; }
public string detected_source_language { get; set; default = ""; }
public string provider { get; set; default = ""; }

public static Translation from (Json.Node node) throws Error {
return Entity.from_json (typeof (API.Translation), node) as API.Translation;
}

public override Type deserialize_array_type (string prop) {
switch (prop) {
case "media-attachments":
return typeof (Attachment);
}

return base.deserialize_array_type (prop);
}
}
4 changes: 3 additions & 1 deletion src/API/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ sources += files(
'Entity.vala',
'Funkwhale.vala',
'Instance.vala',
'InstanceV2.vala',
'List.vala',
'Mention.vala',
'Notification.vala',
Expand All @@ -23,10 +24,11 @@ sources += files(
'Suggestion.vala',
'Tag.vala',
'TagHistory.vala',
'Translation.vala',
)

subdir('Account')
# subdir('Akkoma')
subdir('Akkoma')
subdir('BookWyrm')
subdir('Filters')
subdir('Funkwhale')
Expand Down
13 changes: 13 additions & 0 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace Tuba {
public static bool is_flatpak = false;
public static string cache_path;

public static string default_locale;

public class Application : Adw.Application {

public Dialogs.MainWindow? main_window { get; set; }
Expand Down Expand Up @@ -248,6 +250,17 @@ namespace Tuba {
Adw.init ();
GtkSource.init ();

var t_default_locale = Gtk.get_default_language ().to_string ();
if (t_default_locale == "c") {
default_locale = "en-US";
} else {
if (t_default_locale.index_of_char ('-') != -1) {
var tdl_parts = t_default_locale.split ("-", 2);
t_default_locale = @"$(tdl_parts[0].down ())-$(tdl_parts[1].up ())";
}
default_locale = t_default_locale;
}

settings = new Settings ();
streams = new Streams ();
network = new Network ();
Expand Down
21 changes: 21 additions & 0 deletions src/Services/Accounts/InstanceAccount.vala
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,27 @@ public class Tuba.InstanceAccount : API.Account, Streamable {
supported_mime_types.append (new StatusContentType (content_type));
}
}

if (instance_info.pleroma == null) {
gather_v2_instance_info ();
} else if (instance_info.pleroma.metadata != null && instance_info.pleroma.metadata.features != null) {
instance_info.tuba_can_translate = "akkoma:machine_translation" in instance_info.pleroma.metadata.features;
}
})
.exec ();
}

private void gather_v2_instance_info () {
new Request.GET ("/api/v2/instance")
.with_account (this)
.then ((in_stream) => {
var parser = Network.get_parser_from_inputstream (in_stream);
var node = network.parse_node (parser);
var instance_v2 = API.InstanceV2.from (node);

if (instance_v2 != null && instance_v2.configuration != null && instance_v2.configuration.translation != null) {
instance_info.tuba_can_translate = instance_v2.configuration.translation.enabled;
}
})
.exec ();
}
Expand Down
9 changes: 8 additions & 1 deletion src/Widgets/Attachment/Image.vala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ public class Tuba.Widgets.Attachment.Image : Widgets.Attachment.Item {
ulong pic_paintable_id = 0;
protected override void on_rebind () {
base.on_rebind ();
pic.alternative_text = entity == null ? null : entity.description;

if (entity == null) {
pic.alternative_text = null;
} else if (entity.tuba_translated_alt_text != null) {
pic.alternative_text = entity.tuba_translated_alt_text;
} else {
pic.alternative_text = entity.description;
}

if (pic_paintable_id != 0) {
pic.disconnect (pic_paintable_id);
Expand Down
2 changes: 1 addition & 1 deletion src/Widgets/Attachment/Item.vala
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public class Tuba.Widgets.Attachment.Item : Adw.Bin {

private void on_alt_text_btn_clicked () {
if (entity != null && entity.description != null)
create_alt_text_dialog (entity.description, true);
create_alt_text_dialog (entity.tuba_translated_alt_text == null ? entity.description : entity.tuba_translated_alt_text, true);
}

protected Adw.Dialog create_alt_text_dialog (string alt_text, bool show = false) {
Expand Down
Loading
Loading