/** * Returns a sub aggregate key with just the default translation. * * @param defaultLocale source locale * @return Key */ public Key subKey(String defaultLocale) { Key subKey = new Key(); subKey.setComment(description); subKey.setOutdated(outdated); subKey.setEntityId(entityId); Map<String, Translation> subTranslations = new HashMap<String, Translation>(); addTranslation(subTranslations, subKey, defaultLocale); subKey.setTranslations(subTranslations); return subKey; }
private void assembleKeyInfo(Key key, TranslationRepresentation representation) { representation.setName(key.getId()); representation.setComment(key.getComment()); }
/** * Remove the translation corresponding to the given locale code. * * @param locale locale code * @throws java.lang.IllegalArgumentException if locale is blank */ public void removeTranslation(String locale) { Preconditions.checkArgument(StringUtils.isNotBlank(locale)); Translation translation = getTranslation(locale); if (translation != null) { this.getTranslations().remove(translation); } }
@Override public Key createKey(String name, String comment, boolean outdated) { Key key = new Key(); key.setEntityId(name); key.setComment(comment); key.setOutdated(outdated); return key; }
@Override public Key createKey(String name) { Key key = new Key(); key.setEntityId(name); return key; }
@Override protected void doMergeAggregateWithDto(Key targetEntity, KeyDTO sourceDto) { targetEntity.setComment(sourceDto.getComment()); targetEntity.setOutdated(sourceDto.isOutdated()); for (TranslationDTO tln : sourceDto.getTranslations()) { targetEntity.addTranslation(tln.getLocale(), tln.getValue(), tln.isApproximate(), false); } } }
@Override public void mergeAggregateIntoDto(Key sourceAggregate, KeyDTO targetDto) { targetDto.setName(sourceAggregate.getId()); targetDto.setComment(sourceAggregate.getComment()); targetDto.setOutdated(sourceAggregate.isOutdated()); for (Translation tln : sourceAggregate.getTranslations().values()) { targetDto.addTranslationDTO(tln.getId().getLocale(), tln.getValue(), tln.isOutdated(), tln.isApproximate()); } }
@Override protected void doAssembleDtoFromAggregate(KeyDTO targetDto, Key sourceEntity) { targetDto.setName(sourceEntity.getEntityId()); targetDto.setComment(sourceEntity.getComment()); targetDto.setOutdated(sourceEntity.isOutdated()); for (Translation tln : sourceEntity.getTranslations().values()) { targetDto.addTranslationDTO(tln.getEntityId().getLocale(), tln.getValue(), tln.isOutdated(), tln.isApproximate()); } }
@Override public Key createKey(String name, String comment, Map<String, Triplet<String, Boolean, Boolean>> translations) { Key key = new Key(); key.setEntityId(name); key.setComment(comment); for (Map.Entry<String, Triplet<String, Boolean, Boolean>> entry : translations.entrySet()) { key.addTranslation(entry.getKey(), entry.getValue().getValue0(), entry.getValue().getValue1(), entry.getValue().getValue2()); } return key; } }
@Override public KeyRepresentation assemble(Key key) { KeyRepresentation keyRepresentation = new KeyRepresentation(); keyRepresentation.setName(key.getId()); keyRepresentation.setComment(key.getComment()); keyRepresentation.setOutdated(key.isOutdated()); String defaultLocale = localeService.getDefaultLocale(); keyRepresentation.setDefaultLocale(defaultLocale); assembleDefaultTranslation(key, keyRepresentation, defaultLocale); return keyRepresentation; }
private void addKeyWithTranslationsToFile(Key key, CSVRowWriter csvRowWriter) { for (String columnName : csvRowWriter.getColumnNames()) { if (isColumnKey(columnName)) { csvRowWriter.addColumnValue(I18nCSVTemplateLoader.KEY, key.getId()); } else { if (key.isTranslated(columnName)) { csvRowWriter.addColumnValue(columnName, key.getTranslation(columnName).getValue()); } } } }
private void updateTranslation(String locale, String value, Key key) { if (translationHasChanged(key, locale, value)) { if (isDefaultLocale(locale)) { key.setOutdated(); } key.addTranslation(locale, value); } }
private boolean translationHasChanged(Key targetKey, String localeCode, String translationValue) { Translation translation = targetKey.getTranslation(localeCode); return translation == null || !translation.getValue().equals(translationValue); } }
private Optional<String> getTranslationWithFallback(String locale, Key key) { for (String parentLocale : getParentLocalesFor(new ULocale(locale))) { if (key.isTranslated(parentLocale)) { return Optional.of(key.getTranslation(parentLocale).getValue()); } } if (i18nConfig.isTranslationFallback()) { String defaultLocale = localeService.getDefaultLocale(); if (key.isTranslated(defaultLocale)) { return Optional.of(key.getTranslation(defaultLocale).getValue()); } } return Optional.empty(); }
@Override protected void doAssembleDtoFromAggregate(CSVRepresentation targetDto, Key sourceEntity) { targetDto.setKey(sourceEntity.getId()); Map<String, String> translations = new HashMap<>(); for (Map.Entry<String, Translation> entry : sourceEntity.getTranslations().entrySet()) { translations.put(entry.getKey(), entry.getValue().getValue()); } targetDto.setValue(translations); }
/** * Saves or updates the translation for the specified locale. * If the key was outdated, checks if the key is still outdated. * * @param locale specified the translation locale * @param value translation value * @return the new translation * @throws java.lang.IllegalArgumentException if the locale is null or empty * or contains other characters than letters and "-". */ public Translation addTranslation(String locale, String value) { return addTranslation(locale, value, false); }
/** * Inserts a key with the translation in the default language. * * @param keyRepresentation key representation * @return 201 if the resource is created, 409 if the resource already existed */ @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @RequiresPermissions(I18nPermissions.KEY_WRITE) public Response createKey(KeyRepresentation keyRepresentation) throws URISyntaxException { WebAssertions.assertNotNull(keyRepresentation, THE_KEY_SHOULD_NOT_BE_NULL); WebAssertions.assertNotBlank(keyRepresentation.getName(), THE_KEY_SHOULD_CONTAINS_A_NAME); WebAssertions.assertNotBlank(keyRepresentation.getDefaultLocale(), THE_KEY_SHOULD_CONTAINS_A_LOCALE); assertKeyDoNotAlreadyExists(keyRepresentation); Key key = factory.createKey(keyRepresentation.getName()); key.setComment(keyRepresentation.getComment()); addDefaultTranslation(keyRepresentation, key); keyRepository.add(key); return Response.created(new URI(uriInfo.getRequestUri() + "/" + key.getId())) .entity(keyFinder.findKeyWithName(key.getId())).build(); }
/** * Returns the translation corresponding to the given locale code. * * @param locale locale code * @return translation * @throws java.lang.IllegalArgumentException if locale is blank */ public Translation getTranslation(String locale) { Preconditions.checkArgument(StringUtils.isNotBlank(locale), "The locale should not be blank"); return this.getTranslations().get(locale); }
private void assembleSourceTranslation(TranslationRepresentation representation, Key key) { String defaultLocale = localeService.getDefaultLocale(); if (defaultLocale != null && key.isTranslated(defaultLocale)) { TranslationValueRepresentation source = assembleTranslation(key, defaultLocale); representation.setSource(source); } }
@Override public Key createKey(String name) { return new Key(name); } }