Локализованное сообщение для ответа об ошибке RestAPI в пользовательской политике B2 C - PullRequest
0 голосов
/ 17 января 2020

Я сделал локализацию в своей пользовательской политике, но на определенных этапах я вызываю REST API для проверки некоторых данных. Ответ приходит в Engli sh, но теперь мне нужно перевести эти сообщения также как часть локализации. Есть ли способ сделать это в B2 C?

Вот ответ, который я получаю от API:

                {
                    "userMessage": "Password is incorrect",
                    "version":"1.0.0",
                    "status:: 409,
                    "code": "API12345",
                    "requestId":"50f0bd91-2ff4-4b8f-828f-00f170519ddb",
                    "developerMessage":"Verbose description of problem and how to fix it.",
                    "moreInfo": "https://restapi/error/API12345/moreinfo"
                }

1 Ответ

1 голос
/ 20 января 2020

Вы можете отправить параметр локализации в REST API и вернуть ему локализованную ошибку. Или вы можете вернуть код ошибки из API вместо строки ошибки. Затем используйте следующий пример, чтобы сделать это в политике:

    <BuildingBlocks>
        <ClaimsSchema>
            <ClaimType Id="errorCode">
                <DisplayName>errorCode</DisplayName>
                <DataType>string</DataType>
                <UserHelpText>A claim responsible for holding response codes to send to the relying party</UserHelpText>
            </ClaimType>
            <ClaimType Id="messageValue">
                <DisplayName>Message</DisplayName>
                <DataType>string</DataType>
                <UserHelpText>A claim responsible for holding response messages to send to the relying party</UserHelpText>
                <UserInputType>Paragraph</UserInputType>
                <Restriction>
                    <Enumeration Text="errorCode1" Value="will get overidden by localization" />
                    <Enumeration Text="errorCode2" Value="will get overidden by localization" />
                </Restriction>
            </ClaimType>
        </ClaimsSchema>
        <ClaimsTransformations>
            <ClaimsTransformation Id="SetMessageId" TransformationMethod="CreateStringClaim">
                <InputParameters>
                    <InputParameter Id="value" DataType="string" Value="errorCode1" /> <!-- Toggle for errorCode2 -->
                </InputParameters>
                <OutputClaims>
                    <OutputClaim ClaimTypeReferenceId="errorCode" TransformationClaimType="createdClaim" />
                </OutputClaims>
            </ClaimsTransformation>
            <ClaimsTransformation Id="GetLocalizedMessage" TransformationMethod="GetMappedValueFromLocalizedCollection">
                <InputClaims>
                    <InputClaim ClaimTypeReferenceId="errorCode" TransformationClaimType="mapFromClaim" />
                </InputClaims>
                <OutputClaims>
                    <OutputClaim ClaimTypeReferenceId="messageValue" TransformationClaimType="restrictionValueClaim" />
                </OutputClaims>
            </ClaimsTransformation>
        </ClaimsTransformations>
        <ContentDefinitions>
            <ContentDefinition Id="api.selfasserted">
                <LocalizedResourcesReferences MergeBehavior="Prepend">
                    <LocalizedResourcesReference Language="en" LocalizedResourcesReferenceId="api.selfasserted.en" />
                    <LocalizedResourcesReference Language="es" LocalizedResourcesReferenceId="api.selfasserted.es" />
                </LocalizedResourcesReferences>
            </ContentDefinition>
        </ContentDefinitions>
        <Localization Enabled="true">
            <SupportedLanguages DefaultLanguage="en" MergeBehavior="ReplaceAll">
                <SupportedLanguage>en</SupportedLanguage>
                <SupportedLanguage>es</SupportedLanguage>
            </SupportedLanguages>
            <LocalizedResources Id="api.selfasserted.en">
                <LocalizedCollections>
                    <LocalizedCollection ElementType="ClaimType" ElementId="messageValue" TargetCollection="Restriction">
                        <Item Text="errorCode1" Value="First message in english" />
                        <Item Text="errorCode2" Value="Second message in english" />
                    </LocalizedCollection>
                </LocalizedCollections>
            </LocalizedResources>
            <LocalizedResources Id="api.selfasserted.es">
                <LocalizedCollections>
                    <LocalizedCollection ElementType="ClaimType" ElementId="messageValue" TargetCollection="Restriction">
                        <Item Text="errorCode1" Value="Primer mensaje en español" />
                        <Item Text="errorCode2" Value="Segundo mensaje en español" />
                    </LocalizedCollection>
                </LocalizedCollections>
            </LocalizedResources>
        </Localization>
    </BuildingBlocks>
    <ClaimsProviders>
        <ClaimsProvider>
            <DisplayName>Self Asserted</DisplayName>
            <TechnicalProfiles>
                <TechnicalProfile Id="SelfAsserted-WelcomePage">
                    <DisplayName>User profile</DisplayName>
                    <Protocol Name="Proprietary" Handler="Web.TPEngine.Providers.SelfAssertedAttributeProvider, Web.TPEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
                    <Metadata>
                        <Item Key="ContentDefinitionReferenceId">api.selfasserted</Item>
                    </Metadata>
                    <InputClaimsTransformations>
                        <InputClaimsTransformation ReferenceId="SetMessageId" />
                        <InputClaimsTransformation ReferenceId="GetLocalizedMessage" />
                    </InputClaimsTransformations>
                    <InputClaims>
                        <InputClaim ClaimTypeReferenceId="messageValue" />
                    </InputClaims>
                    <OutputClaims>
                        <OutputClaim ClaimTypeReferenceId="email"/>
                        <OutputClaim ClaimTypeReferenceId="messageValue"/>
                    </OutputClaims>
                </TechnicalProfile>
            </TechnicalProfiles>
        </ClaimsProvider>
    </ClaimsProviders>
    <UserJourneys>
        <UserJourney Id="Localization_Tester">
            <OrchestrationSteps>
                <OrchestrationStep Order="1" Type="ClaimsExchange">
                    <ClaimsExchanges>
                        <ClaimsExchange Id="SelfAsserted-WelcomePage" TechnicalProfileReferenceId="SelfAsserted-WelcomePage" />
                    </ClaimsExchanges>
                </OrchestrationStep>
                <OrchestrationStep Order="2" Type="ClaimsExchange">
                    <ClaimsExchanges>
                        <ClaimsExchange Id="AAD-UserReadUsingEmailAddress" TechnicalProfileReferenceId="AAD-UserReadUsingEmailAddress" />
                    </ClaimsExchanges>
                </OrchestrationStep>
                <OrchestrationStep Order="3" Type="SendClaims" CpimIssuerTechnicalProfileReferenceId="JwtIssuer" />
            </OrchestrationSteps>
        </UserJourney>
    </UserJourneys>
...