Пример #1
0
 /** Test methods i18nManager.getLocaleOrFallback() */
 @Test
 public void testGetLocaleOrFallback() {
   // Normal case
   Locale locale = i18nMgr.getLocaleOrDefault("de");
   assertNotNull(locale);
   assertEquals(i18nMgr.getLocaleOrNull("de"), locale);
   // Unexisting locale case
   locale = i18nMgr.getLocaleOrDefault("xy");
   assertEquals(I18nModule.getDefaultLocale(), locale);
   // Test trying to get overlay via getLocale method which should not return the overlay
   Locale overlay = i18nMgr.getLocaleOrDefault("de__" + I18nModule.getOverlayName());
   assertEquals(I18nModule.getDefaultLocale(), overlay);
   overlay = i18nMgr.getLocaleOrDefault("zh_CN__" + I18nModule.getOverlayName());
   assertEquals(I18nModule.getDefaultLocale(), overlay);
 }
Пример #2
0
  /**
   * Test methods i18nManager inline translation tool and
   * InlineTranslationInterceptHandlerController
   */
  @Test
  public void testInlineTranslationReplaceLocalizationMarkupWithHTML() {
    // enable inline translation markup
    i18nMgr.setMarkLocalizedStringsEnabled(null, true);
    Translator inlineTrans =
        Util.createPackageTranslator(
            InlineTranslationInterceptHandlerController.class, i18nMgr.getLocaleOrNull("de"));
    URLBuilder inlineTranslationURLBuilder = new jUnitURLBuilder();
    String testBundle = "org.olat.core.util.i18n.junittestdata";
    String testKey = "no.need.to.translate.this";
    String rawtext1 = "Lorem impsum<b>nice stuff</b>";
    String rawtext2 = "Holderadio <ul>lsdfsdf<y  asdf blblb";
    String combinedKey = testBundle + ":" + testKey;
    // test method that adds identifyers around the translation items
    String plainVanillaWrapped =
        i18nMgr.getLocalizedString(
            testBundle, testKey, null, i18nMgr.getLocaleOrNull("de"), false, false);
    String plainVanillaPlain = "just a test";
    Pattern plainVanillaWrappedPattern =
        Pattern.compile(
            I18nManager.IDENT_PREFIX
                + combinedKey
                + ":([0-9]*?)"
                + I18nManager.IDENT_START_POSTFIX
                + plainVanillaPlain
                + I18nManager.IDENT_PREFIX
                + combinedKey
                + ":\\1"
                + I18nManager.IDENT_END_POSTFIX);
    Matcher m = plainVanillaWrappedPattern.matcher(plainVanillaWrapped);
    assertTrue(m.find());

    // test find-replace translator identifyers with HTML markup
    StringOutput inlineTransLink = new StringOutput();
    String[] args = (combinedKey + ":1000000001").split(":");
    InlineTranslationInterceptHandlerController.buildInlineTranslationLink(
        args, inlineTransLink, inlineTrans, inlineTranslationURLBuilder);
    // Plain vanilla text
    String convertedToHTMLMarkup =
        InlineTranslationInterceptHandlerController.replaceLocalizationMarkupWithHTML(
            plainVanillaWrapped, inlineTranslationURLBuilder, inlineTrans);
    assertEquals(
        "<span class=\"b_translation_i18nitem\">"
            + inlineTransLink.toString()
            + plainVanillaPlain
            + "</span>",
        convertedToHTMLMarkup);
    // Simple link
    String linkOPEN =
        "<a href=\"http://www.olat.org/bla/blu:bli#bla\" title='funny title' class=\"b_css b_anothercss\">";
    String linkCLOSE = "</a>";
    String inlineSpanOPEN = "<span class=\"b_translation_i18nitem\">";
    String inlineSpanCLOSE = "</span>";
    String translatedWithinLink = linkOPEN + plainVanillaWrapped + linkCLOSE + rawtext1;
    convertedToHTMLMarkup =
        InlineTranslationInterceptHandlerController.replaceLocalizationMarkupWithHTML(
            translatedWithinLink, inlineTranslationURLBuilder, inlineTrans);
    String convertedWithinLinkExpected =
        inlineSpanOPEN
            + inlineTransLink.toString()
            + linkOPEN
            + plainVanillaPlain
            + linkCLOSE
            + inlineSpanCLOSE
            + rawtext1;
    assertEquals(convertedWithinLinkExpected, convertedToHTMLMarkup);
    // Simple link with span
    String linkSpanOPEN = "<span class=\"bluber\">";
    String linkSpanCLOSE = "</span>";
    String translatedWithinLinkAndSpan =
        rawtext2 + linkOPEN + linkSpanOPEN + plainVanillaWrapped + linkSpanCLOSE + linkCLOSE;
    convertedToHTMLMarkup =
        InlineTranslationInterceptHandlerController.replaceLocalizationMarkupWithHTML(
            translatedWithinLinkAndSpan, inlineTranslationURLBuilder, inlineTrans);
    String convertedWithinLinkAndSpanExpected =
        rawtext2
            + inlineSpanOPEN
            + inlineTransLink.toString()
            + linkOPEN
            + linkSpanOPEN
            + plainVanillaPlain
            + linkSpanCLOSE
            + linkCLOSE
            + inlineSpanCLOSE;
    assertEquals(convertedWithinLinkAndSpanExpected, convertedToHTMLMarkup);
    // Muliple links
    String translatedWithinMultipleLinks =
        translatedWithinLink + translatedWithinLinkAndSpan + translatedWithinLinkAndSpan;
    convertedToHTMLMarkup =
        InlineTranslationInterceptHandlerController.replaceLocalizationMarkupWithHTML(
            translatedWithinMultipleLinks, inlineTranslationURLBuilder, inlineTrans);
    String convertedWithinMultipleLinksExpected =
        convertedWithinLinkExpected
            + convertedWithinLinkAndSpanExpected
            + convertedWithinLinkAndSpanExpected;
    assertEquals(convertedWithinMultipleLinksExpected, convertedToHTMLMarkup);
    // Input elements
    String inputOPEN = "<input type='submit' class=\"bluber\" value=\"";
    String inputCLOSE = "\" />";
    String translatedWithinInput = inputOPEN + plainVanillaWrapped + inputCLOSE + rawtext1;
    convertedToHTMLMarkup =
        InlineTranslationInterceptHandlerController.replaceLocalizationMarkupWithHTML(
            translatedWithinInput, inlineTranslationURLBuilder, inlineTrans);
    String convertedWithinInputExpected =
        inlineSpanOPEN
            + inlineTransLink.toString()
            + inputOPEN
            + plainVanillaPlain
            + inputCLOSE
            + inlineSpanCLOSE
            + rawtext1;
    assertEquals(convertedWithinInputExpected, convertedToHTMLMarkup);
    // checkbox elements
    String checkboxOPEN = "<input type='submit' class=\"bluber\" type=\"checkbox\" value=\"";
    String checkboxCLOSE = "\" />";
    String translatedWithinCheckbox = checkboxOPEN + plainVanillaWrapped + checkboxCLOSE + rawtext1;
    convertedToHTMLMarkup =
        InlineTranslationInterceptHandlerController.replaceLocalizationMarkupWithHTML(
            translatedWithinCheckbox, inlineTranslationURLBuilder, inlineTrans);
    String convertedWithinCheckboxExpected =
        checkboxOPEN + plainVanillaPlain + checkboxCLOSE + rawtext1;
    assertEquals(convertedWithinCheckboxExpected, convertedToHTMLMarkup);
    // Input and links mixed
    String translatedWithinMultipleLinksAndInput =
        translatedWithinLink
            + rawtext1
            + translatedWithinInput
            + translatedWithinLinkAndSpan
            + rawtext2
            + translatedWithinInput
            + translatedWithinLinkAndSpan;
    convertedToHTMLMarkup =
        InlineTranslationInterceptHandlerController.replaceLocalizationMarkupWithHTML(
            translatedWithinMultipleLinksAndInput, inlineTranslationURLBuilder, inlineTrans);
    String convertedWithinMultipleLinksAndInputExpected =
        convertedWithinLinkExpected
            + rawtext1
            + convertedWithinInputExpected
            + convertedWithinLinkAndSpanExpected
            + rawtext2
            + convertedWithinInputExpected
            + convertedWithinLinkAndSpanExpected;
    assertEquals(convertedWithinMultipleLinksAndInputExpected, convertedToHTMLMarkup);
    // Within element attribute
    String attributeOPEN = "<a href='sdfsdf' title=\"";
    String attributeCLOSE = "\" class=\"b_bluber\">hello world</a>";
    String translatedWithinAttribute =
        attributeOPEN + plainVanillaWrapped + attributeCLOSE + rawtext1;
    convertedToHTMLMarkup =
        InlineTranslationInterceptHandlerController.replaceLocalizationMarkupWithHTML(
            translatedWithinAttribute, inlineTranslationURLBuilder, inlineTrans);
    String convertedWithinAttributeExpected =
        attributeOPEN + plainVanillaPlain + attributeCLOSE + rawtext1;
    assertEquals(convertedWithinAttributeExpected, convertedToHTMLMarkup);
    // Ultimate test
    String translatedUltimate =
        translatedWithinMultipleLinksAndInput
            + rawtext1
            + translatedWithinAttribute
            + translatedWithinMultipleLinksAndInput
            + rawtext2
            + translatedWithinInput
            + translatedWithinLinkAndSpan;
    convertedToHTMLMarkup =
        InlineTranslationInterceptHandlerController.replaceLocalizationMarkupWithHTML(
            translatedUltimate, inlineTranslationURLBuilder, inlineTrans);
    String convertedUltimateExpected =
        convertedWithinMultipleLinksAndInputExpected
            + rawtext1
            + convertedWithinAttributeExpected
            + convertedWithinMultipleLinksAndInputExpected
            + rawtext2
            + convertedWithinInputExpected
            + convertedWithinLinkAndSpanExpected;
    assertEquals(convertedUltimateExpected, convertedToHTMLMarkup);

    // don't do inline translation markup
    i18nMgr.setMarkLocalizedStringsEnabled(null, false);
  }