如何在云代码功能内的 Parse Server 中使用 i18n(本地化)?

How to use i18n (localization) in Parse Server, inside a cloud code function?

我正在尝试在 Parse Cloud 函数内本地化推送通知文本,但经过多次尝试后我无法找到有效的解决方案。有没有办法在 Parse Server 云函数中本地化文本?

我遇到了同样的问题,在发送通知之前用UTF-8编码解决了。

npm 包:UTF8

用法:

var utf8 = require("utf8");


// encode before sending the text
text = utf8.encode(text);

因此,对于任何寻求解决方案的人,我使用了以下库:i18n-node

然后在云代码中(我用的是Typescript):

import i18n from 'i18n';

//... other imports

i18n.configure({
    locales:['en', 'it'],
    directory: __dirname + '/locales'
});

然后在云函数里面就可以运行:

i18n.__({phrase: "Hey, well done!", locale: locale}

区域设置可以来自请求,或者在我的例子中来自用户的设备语言偏好。