blob: 0bf802d79bec01ce8713e77b41023e68047daaa6 [file] [log] [blame]
/**
*
* Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*/
package org.eclipse.osbp.i18n.common;
import java.util.Locale;
import org.eclipse.osbp.runtime.common.dispose.AbstractDisposable;
import org.eclipse.osbp.runtime.common.i18n.II18nService;
import org.eclipse.osbp.ui.api.metadata.IDSLMetadataService;
/**
* A simple I18n service implementation.
*/
public class OSI18nService extends AbstractDisposable implements II18nService {
private IDSLMetadataService dslMetadataService;
private II18nService i18nService;
public OSI18nService(IDSLMetadataService dslMetadataService, II18nService i18nService) {
super();
this.dslMetadataService = dslMetadataService;
this.i18nService = i18nService;
}
@Override
public String getValue(String i18nKey, Locale locale) {
String[] splitI18nKey = i18nKey.split("\\.");
StringBuffer translation = new StringBuffer();
int bracketsToClose = 0;
// LIFO - Therefore starting from the last to the first
for (int i = splitI18nKey.length - 1; i > -1; i--) {
String translationTemp = dslMetadataService.translate(locale.toLanguageTag(), splitI18nKey[i]);
if (translationTemp == null || translationTemp.isEmpty() || translationTemp.equals(i18nKey)) {
return null;
}
// Nested properties will be translated individually and put together
if (i < splitI18nKey.length - 1) {
translation.append("(");
bracketsToClose++;
}
translation.append(translationTemp);
}
for (int i = 0; i < bracketsToClose; i++) {
translation.append(")");
}
return translation.toString();
}
@Override
protected void internalDispose() {
// nothing to do
}
}