384 lines
No EOL
12 KiB
Text
384 lines
No EOL
12 KiB
Text
#!/usr/bin/env nu
|
|
# AI-Enhanced i18n Commands for AuroraFrame
|
|
#
|
|
# Integrates the Fluent plugin with AI-powered translation tools
|
|
# via the MCP server to provide intelligent internationalization.
|
|
|
|
# Load the main i18n framework
|
|
use i18n.nu *
|
|
|
|
# AI-powered translation generation
|
|
export def "aurora ai i18n translate" [
|
|
source_locale: string
|
|
target_locale: string
|
|
--page: string # Specific page to translate
|
|
--namespace: string # Specific namespace to translate
|
|
--context: string # Additional context for AI
|
|
--output: string # Output file path
|
|
--validate: bool = true # Validate generated translations
|
|
] {
|
|
print $"🤖 AI Translation: ($source_locale) → ($target_locale)"
|
|
|
|
# Load site configuration
|
|
let site_config = (open "config/site.k" | from json)
|
|
let page_config = if ($page | is-not-empty) {
|
|
open $"pages/($page)/config.k" | from json
|
|
} else {
|
|
{}
|
|
}
|
|
|
|
# Extract source messages
|
|
let source_bundle = (load_merged_locales $source_locale $page $site_config $page_config)
|
|
|
|
# Prepare context information
|
|
let translation_context = {
|
|
page: ($page | default "global")
|
|
namespace: ($namespace | default "all")
|
|
brand_name: ($site_config.name | default "AuroraFrame")
|
|
additional_context: ($context | default "")
|
|
}
|
|
|
|
# Convert bundle to FTL format for translation
|
|
let source_ftl = (bundle_to_ftl $source_bundle $namespace)
|
|
|
|
# Call MCP server for AI translation
|
|
let mcp_request = {
|
|
method: "tools/call"
|
|
params: {
|
|
name: "generate_translations"
|
|
arguments: {
|
|
source_locale: $source_locale
|
|
target_locale: $target_locale
|
|
source_messages: $source_ftl
|
|
context: $translation_context
|
|
}
|
|
}
|
|
}
|
|
|
|
let ai_result = (call_mcp_server $mcp_request true)
|
|
|
|
if "error" in $ai_result {
|
|
error make { msg: $"AI translation failed: ($ai_result.error)" }
|
|
}
|
|
|
|
# Extract FTL content from AI response
|
|
let generated_ftl = (extract_ftl_from_response $ai_result.content.0.text)
|
|
|
|
# Validate the generated FTL if requested
|
|
if $validate {
|
|
let validation = (fluent validate $generated_ftl)
|
|
if not $validation.valid {
|
|
print "⚠️ Generated FTL has validation issues:"
|
|
for error in $validation.errors {
|
|
print $" - ($error)"
|
|
}
|
|
}
|
|
}
|
|
|
|
# Save or display the result
|
|
if ($output | is-not-empty) {
|
|
$generated_ftl | save $output
|
|
print $"✅ Translations saved to: ($output)"
|
|
} else {
|
|
# Determine output path based on page and namespace
|
|
let output_path = if ($page | is-not-empty) {
|
|
$"locales/pages/($page)/($target_locale)/($namespace | default 'generated').ftl"
|
|
} else {
|
|
$"locales/global/($target_locale)/($namespace | default 'generated').ftl"
|
|
}
|
|
|
|
mkdir ($output_path | path dirname)
|
|
$generated_ftl | save $output_path
|
|
print $"✅ Translations saved to: ($output_path)"
|
|
}
|
|
}
|
|
|
|
# AI-powered translation validation
|
|
export def "aurora ai i18n validate" [
|
|
--locales: list<string> # Locales to validate
|
|
--page: string # Specific page to validate
|
|
--fix: bool = false # Auto-fix issues where possible
|
|
] {
|
|
print "🧠 AI Translation Validation"
|
|
|
|
# Load configuration
|
|
let site_config = (open "config/site.k" | from json)
|
|
let target_locales = $locales | default ($site_config.i18n.available_locales | each { |l| $l.code })
|
|
|
|
# Load all locale bundles
|
|
mut message_bundles = {}
|
|
for locale in $target_locales {
|
|
let bundle = (load_merged_locales $locale $page $site_config)
|
|
$message_bundles = ($message_bundles | insert $locale $bundle)
|
|
}
|
|
|
|
# Call AI validation via MCP
|
|
let mcp_request = {
|
|
method: "tools/call"
|
|
params: {
|
|
name: "validate_translations"
|
|
arguments: {
|
|
locales: $target_locales
|
|
reference_locale: $site_config.i18n.default_locale
|
|
message_bundles: $message_bundles
|
|
}
|
|
}
|
|
}
|
|
|
|
let validation_result = (call_mcp_server $mcp_request true)
|
|
|
|
if "error" in $validation_result {
|
|
error make { msg: $"AI validation failed: ($validation_result.error)" }
|
|
}
|
|
|
|
print $validation_result.content.0.text
|
|
|
|
if $fix {
|
|
print "🔧 Auto-fix functionality coming soon..."
|
|
# TODO: Implement auto-fix based on AI suggestions
|
|
}
|
|
}
|
|
|
|
# Extract translatable strings from templates
|
|
export def "aurora ai i18n extract" [
|
|
template_files: list<string> # Template files to analyze
|
|
--page: string # Page context
|
|
--output: string # Output file for extracted strings
|
|
--format: string = "ftl" # Output format (ftl, json)
|
|
] {
|
|
print $"🔍 AI String Extraction from ($template_files | length) templates"
|
|
|
|
# Load existing messages to avoid duplicates
|
|
let site_config = (open "config/site.k" | from json)
|
|
let existing_bundle = (load_merged_locales $site_config.i18n.default_locale $page $site_config)
|
|
let existing_messages = (extract_message_ids $existing_bundle)
|
|
|
|
# Prepare page context
|
|
let page_context = if ($page | is-not-empty) {
|
|
let page_config = (open $"pages/($page)/config.k" | from json)
|
|
{
|
|
page_name: $page
|
|
page_type: ($page_config.layout? | default "default")
|
|
features: ($page_config | columns)
|
|
}
|
|
} else {
|
|
{}
|
|
}
|
|
|
|
# Call AI extraction via MCP
|
|
let mcp_request = {
|
|
method: "tools/call"
|
|
params: {
|
|
name: "extract_strings"
|
|
arguments: {
|
|
template_files: $template_files
|
|
page_context: $page_context
|
|
existing_messages: $existing_messages
|
|
}
|
|
}
|
|
}
|
|
|
|
let extraction_result = (call_mcp_server $mcp_request true)
|
|
|
|
if "error" in $extraction_result {
|
|
error make { msg: $"AI extraction failed: ($extraction_result.error)" }
|
|
}
|
|
|
|
# Process the extraction results
|
|
let extracted_content = $extraction_result.content.0.text
|
|
|
|
if ($output | is-not-empty) {
|
|
$extracted_content | save $output
|
|
print $"✅ Extracted strings saved to: ($output)"
|
|
} else {
|
|
print $extracted_content
|
|
}
|
|
}
|
|
|
|
# Get AI-powered i18n improvement suggestions
|
|
export def "aurora ai i18n optimize" [
|
|
--analyze-usage: bool = false # Include usage pattern analysis
|
|
--performance: bool = false # Include performance metrics
|
|
] {
|
|
print "🚀 AI i18n Optimization Analysis"
|
|
|
|
# Analyze current locale structure
|
|
let site_config = (open "config/site.k" | from json)
|
|
let locale_structure = (analyze_locale_structure $site_config)
|
|
|
|
# Gather usage patterns if requested
|
|
let usage_patterns = if $analyze_usage {
|
|
analyze_usage_patterns $site_config
|
|
} else {
|
|
{}
|
|
}
|
|
|
|
# Gather performance metrics if requested
|
|
let performance_metrics = if $performance {
|
|
analyze_performance_metrics $site_config
|
|
} else {
|
|
{}
|
|
}
|
|
|
|
# Call AI optimization via MCP
|
|
let mcp_request = {
|
|
method: "tools/call"
|
|
params: {
|
|
name: "suggest_improvements"
|
|
arguments: {
|
|
locale_structure: $locale_structure
|
|
usage_patterns: $usage_patterns
|
|
performance_metrics: $performance_metrics
|
|
}
|
|
}
|
|
}
|
|
|
|
let optimization_result = (call_mcp_server $mcp_request true)
|
|
|
|
if "error" in $optimization_result {
|
|
error make { msg: $"AI optimization failed: ($optimization_result.error)" }
|
|
}
|
|
|
|
print $optimization_result.content.0.text
|
|
}
|
|
|
|
# Initialize AI-enhanced i18n for a project
|
|
export def "aurora ai i18n init" [
|
|
--locales: list<string> # Initial locales to set up
|
|
--interactive: bool = true # Interactive setup mode
|
|
] {
|
|
print "🌍 Initializing AI-Enhanced i18n"
|
|
|
|
let target_locales = $locales | default ["en-US", "es-ES", "fr-FR"]
|
|
|
|
if $interactive {
|
|
print "Setting up i18n structure..."
|
|
|
|
# Create directory structure
|
|
for locale in $target_locales {
|
|
mkdir $"locales/global/($locale)"
|
|
}
|
|
|
|
# Generate initial common.ftl files with AI assistance
|
|
for locale in $target_locales {
|
|
if $locale != "en-US" {
|
|
print $" Generating initial ($locale) translations..."
|
|
aurora ai i18n translate "en-US" $locale --namespace "common" --context "Initial setup for AuroraFrame project"
|
|
}
|
|
}
|
|
|
|
print "✅ AI-enhanced i18n initialized successfully!"
|
|
print $"📁 Created structure for locales: ($target_locales | str join ', ')"
|
|
print "🚀 Use 'aurora ai i18n translate' to generate more translations"
|
|
}
|
|
}
|
|
|
|
# Helper functions
|
|
|
|
def bundle_to_ftl [bundle: record, namespace?: string] {
|
|
# Convert a message bundle back to FTL format
|
|
mut ftl_content = ""
|
|
|
|
let target_namespaces = if ($namespace | is-not-empty) {
|
|
[$namespace]
|
|
} else {
|
|
$bundle | columns
|
|
}
|
|
|
|
for ns in $target_namespaces {
|
|
if $ns in ($bundle | columns) {
|
|
let namespace_data = ($bundle | get $ns)
|
|
if "messages" in ($namespace_data | columns) {
|
|
let messages = ($namespace_data.messages | columns)
|
|
for msg_id in $messages {
|
|
let msg_data = ($namespace_data.messages | get $msg_id)
|
|
$ftl_content = $ftl_content + $"($msg_id) = ($msg_data.text)\n"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$ftl_content
|
|
}
|
|
|
|
def extract_ftl_from_response [response_text: string] {
|
|
# Extract FTL code block from AI response
|
|
let lines = ($response_text | lines)
|
|
mut in_ftl_block = false
|
|
mut ftl_lines = []
|
|
|
|
for line in $lines {
|
|
if ($line | str trim) == "```ftl" {
|
|
$in_ftl_block = true
|
|
} else if ($line | str trim) == "```" and $in_ftl_block {
|
|
break
|
|
} else if $in_ftl_block {
|
|
$ftl_lines = ($ftl_lines | append $line)
|
|
}
|
|
}
|
|
|
|
$ftl_lines | str join "\n"
|
|
}
|
|
|
|
def extract_message_ids [bundle: record] {
|
|
# Extract all message IDs from a bundle
|
|
mut message_ids = []
|
|
|
|
for namespace in ($bundle | columns) {
|
|
let ns_data = ($bundle | get $namespace)
|
|
if "messages" in ($ns_data | columns) {
|
|
let ids = ($ns_data.messages | columns)
|
|
$message_ids = ($message_ids | append $ids)
|
|
}
|
|
}
|
|
|
|
$message_ids | uniq
|
|
}
|
|
|
|
def analyze_locale_structure [site_config: record] {
|
|
# Analyze the current locale directory structure
|
|
let global_dir = $site_config.i18n.global_locales_dir
|
|
let pages_dir = $site_config.i18n.page_locales_dir
|
|
|
|
{
|
|
global_locales: (try { ls $global_dir } catch { [] })
|
|
page_locales: (try { ls $pages_dir } catch { [] })
|
|
config: $site_config.i18n
|
|
}
|
|
}
|
|
|
|
def analyze_usage_patterns [site_config: record] {
|
|
# Placeholder for usage pattern analysis
|
|
# In practice, this would analyze actual usage logs
|
|
{
|
|
most_requested_locales: ["en-US", "es-ES"]
|
|
page_views_by_locale: {}
|
|
missing_translation_requests: []
|
|
}
|
|
}
|
|
|
|
def analyze_performance_metrics [site_config: record] {
|
|
# Placeholder for performance metrics
|
|
# In practice, this would measure actual load times and bundle sizes
|
|
{
|
|
bundle_sizes: {}
|
|
load_times: {}
|
|
cache_hit_rates: {}
|
|
}
|
|
}
|
|
|
|
# Mock MCP server call (replace with actual implementation)
|
|
def call_mcp_server [request: record, debug: bool] {
|
|
# This is a simplified mock - in practice, this would call the actual MCP server
|
|
print "📡 Calling MCP server for AI assistance..."
|
|
|
|
# Simulate successful response
|
|
{
|
|
content: [
|
|
{
|
|
type: "text"
|
|
text: "AI-powered i18n functionality is ready! The actual implementation will connect to the AuroraFrame MCP server for intelligent translation assistance."
|
|
}
|
|
]
|
|
}
|
|
} |