Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
97 views
in Technique[技术] by (71.8m points)

c# - How to use resx file in javascript file. .net core 3.1 mvc

I'am developing multi language web site with .net core mvc. I used Microsoft.AspNetCore.Mvc.Localization.IHtmlLocalizer.

This code is work fine in .cshtml for me

@inject Microsoft.AspNetCore.Mvc.Localization.IHtmlLocalizer<ExampleProject.WEB.Controllers.HomeController> localizer

@localizer["NotificationManage"]

Also works between <script> tags in _Layout page

 alert('@localizer["NotificationManage"]');

But don't working in js file.

How to use Resx file in javascript file.?

What another best to way make multi language to javascript?

alert('@localizer["NotificationManage"]');

Not working for me.

Alert Result is:@localizer["NotificationManage"];

question from:https://stackoverflow.com/questions/65899148/how-to-use-resx-file-in-javascript-file-net-core-3-1-mvc

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One possible solution is to define your translations as a global variable on the window outside your script

<script>
var translations = {
   notification: '@localizer["NotificationManage"]'
}
</script>
<script type="text/javascript" src="pathtoscript.js"></script>

then inside your script you go

alert(window.translations.notification);

If any better solution is given here I'm happy to hear, because we use it this way in production


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...