catalyst
catalyst copied to clipboard
Dynamic Language.Register()
Is your feature request related to a problem? Please describe. First of all, amazing work. I think I'm not using the API properly. Here is what I'm doing:
- Detect language with the language detector --> OK I et a Language object
- I need to detect sentences optimized for that language --> KO because I need to enter for ex English.Register() before creating the pipeline, but its not ideal I would like to do Language.Register() dynamically because I might have several languages. Currently I have to either register all languages at the beginning of the app, OR create a custom mapping somehow but it's really messy.
Describe the solution you'd like do Language.Register() dynamically
Describe alternatives you've considered Currently I have to either register all languages at the beginning of the app, OR create a custom mapping somehow but it's really messy.
Additional context I'm probably don't use the proper API here, so please let me know if there is a seamless solution :)
Best.
Here is my fix, it works.
private void TryRegisterConcreteLanguage(Language language)
{
lock (_registerLock)
{
var langName = language.ToString(); // "English", "Arabic", etc.
var typeName = $"Catalyst.Models.{langName}";
try
{
// Step 1: Load the assembly manually if it's not already loaded
var assembly = AppDomain.CurrentDomain
.GetAssemblies()
.FirstOrDefault(a => a.GetName().Name == typeName)
?? Assembly.Load(typeName); // Load from name (e.g., "Catalyst.Models.English")
if (assembly == null)
{
_logger.LogWarning("Failed to load assembly: {Assembly}", typeName);
return;
}
// Step 2: Try to find the type
var langType = assembly.ExportedTypes.FirstOrDefault(t => t?.FullName != null && t.FullName.Equals(typeName));
if (langType == null)
{
_logger.LogWarning("Type {TypeName} not found in assembly {Assembly}", typeName, typeName);
return;
}
// Step 3: Call Register()
var registerMethod = langType.GetMethod("Register", BindingFlags.Public | BindingFlags.Static);
if (registerMethod != null)
{
registerMethod.Invoke(null, null);
_logger.LogInformation("Catalyst language registered: {Language}", langName);
}
else
{
_logger.LogWarning("Register() method not found for type {TypeName}", typeName);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception while registering Catalyst language: {Language}", langName);
}
}
}