Skip to content
This repository has been archived by the owner on Nov 29, 2018. It is now read-only.

Localization no longer works after updating to rc1 #150

Closed
damienbod opened this issue Nov 18, 2015 · 30 comments
Closed

Localization no longer works after updating to rc1 #150

damienbod opened this issue Nov 18, 2015 · 30 comments
Assignees

Comments

@damienbod
Copy link

https://github.com/damienbod/AspNet5Localization

Greetings Damien

@hishamco
Copy link
Contributor

It works fine .. what's the issue you are facing?

@damienbod
Copy link
Author

Does not localize the string anymore, console, or in VS

Result is Name (key) instead of for example name it-CH

Greetings Damien

@hishamco
Copy link
Contributor

I'll check your source later

@damienbod
Copy link
Author

Thanks

@hishamco
Copy link
Contributor

Can you let me know which dnx version you are using?

@damienbod
Copy link
Author

1.0.0-rc1-final clr x86 win

Greetings Damien

@hishamco
Copy link
Contributor

Why you didn't switch to rc2, because I faced another issue in previous versions have a look #147

@damienbod
Copy link
Author

Hi @hishamco
Thanks for your reply.

I used rc1 because it was released yesterday. Are you saying rc1 has a problem?

Greetings Damien

@hishamco
Copy link
Contributor

@damienbod I run the localization sample, and it works fine, are you sure there's no missing piece in your app?

@danroth27
Copy link
Member

@ryanbrandenburg Can you please take a look?

@damienbod
Copy link
Author

@hishamco, not that I know of.

The following URL is no longer localized since the update to rc1

http://localhost:5000/api/about?culture=it-CH

DataAnnotations localization still works.

All I did was update the project.json, changed the Startup.cs with the breaking changes. You can see this in the history. If I downgrade to beta8, it works.

I also tried upgrading to rc2 with no success. Tested in the console.

Thanks for your help

Greetings Damien

@hishamco
Copy link
Contributor

Now I will clone the repo and test it

@hishamco
Copy link
Contributor

Seems it doesn't work as expected, can you let me know the latest dnx version where you got the right result?

@damienbod
Copy link
Author

Hi @hishamco

Version 1.0.0-beta8 works as expected. Got it from NuGet (released version)

Greetings Damien

@hishamco
Copy link
Contributor

I'm not sure if the issue is cause by #138, but seems @ryanbrandenburg will investigate into this

@Guillaume-Fortin
Copy link

Reproduce in rc1 with https://github.com/damienbod/AspNet5Localization, my own test and https://github.com/aspnet/Localization/tree/dev/samples/LocalizationSample.
i test later with a IStringLocalizerFactory.

@hishamco
Copy link
Contributor

Even I it doesn't localize anything with me, but localization sample works fine, i'm not sure if this related to #138 as I mentioned before or there's a bug in mvc localization

@Guillaume-Fortin
Copy link

in beta8, culture and uiculture is correct

  • when i debug in vs 2015 it don't change resx file with culture.
  • when i use dnx web command, it's ok.

In rc1 no resx found when i use IStringLocalizer and HtmlLocalizer.
you can see this problem inside my web stream : https://www.livecoding.tv/video/frbodibuilding-report-website-mobile-app-2/

@ryanbrandenburg
Copy link
Contributor

There are a couple things happening here. One is that the convention you're using doesn’t work well with IHtmlLocalizer. Check out https://github.com/aspnet/Mvc/tree/dev/test/WebSites/LocalizationWebSite for an example of the new way. Basically now instead of having "IHtmlLocalizer" You want to do "IHtmlLocalizer" and have your resource as "AspNet5Localization.Controllers.AboutController..resx". It is POSSIBLE to do the "AmazingResource" method, but it's not recommended.

Another part of this is that it's no longer recommended to have a "Default.resx". Instead the "key" of the resource should be the default value for it. So instead of having "Greeting" with a value in the default resx of "Hello" the key is "Hello", which will be returned if no matching language is found.

Also for what @Thetyne is talking about, where VS will not localize properly because the resources aren't handled correctly, that's tracked here aspnet/dnx#3047

@hishamco
Copy link
Contributor

@ryanbrandenburg you are right that's the pattern that we should follow, but if the resource file resident with that same place of the controller issue #138 will happen

@damienbod
Copy link
Author

Hi @ryanbrandenburg

Thanks for your answers.

So you’re saying if you want to use the new localization, you have to/should name the resource like the controller with this magic string.

Not very nice, or developer friendly. (Just my opinion)

I then need to have one resx per controller. So for an application with a 100 controllers and 3 languages I would have 300 resource files.

This would be a nightmare.

Your recommendation that the key is the default value. This does not work for most of the applications which I am involved in.

