turquoiseowl / i18n

Smart internationalization for ASP.NET
Other
556 stars 156 forks source link

How to properly exclude URLs from being localized? #295

Closed hognevevle closed 7 years ago

hognevevle commented 7 years ago

Hi!

I've been trying for a while to successfully exclude certain URLs from being localized (and thus saving the redirection round trip), but I'm not having too much luck.

In my Application_Start(), I have added the following:

i18n.UrlLocalizer.QuickUrlExclusionFilter = new Regex(@"(?:\.css|\.json|\.less|\.jpg|\.jpeg|\.png|\.gif|\.ico|\.svg|\.woff|\.woff2|\.ttf|\.eot)$", RegexOptions.IgnoreCase);

However, this does not seem to exclude those files. I tested to fetch a .css file via curl to see what is happening:

$ curl -IL http://myapplication.local/Content/bootstrap/css/bootstrap.min.css HTTP/1.1 302 Moved Temporarily Content-Length: 0 Location: /en-US/Content/bootstrap/css/bootstrap.min.css Server: Microsoft-IIS/10.0 X-Powered-By: ASP.NET Date: Fri, 16 Sep 2016 10:40:17 GMT

HTTP/1.1 200 OK Content-Length: 95563 Content-Type: text/css Last-Modified: Mon, 11 Apr 2016 08:45:46 GMT Accept-Ranges: bytes ETag: "7d3a448ace93d11:0" Server: Microsoft-IIS/10.0 X-Powered-By: ASP.NET Date: Fri, 16 Sep 2016 10:40:17 GMT

As you can see, the redirection to /en-US/ is still happening.

I have also tried the following, which does not change the outcome:

i18n.LocalizedApplication.Current.UrlsToExcludeFromProcessing = new Regex(@"(\.css)$");

I've done some additional tests using i18n.UrlLocalizer.IncomingUrlFilters, but not having any success there either.

i18n.UrlLocalizer.IncomingUrlFilters += delegate (Uri url) { throw new Exception(url.LocalPath); };

When retrying my request above, the exception reveals: System.Exception: /Content/ So what happened to the rest of the URL (bootstrap/css/bootstrap.min.css)?

Other properties I've defined in App_Start() are: i18n.UrlLocalizer.UrlLocalizationScheme = i18n.UrlLocalizationScheme.Scheme1; i18n.LocalizedApplication.Current.DefaultLanguage = "en-US";

What am I missing?

Any input would be greatly appreciated.

turquoiseowl commented 7 years ago

Can you confirm there is no caching of redirections going on?

The default settings (i.e. no changes at all in App_Start) should result in a .css request NOT being redirected. I've just confirmed that at this end:

Failing all that, you might build against the source and put a breakpoint in i18n.EarlyUrlLocalizer.ProcessIncoming to see what's happening.

turquoiseowl commented 7 years ago

Good way to ensure against caching is to add arbitrary query string to the URL, of course.

hognevevle commented 7 years ago

Seems like IIS was doing some caching of its own, indeed. After restarting IIS, the exclusions were working just fine. I only thought about client caching, and thought I was escaping it all by going the curl route :)

Thanks a lot for your input!