自定义Azure B2C策略中声明类型的'requiredField'的本地化

时间:2020-07-01 08:52:34

标签: localization azure-ad-b2c

我有一个自定义Azure B2C策略,并在注册中使用了以下声明:

...
  <ClaimType Id="givenName">
    <DisplayName>First Name</DisplayName>
    <DataType>string</DataType>
    <DefaultPartnerClaimTypes>
      <Protocol Name="OAuth2" 
        PartnerClaimType="given_name" />
      <Protocol Name="OpenIdConnect" 
        PartnerClaimType="given_name" />
      <Protocol Name="SAML2" 
        PartnerClaimType="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname" />
    </DefaultPartnerClaimTypes>
    <UserHelpText>Your given name (also known as first name).</UserHelpText>
    <UserInputType>TextBox</UserInputType>
  </ClaimType>
...

我希望能够将用户未输入值并尝试继续时出现的错误消息本地化。

这是当前默认设置/使用本地化元素的“ required_field”。 但是,还有许多其他这样的要求,即姓氏,我希望为每个要求显示不同的消息。 即“姓氏为必填项”,“姓氏为必填项”等;而不是所有声明都要求“此信息必填”的通用消息。

此文档https://docs.microsoft.com/en-us/azure/active-directory-b2c/localization-string-ids显示了可用的各种ID,但是我仍然看不到要完成此操作吗?

这是当前本地化的外观

...
      <LocalizedResources Id="api.localaccountsignup.en">
    <LocalizedStrings>
      <!-- Header -->
      <LocalizedString ElementType="UxElement" StringId="initial_intro">Create a account to start</LocalizedString>
      <!-- First name -->
      <LocalizedString ElementType="ClaimType" ElementId="givenName" StringId="DisplayName">First name</LocalizedString>
      <LocalizedString ElementType="ClaimType" ElementId="givenName" StringId="UserHelpText"></LocalizedString>
      <LocalizedString ElementType="ClaimType" ElementId="givenName" StringId="PatternHelpText">First name is required.</LocalizedString>
      <!-- Last name -->
      <LocalizedString ElementType="ClaimType" ElementId="surname" StringId="DisplayName">Last name</LocalizedString>
      <LocalizedString ElementType="ClaimType" ElementId="surname" StringId="UserHelpText"></LocalizedString>     
      <LocalizedString ElementType="ClaimType" ElementId="surname" StringId="PatternHelpText"></LocalizedString>     
...
      <LocalizedString ElementType="UxElement" StringId="required_field">This information is required.</LocalizedString>
...

我可以看到本地化帮助文本和模式的方法(用于限制),但是当用户根本不输入任何内容时,什么也找不到。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我从来没有真正解决过这个问题,因为我认为没有解决方案。至少现在不是(2020 年 7 月)

相反,我不得不解决这个问题。 我所做的是,由于 B2C 允许包含静态 HTML,因此我向该页面添加了自定义 Javascript,以添加所需的额外消息。我保持简单,只是使用基于声明名称的 JQuery。

下面的片段

function build() {
    addValdationInput('signInName', 'Email address is required', function validiateInput(e) {
        var value = e.target.value;
        const error = document.getElementById('signInName');
        
        if(isEmpty(value)) {
            error.style.visibility = "visible";
            e.target.style.borderColor = "#bb191c";         
            $('#signInName')).html('Email address is required' + svgItemLevelErrorIconHtml());
        } else if(!isValidEmail(value)) {
            error.style.visibility = "visible";
            e.target.style.borderColor  = "#bb191c";
            $('#signInName')).html('Email address is required' + svgItemLevelErrorIconHtml());
        } else {
            e.target.value = value.trim();
            Controller.hideValidationError(error,e);    
        }
    });
  
  // ...
}
// ...
export function svgItemLevelErrorIconHtml() {
  return '<svg class=...';
}
export function addValdationInput(id, errorText, validationAction) {
    $('#' + id).after(buildValidationHtml(id,errorText));   
    $("#" + id).blur(validationAction);
}
export function buildValidationHtml(id, errorText) {
    var content = '<div id=';
    content += getErrorID(id);
    content += ' class="b2c-error" style="visibility: hidden;">';
    content += svgItemLevelErrorIconHtml();
    content += errorText;
    content += '</div>';
    return content;
}

我知道这不是最好的,因为这意味着我现在需要存储 i18n 字符串的位置,但我找不到替代方法。