Search code examples
asp.net-mvclocalizationeasyquery

Localization of Korzh EasyQuery in asp.net mvc


I'm trying to localize the Korzh component EasyQuery. I'm actually halfway as I managed to localize the ui messages. But the entity names, the attributes and the operators desperately remains in English.

Here is what I've done (referring step described on korzh.com.

$(document).ready(function() {
    var culture = $('#hidCulture').val();

    if (culture.contains('nl')) {
        EQ.core.texts = getDutchText();
    } else if (culture.contains('fr')) {
        EQ.core.texts = getFrenchText();
    } else {
        EQ.core.texts = getEnglishText();
    }
});

The text resource comes from another file and looks like this:

getFrenchText = function() {
    return {
        Local: "fr",
        AltMenuAttribute: "Attribute",
        AltMenuAttribute: "Attribut",
        ...            
        StrAddColumns: "Ajouter des colonnes",

        Entities: {
            "Company": "Entreprise",
            "Office": "Site",
            ...
        },
        Attribute: {
            "Company.Name": "Nom",
            ...
        },
        Operators: {
            "Equal": {
                "caption": "Egal à",
                "displayFormat": "{expr1} [[est égal à]] {expr2}"
        }
    };
}

There are other solutions using resource or property files but it doesn't fit my needs. As I understood it doesn't localize entity names.

Here ends the a last-ditch attempt. Has anyone met the same issue?


Solution

  • I couldn't solve the issue using javascript.

    I thought first to hard code the entities and attributes captions in one language using the Data Model Editor delivered with EasyQuery. This solution allows me only one language or one language by xml file. To my opinion, generate a different xml for each language is a seriously bad idea.

    In the query builder controller, there is a method that loads the model. I simply added a method to recursively look for captions in a resources set.

    [HttpPost]
    public ActionResult GetModel(string modelName)
    {
        var dbModel = _eqService.GetModel(modelName);
        var resources = EasyQueryModule.ResourceManager.GetResourceSet(
            CultureInfo.CurrentUICulture, false, true
        );
    
        Translate(dbModel.EntityRoot.SubEntities, resources);
    
        return Json(dbModel.SaveToDictionary());
    }
    
    private static void Translate(IEnumerable<Entity> entities, ResourceSet resources)
    {
        entities.ForEach(entity =>
        {
            entity.Name = resources.GetString(entity.Name) ?? entity.Name;
    
            entity.Attributes.ForEach(attribute =>
                attribute.Caption = resources.GetString(attribute.Caption) ?? attribute.Caption
            );
    
            Translate(entity.SubEntities, resources);
        });
    }
    

    It might be not the most efficient way to localize the widget. I haven't found anything better. Note that I prevented error in case of missing resource item

    For my concerns, localized operators would have be nice but not mandatory. They remain in English until further investigation.