Skip to content
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

Localizable component #824

Merged
merged 4 commits into from
Oct 20, 2020
Merged

Conversation

hishamco
Copy link
Contributor

@sbwalker this tested and will make component localization more easier

[Parameter]
public string ResourceKey { get; set; }

protected string Localize(string name)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a little note here, I had seen T is common name for translation, so can we use it instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you provide more detail on what you are asking?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just if we can rename Localize to T or just forget it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My preference is to use Localize() as its much more explicit/intuitive - I would have no idea what T means.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

T for Translate, I'm with you, seems I need to add a check in every child component, cause we have 3 cases:

  1. Localizable component and key is exist

  2. Localizable component and key isn't exist

  3. Non localizable component

I will do a final test before merge

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Localization

Everything works as except, regarding the above cases the result should be:

  1. The translated text

  2. {ResourceKey}.{Property}

  3. Use the property value

@hishamco
Copy link
Contributor Author

You can merge after the upcoming commit because there's a case needed where the resource key is not set

@hishamco
Copy link
Contributor Author

@sbwalker it's ready to merge

@sbwalker sbwalker merged commit e61cd3d into oqtane:master Oct 20, 2020
@hishamco hishamco deleted the localizable-component branch October 20, 2020 13:41
if (ModuleState?.ModuleType != null)
{
var moduleType = Type.GetType(ModuleState.ModuleType);
var localizerTypeName = $"Microsoft.Extensions.Localization.IStringLocalizer`1[[{moduleType.AssemblyQualifiedName}]], Microsoft.Extensions.Localization.Abstractions";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooh, how did I forgot that we already have IStringLocalizerFactory.Create() ;) I will try to simplify this soon

Thanks again

var localizerTypeName = $"Microsoft.Extensions.Localization.IStringLocalizer`1[[{moduleType.AssemblyQualifiedName}]], Microsoft.Extensions.Localization.Abstractions";
var localizerType = Type.GetType(localizerTypeName);

// HACK: Use ServiceActivator instead of injecting IHttpContextAccessor, because HttpContext throws NRE in WebAssembly runtime
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using IHttpContextAccessor in a Blazor Server app could expose you to security vulnerabilities. Components do not guarantee they execute in the context of a user's request (think a timer that's updated in the background or a chat component that causes other instances of chat components to update when you post a message). You should not use it in general since it's an anti-pattern, but definitely not in a server app.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @pranavkm for pointing to this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hishamco are you going to be able to take a look at this? Or should I make an attempt at it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sbwalker if you meant IStringLocalizerFactory I already started today on this on, but one point I'm still think about, the factory will create the IStringLocalizer but my aim in the PR was to retrieve it from the DI. I will update you on this soon, before releasing 2.0.0

Copy link
Contributor Author

@hishamco hishamco Nov 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems something broken in localization, I will try to figure out what happen into the latest few PRs

It was a naming issue in my resources ;)

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

Successfully merging this pull request may close these issues.

3 participants