Code Monkey home page Code Monkey logo

Comments (16)

mccalltd avatar mccalltd commented on July 17, 2024

Add it via one of the following:

[RouteArea("Name", AreaUrl = "{culture}/area")]
[RoutePrefix("{culture}/prefix")]
[GET/PUT/POST/DELETE("{culture}/url")]

There's nothing in place to do this for you in an automatic way. So you might have to repeat yourself here and there. Hope that helps.

On Feb 14, 2012, at 2:34 PM, shapper wrote:

Hello,

I am using the new Localization version.

In some cases I might need to add the culture code to the route:

/{culture}/area/prefix/controller/action/id

What is the best way to do this?

Thank You,
Miguel


Reply to this email directly or view it on GitHub:
#48

from attributerouting.

mdmoura avatar mdmoura commented on July 17, 2024

Hello,

I tried your suggestion but:
1 - When I add it to RoutePrefix or Action the culture does not show;
2 - When I add it to RouteArea I get a NotFound error.

For example, I had this:

[RouteArea("CMS"), RoutePrefix("Access")]
public partial class UserController : Controller {

  [GET("Users")]
  public virtual ActionResult Index(Int32 p = 1) { }

}

And the generated url, translated to portuguese, is:
cms/acesso/utilizadores?p=1 (In english it would be cms/access/users?p=1)

When I apply the culture to route area as follows:

[RouteArea("CMS", AreaUrl = "{culture}/area"), RoutePrefix("Access")]

I get a 404 error and the url becomes:

/CMS/user?p=1

Note that CMS is not even lowercase and user is not translated.

Am I missing something?

Thank you,
Miguel

from attributerouting.

mccalltd avatar mccalltd commented on July 17, 2024

How are you generating the link to the action?

On Feb 14, 2012, at 3:28 PM, shapper wrote:

Hello,

I tried your suggestion but:
1 - When I add it to RoutePrefix or Action the culture does not show;
2 - When I add it to RouteArea I get a NotFound error.

For example, I had this:

[RouteArea("CMS"), RoutePrefix("Access")]
public partial class UserController : Controller {

 [GET("Users")]
 public virtual ActionResult Index(Int32 p = 1) { }

}

And the generated url, translated to portuguese, is:
cms/acesso/utilizadores?p=1 (In english it would be cms/access/users?p=1)

When I apply the culture to route area as follows:

[RouteArea("CMS", AreaUrl = "{culture}/area"), RoutePrefix("Access")]

I get a 404 error and the url becomes:

/CMS/user?p=1

Note that CMS is not even lowercase and user is not translated.

Am I missing something?

Thank you,
Miguel


Reply to this email directly or view it on GitHub:
#48 (comment)

from attributerouting.

mdmoura avatar mdmoura commented on July 17, 2024

Using T4MVC:

@Html.ActionLink("CMS", MVC.CMS.User.Index())

Which has been working without any problems with AR.

from attributerouting.

mdmoura avatar mdmoura commented on July 17, 2024

By the way, T4MVC is here in Nuget:
http://nuget.org/packages/T4MVC

from attributerouting.

mccalltd avatar mccalltd commented on July 17, 2024

Don't you have to pass the culture in as a route param here so the url is built correctly? Not that I suggest this as a best solution.

On Feb 14, 2012, at 3:33 PM, shapper wrote:

Using T4MVC:

@Html.ActionLink("CMS", MVC.CMS.User.Index())

Which has been working without any problems with AR.


Reply to this email directly or view it on GitHub:
#48 (comment)

from attributerouting.

mdmoura avatar mdmoura commented on July 17, 2024

That is something I would like to avoid ... My idea would be:

When the routes are generated every time {culture} appears it would be replace by current culture.
This behavior could be controlled with a True/False property in the AR configuration.

To be honest I am not sure if that makes sense.

I was also reading the following:
http://adamyan.blogspot.com/

But I am not sure how would this work with AR.

from attributerouting.

mccalltd avatar mccalltd commented on July 17, 2024

Just read that blog post. You could do what he does independently of attribute routing. It should work fine. All AR is doing is adding routes to a route table. I'll start thinking about how AR might support detecting the culture from the URL. The one issue I see with having AR handle it is that some people might use http://domain.com/{lang}-[culture}/, or prefer http://{lang}-{culture}.domain.com/. So if AR is going to support detecting culture form the URL, it has to be flexible. Please keep sending along ideas!

from attributerouting.

mdmoura avatar mdmoura commented on July 17, 2024

I'll start thinking about how AR might support detecting the culture from the URL.
The one issue I see with having AR handle it is that some people might use http://domain.com/{lang}-[culture}/, or prefer http://{lang}-{culture}.domain.com/.

I am not sure if that is the best idea.

There are a few ways to do this so AR would need to be really flexible.

But maybe adding to Wiki example code to cover the options might be useful.

I am working in a few ways to do this:

1 - Using different domains for each culture.

DOMAIN.COM > English Version
DOMAIN.PT > Portuguese Version

If only one domain is available then subdomains could be used.

So in Application_BeginRequest the culture is set based on:

 HttpContext.Current.Request.Url.Host  

It is only necessary to check its value.

To change culture just add a link in a view to change domain / subdomain.

This approach is really good for Search Engine Indexing.

2 - Using a CULTURE prefix in route.

On Application_BeginRequest the culture is set based on the URL.

When the user accesses the home page there is no culture code. So:

i) Use a Cookie to remember the culture used by the user last time.

ii) Check culture user preferences ...

