Skip to content

Commit

Permalink
Merge pull request #13 from bb-io/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ce-nistal authored Oct 3, 2024
2 parents d55d1ca + 4e0c569 commit 01adc13
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 4 deletions.
10 changes: 8 additions & 2 deletions Apps.Crowdin/Actions/TranslationActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Crowdin.Api.StringTranslations;
using Crowdin.Api.Translations;
using RestSharp;
using Apps.Crowdin.Models.Request;

namespace Apps.Crowdin.Actions;

Expand All @@ -35,14 +36,18 @@ public TranslationActions(InvocationContext invocationContext, IFileManagementCl
[Action("Apply pre-translation", Description = "Apply pre-translation to chosen files")]
public async Task<PreTranslationEntity> PreTranslate(
[ActionParameter] ProjectRequest project,
[ActionParameter] PreTranslateRequest input)
[ActionParameter] PreTranslateRequest input,
[ActionParameter] UserRequest user)
{
var intProjectId = IntParser.Parse(project.ProjectId, nameof(project.ProjectId));
var intEngineId = IntParser.Parse(input.EngineId, nameof(input.EngineId));

var client = new CrowdinClient(Creds);

PreTranslationMethod? method = input.Method is null ? null : input.Method == "Mt" ? PreTranslationMethod.Mt : PreTranslationMethod.Tm;
PreTranslationMethod? method = input.Method is null ? null :
input.Method == "Mt" ? PreTranslationMethod.Mt :
input.Method == "Tm" ? PreTranslationMethod.Tm :
PreTranslationMethod.Ai;
AutoApproveOption? option = input.AutoApproveOption is null ? null : input.AutoApproveOption == "None" ? AutoApproveOption.None : input.AutoApproveOption == "All" ? AutoApproveOption.All : input.AutoApproveOption == "ExceptAutoSubstituted" ? AutoApproveOption.ExceptAutoSubstituted : AutoApproveOption.PerfectMatchOnly;

var request = new ApplyPreTranslationRequest
Expand All @@ -51,6 +56,7 @@ public async Task<PreTranslationEntity> PreTranslate(
FileIds = input.FileIds.Select(fileId => IntParser.Parse(fileId, nameof(fileId))!.Value).ToList(),
EngineId = intEngineId,
Method = method,
AiPromptId = input.aiPromptId is null ? null : IntParser.Parse(input.aiPromptId, nameof(input.aiPromptId)),
AutoApproveOption = option,
DuplicateTranslations = input.DuplicateTranslations,
TranslateUntranslatedOnly = input.TranslateUntranslatedOnly,
Expand Down
4 changes: 2 additions & 2 deletions Apps.Crowdin/Apps.Crowdin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
<Nullable>enable</Nullable>
<Product>Crowdin</Product>
<Description>Cloud-based solution that streamlines localization management</Description>
<Version>1.0.9</Version>
<Version>1.0.10</Version>
<AssemblyName>Apps.Crowdin</AssemblyName>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Blackbird.Applications.Sdk.Common" Version="2.8.0" />
<PackageReference Include="Blackbird.Applications.SDK.Extensions.FileManagement" Version="1.0.1" />
<PackageReference Include="Blackbird.Applications.Sdk.Utils" Version="1.0.25" />
<PackageReference Include="Crowdin.Api" Version="2.23.1" />
<PackageReference Include="Crowdin.Api" Version="2.25.0" />
<PackageReference Include="MimeTypes" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
55 changes: 55 additions & 0 deletions Apps.Crowdin/DataSourceHandlers/AiPromptIdDataHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Apps.Crowdin.Api.RestSharp;
using Apps.Crowdin.Models.Entities;
using Apps.Crowdin.Models.Request;
using Apps.Crowdin.Models.Request.Project;
using Apps.Crowdin.Models.Response;
using Apps.Crowdin.Utils;
using Blackbird.Applications.Sdk.Common;
using Blackbird.Applications.Sdk.Common.Authentication;
using Blackbird.Applications.Sdk.Common.Dynamic;
using Blackbird.Applications.Sdk.Common.Invocation;
using Crowdin.Api;
using Newtonsoft.Json;
using RestSharp;

namespace Apps.Crowdin.DataSourceHandlers;

public class AiPromptIdDataHandler(
InvocationContext invocationContext,
[ActionParameter] ProjectRequest project,
[ActionParameter] UserRequest user)

: BaseInvocable(invocationContext), IAsyncDataSourceHandler
{
private AuthenticationCredentialsProvider[] Creds =>
InvocationContext.AuthenticationCredentialsProviders.ToArray();

public async Task<Dictionary<string, string>> GetDataAsync(DataSourceContext context,
CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(project.ProjectId) || string.IsNullOrEmpty(user.UserId))
throw new("You should input Project ID and User ID first");

var client = new CrowdinRestClient();

var items = await Paginator.Paginate(async (lim, offset)
=>
{
var request =
new CrowdinRestRequest(
$"/users/{user.UserId}/ai/prompts?limit={lim}&offset={offset}",
Method.Get, Creds);
request.AddQueryParameter("projectId", project.ProjectId);
request.AddQueryParameter("action", "pre_translate");
var response = await client.ExecuteAsync(request, cancellationToken: cancellationToken);
return JsonConvert.DeserializeObject<ResponseList<DataResponse<AiPromptEntity>>>(response.Content);
});

return items
.Select(x => x.Data)
.Where(x => context.SearchString == null ||
x.Name.Contains(context.SearchString, StringComparison.OrdinalIgnoreCase))
.Take(20)
.ToDictionary(x => x.Id.ToString(), x => x.Name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class PreTranslationMethodHandler : IStaticDataSourceHandler
{
{ "Mt", "Machine translation" },
{ "Tm", "Translation memory" },
{ "Ai", "Artificial intelligence" }
};
}
}
14 changes: 14 additions & 0 deletions Apps.Crowdin/Models/Entities/AiPromptEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Apps.Crowdin.Models.Entities
{
public class AiPromptEntity
{
public string Id { get; set; }
public string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ public class PreTranslateRequest
[Display("Pre translation method")]
public string? Method { get; set; }

[DataSource(typeof(AiPromptIdDataHandler))]
[Display("AI prompt ID")]
public string? aiPromptId { get; set; }

}
15 changes: 15 additions & 0 deletions Apps.Crowdin/Models/Request/UserRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Blackbird.Applications.Sdk.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Apps.Crowdin.Models.Request
{
public class UserRequest
{
[Display("User ID")]
public string? UserId { get; set; }
}
}

0 comments on commit 01adc13

Please sign in to comment.