@Override public List<TranslationRepresentation> findTranslations(String localeId) { return translationLocaleAssembler.assemble(keyRepository.loadAll(), localeId); }
@Override public void clear() { logger.info("Clearing all i18n keys"); keyRepository.clear(); }
@Override public KeyRepresentation findKeyWithName(String name) { return keyRepository.get(name).map(keyAssembler::assemble).orElse(null); } }
@Override public void commit(boolean clear) { if (clear) { LOGGER.info("Clear i18n key repository"); for (Key key : keyRepository.loadAll()) { keyRepository.delete(key); } } LOGGER.info("staging size: " + staging.size()); for (KeyDTO keyDTO : staging) { try { Key key = fluentAssembler.merge(keyDTO).into(Key.class).fromFactory(); keyRepository.persist(key); } catch (RuntimeException e) { LOGGER.error(e.getMessage(), e); throw e; } } LOGGER.info("Import of i18n key completed"); staging.clear(); }
/** * Deletes all the filtered keys. * * @param isMissing filter on missing default translation * @param isApprox filter on approximate default translation * @param isOutdated filter on outdated key * @param searchName filter on key name * @return http status code 200 (ok) with the number of deleted keys */ @DELETE @Produces("text/plain") @RequiresPermissions(I18nPermissions.KEY_DELETE) public Response deleteKeys(@QueryParam(IS_MISSING) Boolean isMissing, @QueryParam(IS_APPROX) Boolean isApprox, @QueryParam(IS_OUTDATED) Boolean isOutdated, @QueryParam(SEARCH_NAME) String searchName) { KeySearchCriteria keySearchCriteria = new KeySearchCriteria(isMissing, isApprox, isOutdated, searchName); long numberOfDeletedKeys; if (shouldDeleteWithoutFilter(keySearchCriteria)) { numberOfDeletedKeys = keyRepository.size(); keyRepository.clear(); // If no filter are precised use the "deleteAll()" method which is more optimized } else { numberOfDeletedKeys = deleteFilteredKeys(keySearchCriteria); } return Response.ok(String.format("%d deleted keys", numberOfDeletedKeys)).build(); }
/** * 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(); } }
@JpaUnit("seed-i18n-domain") @Transactional @Override public int importKeysWithTranslations(InputStream inputStream) { List<CSVRepresentation> CSVRepresentations = parser.parse(inputStream, CSVRepresentation.class); for (CSVRepresentation dto : CSVRepresentations) { Key key = fluentAssembler.merge(dto).into(Key.class).fromRepository().orFromFactory(); keyRepository.add(key); } return CSVRepresentations.size(); } }
@Override public boolean isInitialized() { return !keyRepository.isEmpty(); }
@Override public boolean isInitialized() { boolean initialized = keyRepository.count() != 0; if(initialized) { LOGGER.info("i18n keys already initialized"); } else { LOGGER.info("i18n keys not initialized"); } return initialized; }
@Override public String localize(String locale, String key, Object... args) { ULocale closestLocale = localeService.getClosestULocale(locale); Key loadedKey = keyRepository.load(key); String localizedString; if (loadedKey == null) { localizedString = '[' + key + ']'; } else { // TODO business logic to search in other locales List<ULocale> localeIterator = getParentLocale(closestLocale); Translation translation = getTranslationWithFallBack(loadedKey, localeIterator); if (translation == null) { localizedString = '[' + key + ']'; } else if (ArrayUtils.isEmpty(args)) { localizedString = translation.getValue(); } else { MessageFormat mf = new MessageFormat(translation.getValue(), closestLocale); localizedString = mf.format(args); } } return localizedString; }
/** * 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(); }
@Override public Iterator<KeyDTO> exportData() { return fluentAssembler.assemble(keyRepository.loadAll()).to(KeyDTO.class).iterator(); } }
private Key loadKeyIfExistsOrFail() { return keyRepository.get(keyName) .orElseThrow(() -> new NotFoundException(String.format(KEY_NOT_FOUND, keyName))); } }
/** * Streams a CSV file with all keys with their translations. * * @return an i18n.csv file */ @GET @Produces(MediaType.TEXT_PLAIN) @RequiresPermissions(I18nPermissions.KEY_READ) public Response exportTranslations() { final List<Key> keys = keyRepository.loadAll(); return Response.ok(new StreamingOutput() { private boolean isFirstLine = true; @Override public void write(OutputStream os) throws IOException { Writer writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); for (Key key : keys) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); renderer.render(byteArrayOutputStream, key, APPLICATION_CSV, printHeader(isFirstLine)); writer.write(byteArrayOutputStream.toString("UTF-8")); isFirstLine = false; } writer.flush(); } }).header(CONTENT_DISPOSITION, ATTACHMENT_FILENAME_I18N_CSV).build(); }
@Override public TranslationRepresentation findTranslation(String localeId, String keyId) { return keyRepository.get(keyId).map(k -> translationLocaleAssembler.assemble(k, localeId)).orElse(null); }
@Override public Map<String, String> getTranslationsForLocale(String locale) { Map<String, String> translations = new HashMap<>(); for (Key key : keyRepository.loadAll()) { Optional<String> translation = getTranslationWithFallback(locale, key); if (translation.isPresent() || !i18nConfig.isAllowMissingTranslations()) { translations.put(key.getId(), translation.orElse("[" + key.getId() + "]")); } } return translations; }
@Override public Optional<String> getTranslationWithFallback(String locale, String keyName) { return keyRepository.get(keyName).flatMap(k -> getTranslationWithFallback(locale, k)); }
@Override public Map<String, String> getTranslationsForLocale(String locale) { ICUBasedLocalizationService icuBasedLocalizationService = (ICUBasedLocalizationService) localizationService; Map<String, String> translations = new HashMap<String, String>(); List<ULocale> localeIterator = icuBasedLocalizationService.getParentLocale(new ULocale(locale)); for (Key key : keyRepository.loadAll()) { Translation translation = icuBasedLocalizationService.getTranslationWithFallBack(key, localeIterator); if (translation != null) { translations.put(key.getEntityId(), translation.getValue()); } } return translations; } }
private void assertKeyDoNotAlreadyExists(KeyRepresentation representation) { keyRepository.get(representation.getName()).ifPresent(k -> {throw new AlreadyExistException();}); }
.getCode()) .orElseThrow(() -> new IllegalArgumentException("Locale not found " + localeRepresentation.getCode())); List<Key> keys = keyRepository.loadAll(); if (keys != null) { int translatedCount = 0;