I have a profile script that checks the user's device language and checks against keys for a match to show a translated message. Device language is stored in another profile script called languageDevice.
However, I can't save when I refer to it in this script with var deviceLanguage = user.get('languageDevice');
I also tried var deviceLanguage = mbox.param('languageDevice'); with no luck
function getTranslation() {
//var deviceLanguage = user.get('languageDevice');
//var deviceLanguage = mbox.param('languageDevice');
var deviceLanguage = "zh";
// Extract language code from deviceLanguage using a regular expression
var languageCode = deviceLanguage.match(/[a-z]{2,3}(?:-|_)?(?=-|$)/i)[0];
//console.log("Device Language:", deviceLanguage);
//console.log("Language Code:", languageCode);
var translations = {
"ar": "اكتشف SBS بأكثر من 63 لغة",
"de": "Erleben Sie SBS in 63 Sprachen",
"el": "Ανακαλύψτε τη Ραδιοφωνία SBS σε 63 γλώσσες",
"es": "Descubre SBS en más de 63 idiomas",
"fil": "Tuklasin ang SBS sa 63 na Wika",
"fr": "Découvrez SBS en 63 langues",
"hi": "63 भाषाओं में एसबीएस की खोज करें",
"it": "Scopri SBS in oltre 63 lingue",
"ko": "63개 언어로 SBS를 만나보세요",
"ne": "एसबीएसलाई ६३ भाषाहरूमा पाउनुहोस्",
"pa": "63 ਭਾਸ਼ਾਵਾਂ ਵਿੱਚ SBS ਖੋਜੋ",
"ps": "په 60 ژبو کې SBS ومومئ",
"ur": "63 زبانوں میں SBS دریافت کریں۔",
"vi": "Trải nghiệm SBS với hơn 63 ngôn ngữ",
"zh": "以 63 种语言探索 SBS",
"zh-Hans": "以 63 种语言探索 SBS",
"zh-hans": "以 63 种语言探索 SBS",
"zh-Hant": "以63種語言收聽及瀏覧SBS內容",
"zh-hant": "以63種語言收聽及瀏覧SBS內容"
};
var matchedTranslation = translations[languageCode]; //substring match
if(matchedTranslation) {
//console.log("Matched Translation Key:", languageCode);
return matchedTranslation;
}
// default to English title
return 'Discover SBS in 63 Languages';
}
//console.log(getTranslation());
getTranslation();
I get no errors or warnings, so why can't I save the script?
Solved! Go to Solution.
Topics help categorize Community content and increase your ability to discover relevant content.
@KeithDavo If Postman with delivery API is working please do check the implementation that right mbox(for regional scenario) and mbox parameters are passing from page.If you are familiar with network call debugging please AEP debugger use https://experienceleague.adobe.com/en/docs/experience-platform/debugger/home
Once more ask, is this server side implementation or client implementation and what library and version details will help more.
@KeithDavo first to solve your code, change the line user.get('languageDevice') with user.get('languageDevice')
|| <<default-value>> to get going.
My other two cents for profile scripts, don't divide your logic across many profile scripts like you did for language as order of profile scripts execution is not controllable, always try to code in one profile script or have default value logic to handle first hit conditions (this is like variable initialization with some value if no value from requested profile script), I hope this solves your issue.
Thanks nnakirikanti, I've tried referencing the languageDevice script and getting value from mbox custom parameter.
Oddly, response tokens contain the correct language and translation values but they don't output in the experience.
@KeithDavo Good to know that response tokens have the expected value, please do provide the code snippet in order to solve rendering in experience.
This is the experience JSON referring to the profile scripts:
{
"experienceType":"layout-change",
"lifetime": "test",
"location":"",
"targetComponent":"TabbedShelf",
"factory": "TabbedShelf",
"blockType": "TabbedShelf",
"data": {
"recommendedTitle":"${user.translatedTabbedShelfTitleDevice}",
"title": "${user.translatedTabbedShelfTitleDevice}",
"languageName": "${user.languageDeviceName}"
}
}
and these are the scripts:
translatedTabbedShelfTitleDevice
var deviceLanguage = mbox.param('languageDevice') || 'en';
// Extract language code from deviceLanguage using a regular expression
var languageCode = deviceLanguage.match(/[a-z]{2,3}(?:-|_)?(?=-|$)/i)[0];
var translations = {
"ar": "اكتشف SBS بأكثر من 63 لغة",
"de": "Erleben Sie SBS in 63 Sprachen",
"el": "Ανακαλύψτε τη Ραδιοφωνία SBS σε 63 γλώσσες",
"en": "Discover SBS in 63 Languages",
"es": "Descubre SBS en más de 63 idiomas",
"fil": "Tuklasin ang SBS sa 63 na Wika",
"fr": "Découvrez SBS en 63 langues",
"hi": "63 भाषाओं में एसबीएस की खोज करें",
"it": "Scopri SBS in oltre 63 lingue",
"ko": "63개 언어로 SBS를 만나보세요",
"ne": "एसबीएसलाई ६३ भाषाहरूमा पाउनुहोस्",
"pa": "63 ਭਾਸ਼ਾਵਾਂ ਵਿੱਚ SBS ਖੋਜੋ",
"ps": "په 60 ژبو کې SBS ومومئ",
"ur": "63 زبانوں میں SBS دریافت کریں۔",
"vi": "Trải nghiệm SBS với hơn 63 ngôn ngữ",
"zh": "以 63 种语言探索 SBS",
};
var matchedTranslation = translations[languageCode]; //substring match
if(matchedTranslation) {
//console.log("Matched Translation Key:", languageCode);
return matchedTranslation; // Return the translation value immediately
}
//default to English title
return 'Discover SBS in 63 Languages';
languageDeviceName
var deviceLanguage = mbox.param('languageDevice') || 'en';
var translations = {
"ar": 'العربية',
"de": 'Deutsch',
"el": 'Ελληνικά',
"en": 'English',
"es": 'Español',
"fil": 'Filipino',
"fr": 'Français',
"hi": 'हिन्दी',
"it": 'Italiano',
"ko": '한국어',
"ne": 'नेपाली',
"pa": 'ਪੰਜਾਬੀ',
"ps": "پښتو",
"ur": 'اردو',
"vi": 'Tiếng Việt',
"zh": '普通话',
"more": 'More'
};
for (var key in translations) {
if (deviceLanguage.indexOf(key) !== -1) {
return translations[key]; // Return the translation value immediately
}
}
// If no match is found, return a default value
return 'Translation not found';
Both output correct value if mbox parameter value is passed in Postman but doesn't personalise the experience on page.
@KeithDavo If Postman with delivery API is working please do check the implementation that right mbox(for regional scenario) and mbox parameters are passing from page.If you are familiar with network call debugging please AEP debugger use https://experienceleague.adobe.com/en/docs/experience-platform/debugger/home
Once more ask, is this server side implementation or client implementation and what library and version details will help more.
Views
Likes
Replies