To change culture just do one of the following:

i) Redirect to same page but with culture code changed.
Of course this only works when all pages have equivalent.
If the page does in other culture does not exist then redirect to home.

ii) Just redirect to Home with culture prefix changed.
Change cookie and so on ...

It is option (2) which I am not completely sure of the best way to integrate with AR.

from attributerouting.

mccalltd avatar mccalltd commented on July 17, 2024

So here's something I just worked up having remembered that an AR user submitted a patch allowing the configuration of a custom route handler. The config code would look like this (this is from the AR.Web project):

routes.MapAttributeRoutes(config =>
{
    config.ScanAssemblyOf<ControllerBase>();
    ...
    config.UseRouteHandler(() => new CultureAwareRouteHandler());
});

The route handler looks like this:

public class CultureAwareRouteHandler : MvcRouteHandler
{
    private const string CultureRouteParamName = "culture";

    protected override System.Web.IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        // Detect the current culture from route data first 
        // (will be in here if url {culture} is part of the route).
        var currentCultureName = (string)requestContext.RouteData.Values[CultureRouteParamName];
        if (currentCultureName == null)
        {
            // Fallback to detecting the user language.
            var request = requestContext.HttpContext.Request;
            if (request.UserLanguages != null && request.UserLanguages.Any())
                currentCultureName = request.UserLanguages[0];
        }

        if (currentCultureName != null)
        {
            // Set the current thread's culture based on the detected culture.
            var cultureInfo = new CultureInfo(currentCultureName);
            Thread.CurrentThread.CurrentUICulture = cultureInfo;

            // Add the culture to route data
            requestContext.RouteData.Values[CultureRouteParamName] = currentCultureName;
        }

        return base.GetHttpHandler(requestContext);
    }
}

I tested this in the AR.Web project and it works great. Check out the code. Again, I'm still not sure that AR should do anything but rely on the current thread. It might be tricky to put a one-size fits all solution in place when a custom route handler plugged into the routes would do the trick.

If you implemented your own RouteHandler like above, you have a lot of flexibility to support cookies or anything else. Let me know your thoughts.

from attributerouting.

mdmoura avatar mdmoura commented on July 17, 2024

I agree that a plug and play handler would be ideal.

However, it would be nice to be able to integrate the logic with AR

I tested the handler approach and really liked it. For now I found 2 problems:

1 - Home/Index action. I get a 404 error.

I tried with RoutePrefix("{culture}") or GET("{culture}") on the action and no luck.

On the area routes I simply added: RouteArea("CMS", AreaUrl = "{culture}/CMS")

Here it works fine ...

2 - Translation with Culture Handler.

Consider you have the following route:

http://localhost:6575/en/area/access/roles?p=1

If you go to the URL and replace the "en" by "pt" you will get:

http://localhost:6575/pt/area/access/roles?p=1

The page loads fine and the links get the PT parameter ...
However the url is not translated and the same happens with the links.

There should be a way update/translate the url on the handler.
When I manually change "en" to "pt" in the url the handler should change the culture and translate the url.

Do you understand what I mean?

I will keep testing it and post my feedback here.

from attributerouting.

mdmoura avatar mdmoura commented on July 17, 2024

By the way, I also tried to use the following to solve problem (1):

RouteTable.Routes.MapRoute("Default", "{culture}/{controller}/{action}/{id}", new { controller = "Home", action = "Index", culture = UrlParameter.Optional, id = UrlParameter.Optional }, typeof(HomeController).Assembly);

But I keep getting the 404 error.

from attributerouting.

mdmoura avatar mdmoura commented on July 17, 2024

Hi,

I was thinking on problem (2) and:

Consider you have the following route:

http://localhost:6575/en/area/access/roles?p=1

If you go to the URL and replace the "en" by "pt" you will get:

http://localhost:6575/pt/area/access/roles?p=1

And the view is loaded fine ...

Instead of this behavior or, as I mentioned before, translating the route, I think the ideal would be a 404 error.

Because in fact "pt/area/access/roles?p=1" does not exist.

Any idea how to solve this?

Thank You,
Miguel

from attributerouting.

mccalltd avatar mccalltd commented on July 17, 2024

I chose not to handle this case by default because I was not sure if that would be ideal behavior for everyone. So I think there should a config option to turn on culture-specific route constraints for translated routes. So you would just say: config.ConstrainTranslatedRoutesByCulture = true.

t

On Feb 16, 2012, at 12:51 PM, shapper wrote:

Hi,

I was thinking on problem (2) and:

Consider you have the following route:

http://localhost:6575/en/area/access/roles?p=1

If you go to the URL and replace the "en" by "pt" you will get:

http://localhost:6575/pt/area/access/roles?p=1

And the view is loaded fine ...

Instead of this behavior or, as I mentioned before, translating the route, I think the ideal would be a 404 error.

Because in fact "pt/area/access/roles?p=1" does not exist.

Any idea how to solve this?

Thank You,
Miguel


Reply to this email directly or view it on GitHub:
#48 (comment)

from attributerouting.

mdmoura avatar mdmoura commented on July 17, 2024

Yes, I think that might be a good option ...

First I considered the full translation but then I started to think ...
And I looked at Google site and the site of the university where I studied.

And it seems logic to fire a 404 error. And maybe give less problems.

from attributerouting.

mccalltd avatar mccalltd commented on July 17, 2024

Hey finally! I pushed to nuget (v1.6). Updated the wiki here: https://github.com/mccalltd/AttributeRouting/wiki/Localization

from attributerouting.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.