Skip to content

Is there a way start app in another culture rather than the default? #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
fabercs opened this issue Apr 9, 2019 · 7 comments
Open

Comments

@fabercs
Copy link

fabercs commented Apr 9, 2019

Hi,

First of all, thanks for your efforts on this.

I am trying to implement your work on my project. I have two supported languages, English and Turkish. The default culture should be Turkish (tr), however I would like to keep controller and action names in English in the source code. These names would generate the urls in English as well. So in Startup.cs I changed default culture to > tr
LocalizationRouteDataHandler.DefaultCulture = "tr";

However, it does not generate localized urls, instead generates in english. But if I set ti to en, and then change the culture to tr, then it generate the localized urls.

What I mean is if I give an example on your sample project, I want this project to start with
LocalizationRouteDataHandler.DefaultCulture = "fi";
and all actions shold be localized on startup.

Could you help me on this?

@saaratrix
Copy link
Owner

I did not think about the use case where you would want a controller in english but a different default culture when I wrote this.

In the LocalizationRouteConvention.cs where it sets the actual routing templates it uses the ControllerName for the default culture.
In the function AddControllerRoutes() it does the following:

string controllerName = controllerModel.ControllerName;

LocalizationRouteDataHandler.AddControllerRouteData(controllerName, LocalizationRouteDataHandler.DefaultCulture, controllerName);

And the ControllerName is the class name minus Controller. If HomeController then the ControllerName is Home

To support what you'd want I think instead of using the ControllerModel.ControllerName directly it would need to use a localized name, I'm not sure how difficult it would be to implement without breaking existing functionality. Or if more changes are necessary with how it changes the <a href="">

@fabercs
Copy link
Author

fabercs commented Apr 9, 2019

The only option I came up without changing any core functionality (as I am not familiar with the routing in net core) is at the begginnig set the DefaultCulture to en then as soon as request reach to HomeController, reset DefaultCulture to tr or fi which is desired as default. :) I did not tried that but I will.

But, no matter what, this codebase should support this usecase as well, any supported culture could be default (think of accept headers).

@saaratrix
Copy link
Owner

I agree that it should be supported. It should be a common use case since coding in english is common.

Most likely the routing because of default ASP.NET will keep the controller name and actions, I don't remember if you can remove that.

So I think most likely for you for HomeController the routing would end up being:
/Home,
/Ev (Google translate Home to turkish)
/en/Home

And then it's the job of the CultureActionLinkTagHelper.cs to make sure that it selects
/Ev and not /Home if both exists.

@fabercs
Copy link
Author

fabercs commented Apr 10, 2019

Actually what I need is;

/en/Home/Index
/tr/Ev/Giris

/en/Example/Parameter
/tr/Ornek/Parametre

I tried below, but the default culture's routes are being omitted in somewhere, I guess, didn't work. Also I tried to use Route attributes Name property for a workaround to rename actions in Turkish, but this way throws exceptions.

// Set up cultures
LocalizationRouteDataHandler.DefaultCulture = "tr";
LocalizationRouteDataHandler.SupportedCultures = new Dictionary<string, string>()
{
    { "en", "English" },
    { "tr", "Türkçe" }
};
[LocalizationRoute("tr", "Ornek")]
[LocalizationRoute("en", "Example")]
public class ExampleController : LocalizationController
{
    [LocalizationRoute("tr", "Parametre")]
    [LocalizationRoute("en", "Parameter")]
    public IActionResult Parameter(int index, string test)
    {
        ViewData["index"] = index;
        ViewData["test"] = test;
        ViewData["post"] = false;
        return View();
    }
}

As it takes long time to grasp the code to check/test if this is doable, I need your opinion. What do you think?

@saaratrix
Copy link
Owner

saaratrix commented Apr 10, 2019

If I remember correctly myself they are omitted. I can't remember there being an option to force the default culture.

If you want to force /en/ and /tr/ you could try and create a third culture and redirect to /tr/ if that third culture is found by using a middleware for requests as a workaround.

I tested some and if you define a [LocalizationRoute] for a default culture you can still access it by the /defaultCulture/name route but every route that doesn't have a [LocalizationRoute] will throw an error for default culture.

Which means that it's probably easier to fix the initial issue, That you can write the controller name in english but use a different default culture.

@fabercs
Copy link
Author

fabercs commented Apr 10, 2019

If you want to force /en/ and /tr/ you could try and create a third culture and redirect to /tr/ if that third culture is found by using a middleware for requests as a workaround.

I want to do this but, shouldn't UseMvc be the last middleware in the pipeline? Lets say I have a dummy default culture wa, at the end of the pipleine I need to redierct reuqest to /tr/Home, how can I do this?

Edit:
I found how to do it here and here

@saaratrix
Copy link
Owner

You just use a middleware I believe.
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-2.2

It could also be possible inside the LocalizationController.cs- OnActionExecuting
To add a check for culture and maybe return a context.Result = new RedirectResult()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants