/** * 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); }
@Override protected void doMergeAggregateWithDto(Key targetKey, CSVRepresentation sourceDto) { if (sourceDto.getValue() != null) { for (Map.Entry<String, String> entry : sourceDto.getValue().entrySet()) { String locale = entry.getKey(); String translation = entry.getValue(); if (shouldUpdateTranslation(targetKey, locale, translation)) { targetKey.addTranslation(locale, translation); } } } }
@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; } }
/** * Returns a sub aggregate key with only two translations. * * @param sourceLocale source locale * @param targetLocale target locale * @return Key */ public Key subKey(String sourceLocale, String targetLocale) { Key subKey = new Key(); subKey.setComment(description); subKey.setOutdated(outdated); subKey.setEntityId(entityId); Map<String, Translation> subTranslations = new HashMap<String, Translation>(); addTranslation(subTranslations, subKey, sourceLocale); addTranslation(subTranslations, subKey, targetLocale); subKey.setTranslations(subTranslations); return subKey; }
private void updateTranslation(String locale, String value, Key key) { if (translationHasChanged(key, locale, value)) { if (isDefaultLocale(locale)) { key.setOutdated(); } key.addTranslation(locale, value); } }
private void addDefaultTranslation(KeyRepresentation keyRepresentation, Key key) { String defaultLocale = keyRepresentation.getDefaultLocale(); String translation = keyRepresentation.getTranslation(); boolean approx = keyRepresentation.isApprox(); key.addTranslation(defaultLocale, translation, approx); }
/** * Update translation for the given locale. * * @param keyName key name * @param representation translation representation * @return status code 204 (no content) or 404 (not found) if key or target translation is null. */ @PUT @Path("/{key}") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @RequiresPermissions(I18nPermissions.TRANSLATION_WRITE) public Response updateTranslation(@PathParam("key") String keyName, TranslationRepresentation representation) { WebAssertions.assertNotNull(representation, "The translation should not be null"); TranslationValueRepresentation translationToUpdate = representation.getTarget(); WebAssertions.assertNotNull(translationToUpdate, "The translation target should not be null"); Key key = keyRepository.get(keyName).orElseThrow(() -> new NotFoundException("The key is not found")); key.addTranslation(locale, translationToUpdate.getTranslation(), translationToUpdate.isApprox()); keyRepository.update(key); return Response.noContent().build(); } }
/** * 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; }
/** * Updates a key with the translation in the default language. * * @param representation key representation * @return key representation * @throws URISyntaxException if the URI is not valid. */ @PUT @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @RequiresPermissions(I18nPermissions.KEY_WRITE) public Response updateKey(KeyRepresentation representation) throws URISyntaxException { WebAssertions.assertNotNull(representation, THE_KEY_SHOULD_NOT_BE_NULL); WebAssertions.assertNotBlank(representation.getDefaultLocale(), THE_KEY_SHOULD_CONTAINS_A_LOCALE); Key key = loadKeyIfExistsOrFail(); key.setComment(representation.getComment()); key.setOutdated(); // Updates the default translation key.addTranslation(representation.getDefaultLocale(), representation.getTranslation(), representation.isApprox()); keyRepository.update(key); return Response.ok(new URI(uriInfo.getRequestUri() + "/" + keyName)) .entity(keyFinder.findKeyWithName(keyName)).build(); }
@Override public void mergeDtoIntoAggregate(KeyDTO sourceDto, Key targetAggregate) { targetAggregate.setComment(sourceDto.getComment()); for (TranslationDTO translation : sourceDto.getTranslations()) { targetAggregate.addTranslation(translation.getLocale(), translation.getValue(), translation.isApproximate()); } if (sourceDto.isOutdated()) { targetAggregate.setOutdated(); } for (TranslationDTO translation : sourceDto.getTranslations()) { if (!translation.isOutdated()) { targetAggregate.addTranslation(translation.getLocale(), translation.getValue(), translation.isApproximate()); } } } }
@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); } } }