private boolean translationHasChanged(Key targetKey, String localeCode, String translationValue) { Translation translation = targetKey.getTranslation(localeCode); return translation == null || !translation.getValue().equals(translationValue); } }
/** * Checks if the key has outdated translation * and then update the outdated indicator of the key. */ public void updateOutdated() { outdated = false; for (Translation translation : translations.values()) { if (translation.isOutdated()) { outdated = true; } } }
/** * 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 * @param approximate translation approximate indicator * @param outdated translation outdated indicator * @throws java.lang.IllegalArgumentException if locale is blank */ public void addTranslation(String locale, String value, boolean approximate, boolean outdated) { Preconditions.checkArgument(StringUtils.isNotBlank(locale)); // If exists, get the translation for the given locale Translation translation = this.translations.get(locale); if (translation == null) { translation = new Translation(); translation.setEntityId(new TranslationId(entityId, locale)); } translation.setValue(value); translation.setApproximate(approximate); translation.setOutdated(outdated); translation.setKey(this); translations.put(locale, translation); updateOutdated(); }
private Translation createOrUpdateTranslation(String locale, String value, boolean isApproximate) { Translation translation; if (isTranslated(locale)) { translation = this.translations.get(new TranslationId(entityId, locale)); translation.updateValue(value); } else { translation = new Translation(new TranslationId(entityId, locale), value); } translation.setApproximate(isApproximate); translations.put(new TranslationId(entityId, locale), translation); return translation; }
@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()); } }
private TranslationValueRepresentation assembleTranslation(Key key, String locale) { TranslationValueRepresentation source = new TranslationValueRepresentation(); source.setLocale(locale); Translation translation = key.getTranslation(locale); source.setTranslation(translation.getValue()); source.setApprox(translation.isApproximate()); source.setOutdated(translation.isOutdated()); return source; }
private void assembleDefaultTranslation(Key key, KeyRepresentation keyRepresentation, String defaultLocale) { if (key.isTranslated(defaultLocale)) { Translation translation = key.getTranslation(defaultLocale); keyRepresentation.setTranslation(translation.getValue()); keyRepresentation.setApprox(translation.isApproximate()); } else { keyRepresentation.setMissing(true); } } }
/** * Marks the key and all its translations as outdated. */ public void setOutdated() { this.outdated = true; for (Translation translation : this.translations.values()) { translation.setOutdated(); } }
private void addTranslation(Map<String, Translation> translations, Key key, String locale) { Translation source = this.translations.get(locale); if (source == null) { source = new Translation(new TranslationId(key.entityId, locale), key, "", false, false); } translations.put(locale, source); }
/** * Removes the translation corresponding to the given locale code. * * @param locale locale code * @throws java.lang.IllegalArgumentException if the locale is null or empty * or contains other characters than letters and "-". */ public void removeTranslation(String locale) { LocaleCodeSpecification.assertCode(locale); Translation translation = getTranslation(locale); if (translation != null) { this.translations.remove(translation.getId()); } }
private boolean translationHasChanged(Key key, String locale, String newTranslation) { Translation translation = key.getTranslation(locale); return translation == null || !newTranslation.equals(translation.getValue()); }
/** * Checks if the key has outdated translations and then update the outdated status of the key. */ public void checkOutdatedStatus() { boolean newStatus = false; for (Translation translation : translations.values()) { if (translation.isOutdated()) { newStatus = true; break; } } outdated = newStatus; }
@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); }
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 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; } }
@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; }
.getId()); if (translation != null && StringUtils.isNotBlank(translation.getValue())) { translatedCount++;
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()); } } } }