Most company’s program in English. Most of our applications are translated into de-CH, fr-CH and it-CH, we usually have no default English view. (Switzerland). The default key nearly never matches the default localization.

We do not translate the application ourselves, we usually send the keys as a single file to a separate company which translates this for us. This is usually sent as a csv or excel file. This has to be imported and exported so if I have a lot of separate resource files, this costs too much.

This issue should be a bug. At present in rc1, this does not work, in beta8, it was ok unlike the linked issue.

Maybe you could consider rewriting the use cases for MVC 6 localization, because if it is released as it is, recommended, it will be unusable for most of our applications and for many other company’s which deal in localization regularly. It would mean that I have to re-implement localization, if I want to use ASP.NET 5 for my clients.

Thanks and greetings Damien

@joeaudette
Copy link

@damienbod wrote "I then need to have one resx per controller. So for an application with a 100 controllers and 3 languages I would have 300 resource files."

I have found that I can create an empty class in my web app, say I name it LocalizationWebSite.Components.SharedResources.cs
This class has no properties or methods.
Then take a dependency of

IHtmlLocalizer<SharedResources> 

in my controller and I can localize strings from a resx file named

LocalizationWebSite.Components.SharedResources.fr.resx

This file lives in the /Resources folder of the web app which we point to from startup

services.AddMvc().AddViewLocalization(options => options.ResourcesPath = "Resources");

I could use this in multiple controllers, so we are not forced to use a resx file per controller.

The problem I have at the moment is I can't seem to get it to work if my controller classes or my resource class are in a class library instead of directly in the web app.

If I have a classlibrary named SomeWebLib with a class named CommonResources and in HomeController of the web app I take a dependency on

IHtmlLocalizer<SomeWebLib.CommonResources> 

in the constructor
I cannot seem to get any localization by having a file named
SomeWebLib.CommonResources.fr.resx in the webapp Resources folder

I'm using kestrel from the command line to avoid the known VS 2015 issues. I'm setting my browser language to French while testing.
The project I'm testing with is here and is just a modified version of the LocalizationWebSite project from the mvc repo tests:
https://github.com/joeaudette/experiments

I don't know if these limitations are expected or whether to consider them bugs or whether there is some other approach I'm supposed to use if my controllers and/or resources are in a classlibrary instead of directly in the web app.

@ryanbrandenburg
Copy link
Contributor

@damienbod, @joeaudette 's suggestion should work for re-using the resource.

As for using the default value as the key, you should be able to use the old style of key selection, just know that if it doesn't find a resource for the given key and culture it will default to using the key given. So if your key was "greeting" and you didn't have that set for fr-CH your page would show "greeting".

@joeaudette your class library thing sounds like a separate thing, please feel free to file it as a new issue.

I think that's everything @damienbod so I'm closing this issue, but let us know if we missed something or you have further questions.

@damienbod
Copy link
Author

Hi @ryanbrandenburg

Thanks for your answer. It should be possible to use a Shared resource without having to create an empty class. Hopefully this is not the recommended way to use shared resources.

This is not a good solution, but as least it works. So if I create ten shared resources, I need ten emtpy classes in the project to use the resx files.

@joeaudette Thanks for the workaround.

As a developer, it should be possible to define how resource files are named and also where the resources are saved.

Greetings Damien

@ryanbrandenburg
Copy link
Contributor

It is also an option to implement your own IStringLocalizerFactory and/or IStringLocalizer with your expected behavior.

@joeaudette
Copy link

@Guillaume-Fortin
Copy link

@ryanbrandenburg thanks for reply.

I have implement IStringLocalizerFactory and IStringLocalizer for read json file and it's work perfectly.

@jmurkoth
Copy link

@joeaudette @damienbod .. Ran into the same issue as you did and the work around of using an empty class doesn't seem to work when you are using Data annotation to drive model validation in the view
I took @damienbod 's example and added a view to illustrate the problem. Code is available here
https://github.com/jmurkoth/LocalizationIssue .
Get the following error
System.InvalidOperationException' in mscorlib.dll ("The resource type 'AspNet5Localization.Resources.AmazingResource' does not have an accessible static property named 'BoxLengthRequired'

@ryanbrandenburg
Copy link
Contributor

@jmurkoth it looks like you're using the "Old" style of Resource selection with 'ErrorMessageResourceType'. You're perfectly welcome to do that, but it won't go through the new system which all the Empty class stuff above is intended to work with. If you want to use "ErrorMessageResourceType" you'll have to point it at an old-school "Designer" class. If that doesn't solve you or you run into something else feel free to open a new issue.

@damienbod
Copy link
Author

@joeaudette @hishamco @danroth27 @Thetyne @jmurkoth I've just created a global issue for this, could you give your views here?

dotnet/aspnetcore#1142

Greetings Damien

@Eilon Eilon removed the 3 - Done label Dec 2, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants