GRN2-xx: Added fallback language to locales (#788)

* Added fallback language to locales

* Added comments
This commit is contained in:
Ahmad Farhat 2019-09-06 14:28:47 -04:00 committed by Jesus Federico
parent 2090b9f8e6
commit b590a5a264
5 changed files with 41 additions and 9 deletions

View File

@ -121,6 +121,9 @@ Metrics/LineLength:
Metrics/MethodLength: Metrics/MethodLength:
Enabled: false Enabled: false
Metrics/ModuleLength:
Enabled: false
# A calculated magnitude based on number of assignments, # A calculated magnitude based on number of assignments,
# branches, and conditions. # branches, and conditions.
Metrics/AbcSize: Metrics/AbcSize:

View File

@ -21,3 +21,25 @@ $(document).on('turbolinks:load', function(){
document.addEventListener("turbolinks:before-cache", function() { document.addEventListener("turbolinks:before-cache", function() {
$(".alert").remove() $(".alert").remove()
}) })
// Gets the localized string
function getLocalizedString(key) {
var keyArr = key.split(".")
var translated = I18n
// Search current language for the key
keyArr.forEach(function(k) {
translated = translated[k]
})
// If key is not found, search the fallback language for the key
if (translated == undefined) {
translated = I18nFallback
keyArr.forEach(function(k) {
translated = translated[k]
})
}
return translated
}

View File

@ -32,10 +32,10 @@ $(document).on('turbolinks:load', function(){
if (success) { if (success) {
inviteURL.blur(); inviteURL.blur();
copy.addClass('btn-success'); copy.addClass('btn-success');
copy.html("<i class='fas fa-check'></i>" + I18n["copied"]) copy.html("<i class='fas fa-check'></i>" + getLocalizedString("copied"))
setTimeout(function(){ setTimeout(function(){
copy.removeClass('btn-success'); copy.removeClass('btn-success');
copy.html("<i class='fas fa-copy'></i>" + I18n["copy"]) copy.html("<i class='fas fa-copy'></i>" + getLocalizedString("copy"))
}, 2000) }, 2000)
} }
}); });
@ -49,7 +49,7 @@ $(document).on('turbolinks:load', function(){
// Display and update all fields related to creating a room in the createRoomModal // Display and update all fields related to creating a room in the createRoomModal
$("#create-room-block").click(function(){ $("#create-room-block").click(function(){
$("#create-room-name").val("") $("#create-room-name").val("")
$("#create-room-access-code").text(I18n["modal"]["create_room"]["access_code_placeholder"]) $("#create-room-access-code").text(getLocalizedString("modal.create_room.access_code_placeholder"))
$("#room_access_code").val(null) $("#room_access_code").val(null)
$("#createRoomModal form").attr("action", $("body").data('relative-root')) $("#createRoomModal form").attr("action", $("body").data('relative-root'))
@ -94,10 +94,10 @@ $(document).on('turbolinks:load', function(){
accessCode = $(this).closest("#room-block").data("room-access-code") accessCode = $(this).closest("#room-block").data("room-access-code")
if(accessCode){ if(accessCode){
$("#create-room-access-code").text(I18n["modal"]["create_room"]["access_code"] + ": " + accessCode) $("#create-room-access-code").text(getLocalizedString("modal.create_room.access_code") + ": " + accessCode)
$("#room_access_code").val(accessCode) $("#room_access_code").val(accessCode)
} else{ } else{
$("#create-room-access-code").text(I18n["modal"]["create_room"]["access_code_placeholder"]) $("#create-room-access-code").text(getLocalizedString("modal.create_room.access_code_placeholder"))
$("#room_access_code").val(null) $("#room_access_code").val(null)
} }
}) })
@ -140,11 +140,11 @@ function generateAccessCode(){
accessCode += validCharacters.charAt(Math.floor(Math.random() * validCharacters.length)); accessCode += validCharacters.charAt(Math.floor(Math.random() * validCharacters.length));
} }
$("#create-room-access-code").text(I18n["modal"]["create_room"]["access_code"] + ": " + accessCode) $("#create-room-access-code").text(getLocalizedString("modal.create_room.access_code") + ": " + accessCode)
$("#room_access_code").val(accessCode) $("#room_access_code").val(accessCode)
} }
function ResetAccessCode(){ function ResetAccessCode(){
$("#create-room-access-code").text(I18n["modal"]["create_room"]["access_code_placeholder"]) $("#create-room-access-code").text(getLocalizedString("modal.create_room.access_code_placeholder"))
$("#room_access_code").val(null) $("#room_access_code").val(null)
} }

View File

@ -102,7 +102,13 @@ module ApplicationHelper
# Return all the translations available in the client side through javascript # Return all the translations available in the client side through javascript
def current_translations def current_translations
@translations ||= I18n.backend.send(:translations) @translations ||= I18n.backend.send(:translations)
@translations[I18n.locale].with_indifferent_access @translations[I18n.locale]
end
# Return the fallback translations available in the client side through javascript
def fallback_translations
@fallback_translations ||= I18n.backend.send(:translations)
@fallback_translations[I18n.default_locale]
end end
# Returns the page that the logo redirects to when clicked on # Returns the page that the logo redirects to when clicked on

View File

@ -47,6 +47,7 @@
<script type="text/javascript"> <script type="text/javascript">
// Include the correct translated strings for Javascript // Include the correct translated strings for Javascript
window.I18n = <%= current_translations.to_json.html_safe %> window.I18n = <%= current_translations.to_json.html_safe %>
window.I18nFallback = <%= fallback_translations.to_json.html_safe %>
</script> </script>
</head> </head>