Skip to content

Commit

Permalink
Do Locale matching first in Componentservice.getMessages
Browse files Browse the repository at this point in the history
  • Loading branch information
jessiejuachon committed Oct 23, 2020
1 parent 236ab56 commit 7c2535c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,32 +100,32 @@ public MessageCacheItem getMessages() {
* <li>The messages in a fallback locale</li>
* </ul>
*/
public MessageCacheItem getMessages(Iterator<Locale> fallbackLocalesIter) {
CacheService cacheService = new CacheService(dto);
MessageCacheItem cacheItem = cacheService.getCacheOfComponent();
if (cacheItem != null) { // Item is in cache
if (cacheItem.isExpired()) {
// Refresh the cacheItem in a separate thread
refreshCacheItemTask(cacheItem);
}
} else { // Item is not in cache.
public MessageCacheItem getMessages(Iterator<Locale> fallbackLocalesIter) {
this.doLocaleMatching();

CacheService cacheService = new CacheService(dto);
MessageCacheItem cacheItem = cacheService.getCacheOfComponent();
if (cacheItem != null) { // Item is in cache
if (cacheItem.isExpired())
refreshCacheItemTask(cacheItem); // Refresh the cacheItem in a separate thread
} else { // Item is not in cache.
ProductService ps = new ProductService(dto);
Locale locale = Locale.forLanguageTag(dto.getLocale());
if (ps.isSupportedLocale(locale) || VIPCfg.getInstance().isPseudo()) {
cacheItem = createCacheItem(); // Fetch for the requested locale from data store, create cacheItem and store in cache
if (cacheItem.getCachedData().isEmpty()) // Failed to fetch messages for the requested locale
cacheItem = getFallbackLocaleMessages(fallbackLocalesIter);
} else { // Requested locale is not supported
Locale matchedLocale = LocaleUtility.pickupLocaleFromList(ps.getSupportedLocales(), locale);
if (matchedLocale != null) { // Requested locale matches a supported locale (eg. requested locale "fr_CA matches supported locale "fr")
MessagesDTO matchedLocaleDTO = new MessagesDTO(dto.getComponent(), matchedLocale.toLanguageTag(), dto.getProductID(), dto.getVersion());
cacheItem = new ComponentService(matchedLocaleDTO).getMessages();
} else // Requested locale is not supported and does not match any supported locales
cacheItem = getFallbackLocaleMessages(fallbackLocalesIter);
}
}
return cacheItem;
}
} else // Requested locale is not supported and does not match any supported locales
cacheItem = getFallbackLocaleMessages(fallbackLocalesIter);
}
return cacheItem;
}

private void doLocaleMatching() {
Locale matchedLocale = LocaleUtility.pickupLocaleFromList(new ProductService(dto).getSupportedLocales(), Locale.forLanguageTag(dto.getLocale()));
if (matchedLocale != null) // Requested locale matches a supported locale (eg. requested locale "fr_CA matches supported locale "fr")
dto.setLocale(matchedLocale.toLanguageTag());
}

/**
* Gets the messages in the next fallback locale by passing the next fallback locale DTO to a new instance of ComponentService
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"id" : "2ef91ce6-582f-3238-88fb-8770259b07f6",
"request" : {
"url" : "/i18n/api/v2/translation/products/JavaclientTest/versions/1.0.0/locales/zh-Hans/components/JAVA?pseudo=true",
"method" : "GET"
},
"response" : {
"status" : 200,
"bodyFileName" : "body-i18n-api-v2-translation-products-JavaclientTest-versions-1.0.0-locales-zh-CN-components-JAVA-zNUip.json",
"headers" : {
"Cache-Control" : "private",
"Expires" : "Thu, 01 Jan 1970 00:00:00 GMT",
"Set-Cookie" : "JSESSIONID=4BA5DF67D12D8AA2CD3585DC43FA1CA1; Path=/; Secure; HttpOnly",
"Access-Control-Allow-Origin" : "",
"Access-Control-Allow-Methods" : "GET, POST, PUT, DELETE, OPTIONS",
"Access-Control-Allow-Headers" : "csp-auth-token, Content-Type, x-xmp-ui",
"Access-Control-Allow-Credentials" : "${vipservice.cross.domain.allowCredentials}",
"Access-Control-Max-Age" : "3600",
"ETag" : "\"0eabb94eb52f28dd091eb1285fe05ea90\"",
"Content-Type" : "application/json;charset=UTF-8",
"Date" : "Wed, 23 Oct 2019 08:06:39 GMT"
}
},
"uuid" : "2ef91ce6-582f-3238-88fb-8770259b07f6"
}

0 comments on commit 7c2535c

Please sign in to comment.