Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Controller and Action name according to language selected

I have a Controller with the name "Hem" and Action name is "Om". And default language i have set Swedish. So route will be on Swedish site, it's

/sv/Hem/Om

Now I want to change language to "en" by clicking English in language section. So route will be set automatically like this way :

/en/Home/About

But functionality should be work of /sv/Hem/Om and In address bar should be display as /en/Home/About

Experts can you please help me out.

like image 359
Rahul Modi Avatar asked Dec 25 '22 10:12

Rahul Modi


1 Answers

You can do this way.

routes.MapRoute(
    "English route",
    "en/{controller}/{action}/{id}"
    new { controller = "Home", action = "Index", language = "en" },
);

routes.MapRoute(
    "FrenchHome",
    "/sv/Hem/Om",
    new { controller = "Home", action = "Index", language = "fr" }
);

or you can do that way:

public class GenericRoutes
{
    public string Controller {get;set;}
    public string Action {get;set;}
    public string Url{get;set;}
    public string RouteName{get;set;}
}

public List<GenericRoutes> Routes = new List<GenericRoutes>();

Routes.Add(new GenericRoutes{Cotroller="bl",Action="cl",Url="bl/cl"})

for(int i=0;i<Routes.count();i++) 
{
    routes.MapRoute(
        Routes[i].RouteName,
        Routes[i].Url,
        new { controller = Routes[i].Controller, action = Routes[i].Action },
    );
}
like image 115
Anik Saha Avatar answered Dec 28 '22 07:12

Anik Saha