Search in sources :

Example 1 with ContextualLocalizationManager

use of org.xwiki.localization.ContextualLocalizationManager in project xwiki-platform by xwiki.

the class SeparatePageRating method save.

@Override
public void save() throws RatingsException {
    DocumentReference superadmin = new DocumentReference("xwiki", XWiki.SYSTEM_SPACE, XWikiRightService.SUPERADMIN_USER);
    try {
        if (document == null) {
            throw new RatingsException(RatingsException.MODULE_PLUGIN_RATINGS, RatingsException.ERROR_RATINGS_SAVERATING_NULLDOCUMENT, "Cannot save invalid separate page rating, the rating document is null");
        }
        document.setCreatorReference(superadmin);
        document.setAuthorReference(superadmin);
        ContextualLocalizationManager localization = Utils.getComponent(ContextualLocalizationManager.class);
        context.getWiki().saveDocument(getDocument(), localization.getTranslationPlain("rating.saveComment"), true, context);
    } catch (XWikiException e) {
        throw new RatingsException(e);
    }
}
Also used : ContextualLocalizationManager(org.xwiki.localization.ContextualLocalizationManager) RatingsException(org.xwiki.ratings.RatingsException) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 2 with ContextualLocalizationManager

use of org.xwiki.localization.ContextualLocalizationManager in project xwiki-platform by xwiki.

the class XWikiMessageToolBridgeTest method before.

@Before
public void before() throws Exception {
    Locale.setDefault(Locale.ROOT);
    // checking
    this.preferencesDocumentReference = new DocumentReference(this.oldcore.getXWikiContext().getWikiId(), "XWiki", "XWikiPreferences");
    XWikiDocument preferencesDocument = new XWikiDocument(this.preferencesDocumentReference);
    BaseObject preferencesObject = new BaseObject();
    preferencesObject.setXClassReference(new LocalDocumentReference("XWiki", "XWikiPreferences"));
    preferencesDocument.addXObject(preferencesObject);
    preferencesDocument.setSyntax(Syntax.PLAIN_1_0);
    this.oldcore.getSpyXWiki().saveDocument(preferencesDocument, "", this.oldcore.getXWikiContext());
    this.defaultWikiTranslationReference = new DocumentReference(this.oldcore.getXWikiContext().getWikiId(), "XWiki", "Translations");
    XWikiDocument defaultWikiTranslation = new XWikiDocument(this.defaultWikiTranslationReference);
    defaultWikiTranslation.setSyntax(Syntax.PLAIN_1_0);
    this.oldcore.getSpyXWiki().saveDocument(defaultWikiTranslation, "", this.oldcore.getXWikiContext());
    // MessageTool
    this.tool = new XWikiMessageTool(this.oldcore.getMocker().<ContextualLocalizationManager>getInstance(ContextualLocalizationManager.class));
}
Also used : LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ContextualLocalizationManager(org.xwiki.localization.ContextualLocalizationManager) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Before(org.junit.Before)

Example 3 with ContextualLocalizationManager

use of org.xwiki.localization.ContextualLocalizationManager in project xwiki-platform by xwiki.

the class BooleanClassTest method testLocalization.

/**
 * Test localization.
 */
