orchardcmscurrentculture

How to get the current culture in a controller (in a custom module of Orchard CMS)


I need to get the current culture in a controller in a custom module; I know how to get it from a razor view:

@{
    var currentCulture = WorkContext.CurrentCulture;
}

but in the controller I have no access to the WorkContext and I can't find anything similar (I tried HttpContext but there is no CurrentCulture method).


Solution

  • You can inject the WorkContextAccessor into your controller and get the current WorkContext

    private readonly IWorkContextAccessor _wca;
    
    public MyController(IWorkContextAccessor wca) {
        _wca = wca;
    }
    
    public ActionResult Index() {
        var workContext = _wca.GetContext();
    }
    

    The WorkContext is also available on the IOrchardServices interface