gpt4 book ai didi

How to display text in Angular template from resource file (.rsx) in a ASP.NET Web application?(如何在ASP.NET Web应用程序中的资源文件(.rsx)的Angular模板中显示文本?)

转载 作者:bug小助手 更新时间:2023-10-22 13:51:01 28 4
gpt4 key购买 nike



I'm combining Angular 14 with a legacy ASP.NET Web application and we have static texts in a resource file (.rsx) in a folder outside of Angular.

我将Angular 14与一个遗留的ASP.NET Web应用程序相结合,我们在Angular之外的文件夹中的资源文件(.rsx)中有静态文本。


I need to display those texts in a component's template (inside Angular). How can I do to achieve that?

我需要在组件的模板中显示这些文本(在Angular中)。我该如何做到这一点?


更多回答

Would say that the Angular app can't directly access the .resx file. More like you need to build an API to get the values from the resx file (based on selected culture), and the Angular app call that API.

可以说Angular应用程序无法直接访问.resx文件。更像是,您需要构建一个API来从resx文件(基于选定的区域性)中获取值,然后Angular应用程序调用该API。

Hmm then I guess it already exists (i'm not a backend-developer) because before they used angularJS (I'm rewriting it to latest Angular) and they managed to display the texts. But I'm not sure how to do the same in latest Angular..

嗯,我想它已经存在了(我不是后端开发人员),因为在他们使用angularJS之前(我正在将其重写为最新的Angular),他们成功地显示了文本。但我不知道在最新的Angular中如何做到这一点。。

Ah I found it and know how to access it! :) thanks for your input!

啊,我找到了它,知道如何访问它!:)感谢您的投入!

优秀答案推荐

As mentioned in the comment, it is likely that you need to implement an API to get the resources file's value and return it as a JSON object/dictionary.

正如注释中提到的,您可能需要实现一个API来获取资源文件的值,并将其作为JSON对象/字典返回。


You can refer to ResourceManager.GetResourceSet(CultureInfo, Boolean, Boolean) method to retrieve the resource values.

您可以参考ResourceManager.GetResourceSet(CultureInfo、Boolean、Boolean)方法来检索资源值。


using System.Collections;
using System.Linq;
using System.Resources;

[HttpGet]
[Route("GetResources")]
public ActionResult GetResources(string? languageCode)
{
var rm = new ResourceManager(typeof(NumberResources)); // Replace `NumberResources` with your Resource name

if (String.IsNullOrEmpty(languageCode))
languageCode = "en-US";

ResourceSet rs = rm.GetResourceSet(CultureInfo.CreateSpecificCulture(languageCode),
true, false);
if (rs == null)
{
// Handle if resource file not found
throw BadRequest("Resource not found");
}

var dictionary = resourceSet.Cast<DictionaryEntry>()
.ToDictionary(r => r.Key.ToString(),
r => r.Value.ToString());

return Json(dictionary, JsonRequestBehavior.AllowGet);
}

For the Angular side, implement the service to call the API.

对于Angular方面,实现服务来调用API。


@Injectable({ providedIn: 'root' })
export class LanguageService {
constructor (private httpClient: HttpClient) { }

getLanguage(languageCode: string): Observable<any> {
return this.http.get(`<API Domain>/<Controller path>/GetResources?languageCode=${languageCode}`);
}
}

Caller

呼叫者


constructor (private languageService: LanguageService) { }

ngOnInit() {
this.languageService.getLanguages(/* languageCode */)
.subscribe({
next: (data: any) => {
console.log(data);
}
});
}


I don t think there is a ready to use solution for this, think its best if you provide a dynamic/static generated json that you load in you translation module.

我不认为有现成的解决方案,最好是提供一个动态/静态生成的json,加载到翻译模块中。


something that looks like this

像这样的东西


更多回答

Thank you for your reply, I found that an API already existed so I can access the values from there.

感谢您的回复,我发现API已经存在,所以我可以从那里访问值。

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com