-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ob 7109 add support for informal language fix #106
Merged
tkuzynow
merged 7 commits into
develop
from
OB-7109-add-support-for-informal-language-fix
Jan 17, 2024
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d40b088
fix: informal language
tkuzynow 401d53c
fix: informal language
tkuzynow 74d1e9a
Merge branch 'develop' into OB-7109-add-support-for-informal-language…
tkuzynow 5446588
fix: informal language
tkuzynow 42cd7a1
Merge remote-tracking branch 'origin/OB-7109-add-support-for-informal…
tkuzynow 08bd471
fix: add fallback logic in case informal file not found
tkuzynow 82b45b7
fix: typo
tkuzynow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GERMAN :)