@Test
public void testLocalization() throws Exception {
    // Setup
    ContextualLocalizationManager contextualLocalizationManager = this.mocker.getMocker().registerMockComponent(ContextualLocalizationManager.class);
    when(contextualLocalizationManager.getTranslationPlain("Some.Class_prop_0")).thenReturn("Nay");
    when(contextualLocalizationManager.getTranslationPlain("Some.Class_prop_1")).thenReturn("Aye");
    when(contextualLocalizationManager.getTranslationPlain("Some.Class_prop_2")).thenReturn("Dunno");
    when(contextualLocalizationManager.getTranslationPlain("yesno_0")).thenReturn("No");
    when(contextualLocalizationManager.getTranslationPlain("yesno_1")).thenReturn("Yes");
    when(contextualLocalizationManager.getTranslationPlain("truefalse_0")).thenReturn("False");
    when(contextualLocalizationManager.getTranslationPlain("truefalse_1")).thenReturn("True");
    when(contextualLocalizationManager.getTranslationPlain("active_0")).thenReturn("Inactive");
    when(contextualLocalizationManager.getTranslationPlain("active_1")).thenReturn("Active");
    when(contextualLocalizationManager.getTranslationPlain("allow_0")).thenReturn("Deny");
    when(contextualLocalizationManager.getTranslationPlain("allow_1")).thenReturn("Allow");
    DocumentReference classReference = new DocumentReference(this.mocker.getXWikiContext().getWikiId(), "Some", "Class");
    EntityReferenceSerializer<String> entityReferenceSerializer = this.mocker.getMocker().registerMockComponent(EntityReferenceSerializer.TYPE_STRING, "local");
    when(entityReferenceSerializer.serialize(classReference)).thenReturn("Some.Class");
    // Create a Boolean meta-property and an associated object with a property instance
    BooleanClass metaProperty = new BooleanClass();
    BaseClass cls = new BaseClass();
    BaseObject obj = new BaseObject();
    IntegerProperty prop = new IntegerProperty();
    prop.setValue(0);
    obj.safeput("prop", prop);
    cls.setDocumentReference(classReference);
    metaProperty.setObject(cls);
    StringBuffer out = new StringBuffer();
    // Test the default translations, should be the default "yesno" display type
    metaProperty.displayView(out, "prop", "", obj, this.mocker.getXWikiContext());
    assertEquals("No", out.toString());
    out.setLength(0);
    prop.setValue(1);
    metaProperty.displayView(out, "prop", "", obj, this.mocker.getXWikiContext());
    assertEquals("Yes", out.toString());
    out.setLength(0);
    prop.setValue(2);
    metaProperty.displayView(out, "prop", "", obj, this.mocker.getXWikiContext());
    assertEquals("---", out.toString());
    // Test translations when display type is "active"
    metaProperty.setDisplayType("active");
    out.setLength(0);
    prop.setValue(0);
    metaProperty.displayView(out, "prop", "", obj, this.mocker.getXWikiContext());
    assertEquals("Inactive", out.toString());
    out.setLength(0);
    prop.setValue(1);
    metaProperty.displayView(out, "prop", "", obj, this.mocker.getXWikiContext());
    assertEquals("Active", out.toString());
    out.setLength(0);
    prop.setValue(2);
    metaProperty.displayView(out, "prop", "", obj, this.mocker.getXWikiContext());
    assertEquals("---", out.toString());
    // Test translations when display type is the non-existing "blacktive"
    metaProperty.setDisplayType("blacktive");
    out.setLength(0);
    prop.setValue(0);
    metaProperty.displayView(out, "prop", "", obj, this.mocker.getXWikiContext());
    assertEquals("0", out.toString());
    out.setLength(0);
    prop.setValue(1);
    metaProperty.displayView(out, "prop", "", obj, this.mocker.getXWikiContext());
    assertEquals("1", out.toString());
    out.setLength(0);
    prop.setValue(2);
    metaProperty.displayView(out, "prop", "", obj, this.mocker.getXWikiContext());
    assertEquals("---", out.toString());
    // Test translations with the full classname_prop_value format
    metaProperty.setName("prop");
    out.setLength(0);
    prop.setValue(0);
    metaProperty.displayView(out, "prop", "", obj, this.mocker.getXWikiContext());
    assertEquals("Nay", out.toString());
    out.setLength(0);
    prop.setValue(1);
    metaProperty.displayView(out, "prop", "", obj, this.mocker.getXWikiContext());
    assertEquals("Aye", out.toString());
    out.setLength(0);
    prop.setValue(2);
    metaProperty.displayView(out, "prop", "", obj, this.mocker.getXWikiContext());
    assertEquals("Dunno", out.toString());
}
Also used : IntegerProperty(com.xpn.xwiki.objects.IntegerProperty) ContextualLocalizationManager(org.xwiki.localization.ContextualLocalizationManager) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Example 4 with ContextualLocalizationManager

use of org.xwiki.localization.ContextualLocalizationManager in project xwiki-platform by xwiki.

the class XWikiDocument method getInternalPropertyName.

public static String getInternalPropertyName(String propname, XWikiContext context) {
    ContextualLocalizationManager localizationManager = Utils.getComponent(ContextualLocalizationManager.class);
    String cpropname = StringUtils.capitalize(propname);
    return localizationManager == null ? cpropname : localizationManager.getTranslationPlain(cpropname);
}
Also used : ContextualLocalizationManager(org.xwiki.localization.ContextualLocalizationManager) ToString(org.suigeneris.jrcs.util.ToString)

Aggregations

ContextualLocalizationManager (org.xwiki.localization.ContextualLocalizationManager)4 DocumentReference (org.xwiki.model.reference.DocumentReference)3 BaseObject (com.xpn.xwiki.objects.BaseObject)2 XWikiException (com.xpn.xwiki.XWikiException)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 IntegerProperty (com.xpn.xwiki.objects.IntegerProperty)1 Before (org.junit.Before)1 Test (org.junit.Test)1 ToString (org.suigeneris.jrcs.util.ToString)1 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)1 RatingsException (org.xwiki.ratings.RatingsException)1