-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from Onlineberatung/OB-7109-add-support-for-i…
…nformal-language-fix Ob 7109 add support for informal language fix
- Loading branch information
Showing
14 changed files
with
295 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/java/de/caritas/cob/mailservice/api/service/DefaultTranslationsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package de.caritas.cob.mailservice.api.service; | ||
|
||
import de.caritas.cob.mailservice.api.model.Dialect; | ||
import de.caritas.cob.mailservice.config.apiclient.TranlationMangementServiceApiClient; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.io.IOUtils; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Slf4j | ||
@Service | ||
public class DefaultTranslationsService { | ||
|
||
public String fetchDefaultTranslations(String translationComponentName, String languageCode, | ||
Dialect dialect) { | ||
InputStream inputStream = tryFetchDefaultTranslationWithFallbackToEmptyDialect(translationComponentName, languageCode, dialect); | ||
if (inputStream == null) { | ||
return "{}"; | ||
} | ||
try { | ||
final List<String> fileLines = IOUtils | ||
.readLines(inputStream, StandardCharsets.UTF_8.displayName()); | ||
return String.join("", fileLines); | ||
} catch (IOException ex) { | ||
throw new IllegalStateException(String.format( | ||
"Json file with translations could not be loaded, translation component name: %s", | ||
translationComponentName), ex); | ||
} | ||
} | ||
|
||
private InputStream tryFetchDefaultTranslationWithFallbackToEmptyDialect(String translationComponentName, String languageCode, | ||
Dialect dialect) { | ||
InputStream inputStream = getInputStream(translationComponentName, languageCode, | ||
dialect); | ||
if (inputStream == null) { | ||
log.warn( | ||
"Default translations for component {}, language {}, dialect {} not found in resources. Will try to fallback to default translations for empty dialect.", | ||
translationComponentName, | ||
languageCode, dialect); | ||
|
||
inputStream = getInputStream(translationComponentName, languageCode, null); | ||
if (inputStream == null) { | ||
log.warn( | ||
"Default translations for component {}, language {} and empty dialect not found in resources. Returning empty translations.", | ||
translationComponentName, | ||
languageCode); | ||
return null; | ||
} | ||
} | ||
return inputStream; | ||
} | ||
|
||
private InputStream getInputStream(String translationComponentName, String languageCode, | ||
Dialect dialect) { | ||
String translationFilename = getTranslationFilename( | ||
translationComponentName + "." + languageCode | ||
+ TranlationMangementServiceApiClient.getDialectSuffix(dialect)); | ||
return TranslationService.class.getResourceAsStream(translationFilename); | ||
} | ||
|
||
private String getTranslationFilename(String templateName) { | ||
return "/i18n/" + templateName.toLowerCase() + ".json"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/test/java/de/caritas/cob/mailservice/api/TranslationMessageSourceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package de.caritas.cob.mailservice.api; | ||
|
||
import static org.mockito.Mockito.verify; | ||
|
||
import de.caritas.cob.mailservice.api.model.Dialect; | ||
import de.caritas.cob.mailservice.api.service.TranslationService; | ||
import java.util.Locale; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class TranslationMessageSourceTest { | ||
|
||
private static final String INFORMAL_GERMAL_LANGUAGE_TAG = "de-DE-u-va-posix"; | ||
|
||
@InjectMocks | ||
private TranslationMessageSource translationMessageSource; | ||
|
||
@Mock | ||
private TranslationService translationService; | ||
|
||
@Test | ||
void getMessage_Should_CallFetchTranslations_With_FormalDialect_For_DefaultLocale() { | ||
// given | ||
Locale locale = Locale.getDefault(); | ||
|
||
// when | ||
translationMessageSource.getMessage("translation_key", new Object[]{}, "Message", locale); | ||
|
||
// then | ||
verify(translationService, Mockito.times(1)).fetchTranslations(locale.getLanguage(), Dialect.FORMAL); | ||
} | ||
|
||
@Test | ||
void getMessage_Should_CallFetchTranslations_With_InformalDialect_For_ForLocaleForInformalGerman() { | ||
// given | ||
Locale locale = Locale.forLanguageTag(INFORMAL_GERMAL_LANGUAGE_TAG); | ||
|
||
// when | ||
translationMessageSource.getMessage("translation_key", new Object[]{}, "Message", locale); | ||
|
||
// then | ||
verify(translationService, Mockito.times(1)).fetchTranslations(locale.getLanguage(), Dialect.INFORMAL); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.