2025-04-24 14:49:32 +00:00
using AIStudio.Dialogs.Settings ;
2025-04-25 13:29:17 +00:00
using AIStudio.Tools.PluginSystem ;
using Microsoft.Extensions.FileProviders ;
2025-04-24 14:49:32 +00:00
namespace AIStudio.Assistants.I18N ;
public partial class AssistantI18N : AssistantBaseCore < SettingsDialogI18N >
{
public override Tools . Components Component = > Tools . Components . I18N_ASSISTANT ;
protected override string Title = > "Localization" ;
protected override string Description = >
"" "
Translate MindWork AI Studio text content into another language .
"" ";
protected override string SystemPrompt = >
"" "
TODO
"" ";
protected override bool AllowProfiles = > false ;
2025-04-25 15:43:09 +00:00
protected override bool ShowResult = > false ;
2025-04-24 14:49:32 +00:00
protected override IReadOnlyList < IButtonData > FooterButtons = > [ ] ;
2025-04-25 15:43:09 +00:00
protected override string SubmitText = > "Localize AI Studio & generate the Lua code" ;
2025-04-24 14:49:32 +00:00
protected override Func < Task > SubmitAction = > this . LocalizeText ;
2025-04-25 13:29:17 +00:00
protected override bool SubmitDisabled = > ! this . localizationPossible ;
2025-04-24 14:49:32 +00:00
protected override void ResetForm ( )
{
if ( ! this . MightPreselectValues ( ) )
{
2025-04-25 13:29:17 +00:00
this . selectedLanguagePluginId = InternalPlugin . LANGUAGE_EN_US . MetaData ( ) . Id ;
2025-04-24 14:49:32 +00:00
this . selectedTargetLanguage = CommonLanguages . AS_IS ;
this . customTargetLanguage = string . Empty ;
}
2025-04-25 15:43:09 +00:00
_ = this . OnChangedLanguage ( ) ;
2025-04-24 14:49:32 +00:00
}
protected override bool MightPreselectValues ( )
{
if ( this . SettingsManager . ConfigurationData . I18N . PreselectOptions )
{
2025-04-25 13:29:17 +00:00
this . selectedLanguagePluginId = this . SettingsManager . ConfigurationData . I18N . PreselectedLanguagePluginId ;
2025-04-24 14:49:32 +00:00
this . selectedTargetLanguage = this . SettingsManager . ConfigurationData . I18N . PreselectedTargetLanguage ;
this . customTargetLanguage = this . SettingsManager . ConfigurationData . I18N . PreselectOtherLanguage ;
return true ;
}
return false ;
}
private CommonLanguages selectedTargetLanguage ;
private string customTargetLanguage = string . Empty ;
2025-04-25 13:29:17 +00:00
private bool isLoading = true ;
private string loadingIssue = string . Empty ;
private bool localizationPossible ;
private string searchString = string . Empty ;
private Guid selectedLanguagePluginId ;
2025-04-25 15:43:09 +00:00
private ILanguagePlugin ? selectedLanguagePlugin ;
2025-04-25 13:29:17 +00:00
private Dictionary < string , string > addedKeys = [ ] ;
private Dictionary < string , string > removedKeys = [ ] ;
#region Overrides of AssistantBase < SettingsDialogI18N >
protected override async Task OnInitializedAsync ( )
{
await base . OnInitializedAsync ( ) ;
2025-04-25 15:43:09 +00:00
await this . OnLanguagePluginChanged ( this . selectedLanguagePluginId ) ;
2025-04-25 13:29:17 +00:00
await this . LoadData ( ) ;
}
#endregion
2025-04-25 15:43:09 +00:00
2025-04-25 13:29:17 +00:00
private async Task OnLanguagePluginChanged ( Guid pluginId )
{
this . selectedLanguagePluginId = pluginId ;
2025-04-25 15:43:09 +00:00
await this . OnChangedLanguage ( ) ;
}
private async Task OnChangedLanguage ( )
{
if ( PluginFactory . RunningPlugins . FirstOrDefault ( n = > n is PluginLanguage & & n . Id = = this . selectedLanguagePluginId ) is not PluginLanguage comparisonPlugin )
this . loadingIssue = $"Was not able to load the language plugin for comparison ({this.selectedLanguagePluginId}). Please select a valid, loaded & running language plugin." ;
else if ( comparisonPlugin . IETFTag ! = this . selectedTargetLanguage . ToIETFTag ( ) )
this . loadingIssue = $"The selected language plugin for comparison uses the IETF tag '{comparisonPlugin.IETFTag}' which does not match the selected target language '{this.selectedTargetLanguage.ToIETFTag()}'. Please select a valid, loaded & running language plugin which matches the target language." ;
else
{
this . selectedLanguagePlugin = comparisonPlugin ;
this . loadingIssue = string . Empty ;
await this . LoadData ( ) ;
}
2025-04-25 13:29:17 +00:00
this . StateHasChanged ( ) ;
}
private async Task LoadData ( )
{
2025-04-25 15:43:09 +00:00
if ( this . selectedLanguagePlugin is null )
{
this . loadingIssue = "Please select a language plugin for comparison." ;
this . localizationPossible = false ;
this . isLoading = false ;
this . StateHasChanged ( ) ;
return ;
}
2025-04-25 13:29:17 +00:00
this . isLoading = true ;
this . StateHasChanged ( ) ;
//
// Read the file `Assistants\I18N\allTexts.lua`:
//
#if DEBUG
var filePath = Path . Join ( Environment . CurrentDirectory , "Assistants" , "I18N" ) ;
var resourceFileProvider = new PhysicalFileProvider ( filePath ) ;
#else
var resourceFileProvider = new ManifestEmbeddedFileProvider ( Assembly . GetAssembly ( type : typeof ( Program ) ) ! , "Assistants.I18N" ) ;
#endif
var file = resourceFileProvider . GetFileInfo ( "allTexts.lua" ) ;
await using var fileStream = file . CreateReadStream ( ) ;
using var reader = new StreamReader ( fileStream ) ;
var newI18NDataLuaCode = await reader . ReadToEndAsync ( ) ;
//
// Next, we try to load the text as a language plugin -- without
// actually starting the plugin:
//
var newI18NPlugin = await PluginFactory . Load ( null , newI18NDataLuaCode ) ;
switch ( newI18NPlugin )
{
case NoPlugin noPlugin when noPlugin . Issues . Any ( ) :
this . loadingIssue = noPlugin . Issues . First ( ) ;
break ;
case NoPlugin :
this . loadingIssue = "Was not able to load the I18N plugin. Please check the plugin code." ;
break ;
case { IsValid : false } plugin when plugin . Issues . Any ( ) :
this . loadingIssue = plugin . Issues . First ( ) ;
break ;
case PluginLanguage pluginLanguage :
this . loadingIssue = string . Empty ;
var newI18NContent = pluginLanguage . Content ;
2025-04-25 15:43:09 +00:00
var currentI18NContent = this . selectedLanguagePlugin . Content ;
2025-04-25 13:29:17 +00:00
this . addedKeys = newI18NContent . ExceptBy ( currentI18NContent . Keys , n = > n . Key ) . ToDictionary ( ) ;
this . removedKeys = currentI18NContent . ExceptBy ( newI18NContent . Keys , n = > n . Key ) . ToDictionary ( ) ;
this . localizationPossible = true ;
break ;
}
this . isLoading = false ;
this . StateHasChanged ( ) ;
}
private bool FilterFunc ( KeyValuePair < string , string > element )
{
if ( string . IsNullOrWhiteSpace ( this . searchString ) )
return true ;
if ( element . Key . Contains ( this . searchString , StringComparison . OrdinalIgnoreCase ) )
return true ;
if ( element . Value . Contains ( this . searchString , StringComparison . OrdinalIgnoreCase ) )
return true ;
return false ;
}
2025-04-24 14:49:32 +00:00
private string? ValidatingTargetLanguage ( CommonLanguages language )
{
if ( language = = CommonLanguages . AS_IS )
return "Please select a target language." ;
return null ;
}
private string? ValidateCustomLanguage ( string language )
{
if ( this . selectedTargetLanguage = = CommonLanguages . OTHER & & string . IsNullOrWhiteSpace ( language ) )
return "Please provide a custom language." ;
return null ;
}
private async Task LocalizeText ( )
{
}
}