Improved i18n and sync with new translations.

This commit is contained in:
Martin Edenhofer 2015-04-27 13:47:48 +02:00
parent ed891818ed
commit 46557532aa
5 changed files with 99 additions and 73 deletions

View file

@ -80,7 +80,10 @@ class TranslationToDo extends App.Controller
# local update
App.i18n.removeNotTranslated( @locale, source )
App.i18n.setMap( source, target )
# update runtime if same language is used
if App.i18n.get() is @locale
App.i18n.setMap( source, target, 'string' )
# remote update
params =
@ -109,7 +112,10 @@ class TranslationToDo extends App.Controller
# local update
App.i18n.removeNotTranslated( @locale, source )
App.i18n.setMap( source, source )
# update runtime if same language is used
if App.i18n.get() is @locale
App.i18n.setMap( source, source, 'string' )
# remote update
params =
@ -157,10 +163,17 @@ class TranslationList extends App.Controller
#if !App.i18n.notTranslatedFeatureEnabled(@locale)
# return
@strings = []
@times = []
for item in data.list
if item[4] is 'time'
@times.push item
else
@strings.push item
@html App.view('translation/list')(
list: data.list
timestampFormat: data.timestampFormat
dateFormat: data.dateFormat
times: @times
strings: @strings
)
ui = @
@$('.js-Item').each( (e) ->
@ -174,11 +187,19 @@ class TranslationList extends App.Controller
id = field.data('id')
source = field.data('source')
initial = field.data('initial')
format = field.data('format')
# if it's translated by user it self, delete it
if !initial || initial is ''
# locale reset
$(e.target).closest('tr').remove()
App.i18n.setMap( source, '' )
# update runtime if same language is used
if App.i18n.get() is @locale
App.i18n.setMap( source, '', format )
# remote reset
params =
id: id
@ajax(
@ -188,17 +209,21 @@ class TranslationList extends App.Controller
data: JSON.stringify(params)
processData: false
success: =>
console.log('aaa', @locale, source)
App.i18n.setNotTranslated( @locale, source )
App.Event.trigger('i18n:translation_todo_reload')
)
return
# update item
App.i18n.setMap( source, initial )
# update runtime if same language is used
if App.i18n.get() is @locale
App.i18n.setMap( source, initial, format )
# locale reset
field.val( initial )
@updateRow(id)
# remote reset
params =
id: id
target: initial
@ -215,11 +240,15 @@ class TranslationList extends App.Controller
e.preventDefault()
id = $( e.target ).data('id')
source = $( e.target ).data('source')
format = $( e.target ).data('format')
target = $( e.target ).val()
@updateRow(id)
# local update
App.i18n.setMap( source, target )
@updateRow(id)
# update runtime if same language is used
if App.i18n.get() is @locale
App.i18n.setMap( source, target, format )
# remote update
params =

View file

@ -39,10 +39,10 @@ class App.i18n
_instance ?= new _i18nSingleton()
_instance.set( args )
@setMap: (source, target) ->
@setMap: (source, target, format) ->
if _instance == undefined
_instance ?= new _i18nSingleton()
_instance.setMap( source, target )
_instance.setMap( source, target, format )
@notTranslatedFeatureEnabled: (locale) ->
if _instance == undefined
@ -68,7 +68,8 @@ class _i18nSingleton extends Spine.Module
@include App.LogInclude
constructor: ( locale ) ->
@map = {}
@mapTime = {}
@mapString = {}
@_notTranslatedLog = false
@_notTranslated = {}
@dateFormat = 'yyyy-mm-dd'
@ -98,13 +99,13 @@ class _i18nSingleton extends Spine.Module
@log 'debug', 'translate Update', translation_new, $this.data, 'before'
$this.data 'before', translation_new
# update runtime translation map
@map[ source ] = translation_new
# update runtime translation mapString
@mapString[ source ] = translation_new
# replace rest in page
$(".translation[data-text='#{source}']").html( translation_new )
# update permanent translation map
# update permanent translation mapString
translation = App.Translation.findByAttribute( 'source', source )
if translation
translation.updateAttribute( 'target', translation_new )
@ -133,7 +134,7 @@ class _i18nSingleton extends Spine.Module
# set lang attribute of html tag
$('html').prop( 'lang', locale.substr(0, 2) )
@map = {}
@mapString = {}
App.Ajax.request(
id: 'i18n-set-' + locale,
type: 'GET',
@ -141,22 +142,20 @@ class _i18nSingleton extends Spine.Module
async: false,
success: (data, status, xhr) =>
# set timestamp format
if data.timestampFormat
@timestampFormat = data.timestampFormat
# set date format
if data.dateFormat
@dateFormat = data.dateFormat
# load translation collection
for object in data.list
# set runtime lookup table
@map[ object[1] ] = object[2]
# set date/timestamp format
if object[3] is 'time'
@mapTime[ object[1] ] = object[2]
# load in collection if needed
App.Translation.refresh( { id: object[0], source: object[1], target: object[2], locale: @locale } )
else
# set runtime lookup table
@mapString[ object[1] ] = object[2]
# load in collection if needed
App.Translation.refresh( { id: object[0], source: object[1], target: object[2], locale: @locale } )
)
translateInline: ( string, args... ) =>
@ -188,9 +187,9 @@ class _i18nSingleton extends Spine.Module
return '' if string is ''
# return translation
if @map[string] isnt undefined
if @mapString[string] isnt undefined
@_translated = true
translated = @map[string]
translated = @mapString[string]
else
@_translated = false
translated = string
@ -209,8 +208,11 @@ class _i18nSingleton extends Spine.Module
# return translated string
return translated
setMap: ( source, target ) =>
@map[source] = target
setMap: ( source, target, format = 'string' ) =>
if format is 'time'
@mapTime[source] = target
else
@mapString[source] = target
notTranslatedFeatureEnabled: (locale) =>
if locale.substr(0,2) is 'en'
@ -227,10 +229,10 @@ class _i18nSingleton extends Spine.Module
@_notTranslated[locale][key] = true
date: ( time, offset ) =>
@convert(time, offset, @dateFormat)
@convert(time, offset, @mapTime['date'] || @dateFormat)
timestamp: ( time, offset ) =>
@convert(time, offset, @timestampFormat)
@convert(time, offset, @mapTime['timestamp'] || @timestampFormat)
convert: ( time, offset, format ) =>
s = ( num, digits ) ->

View file

@ -1,5 +1,5 @@
<h2><%- @T('Date & Datetime') %></h2>
<table class="translationOverview table table-striped table-hover">
<table class="translationOverview js-translated table table-striped table-hover">
<thead>
<tr>
<th class="translationOverview-source"><%- @T('Type') %></th>
@ -9,18 +9,16 @@
</tr>
</thead>
<tbody>
<tr>
<td><%- @T('Datetime') %></td>
<td class="translationOverview-itemContainer"><input class="translationOverview-item form-control" value="<%= @timestampFormat %>"></td>
<td></td>
<td></td>
</tr>
<tr>
<td><%- @T('Date') %></td>
<td class="translationOverview-itemContainer"><input class="translationOverview-item form-control" value="<%= @dateFormat %>"></td>
<td></td>
<td></td>
</tr>
<% if @times: %>
<% for time in @times: %>
<tr>
<td title="<%= time[1] %>"><%= time[1] %></td>
<td class="translationOverview-itemContainer"><input class="js-Item translationOverview-item form-control" value="<%= time[2] %>" data-source="<%= time[1] %>" data-initial="<%= time[3] %>" data-id="<%= time[0] %>" data-format="<%= time[4] %>"></td>
<td title="<%= time[3] %>"><%= time[3]%></td>
<td><a href="#" class="js-Reset"><%- @T('Reset') %></a></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
@ -35,11 +33,11 @@
</tr>
</thead>
<tbody>
<% if @list: %>
<% for item in @list: %>
<% if @strings: %>
<% for item in @strings: %>
<tr>
<td title="<%= item[1] %>"><%= item[1] %></td>
<td class="translationOverview-itemContainer"><input class="js-Item translationOverview-item form-control" value="<%= item[2] %>" data-source="<%= item[1] %>" data-initial="<%= item[3] %>" data-id="<%= item[0] %>"></td>
<td class="translationOverview-itemContainer"><input class="js-Item translationOverview-item form-control" value="<%= item[2] %>" data-source="<%= item[1] %>" data-initial="<%= item[3] %>" data-id="<%= item[0] %>" data-format="<%= item[4] %>"></td>
<td title="<%= item[3] %>"><%= item[3]%></td>
<td><a href="#" class="js-Reset"><%- @T('Reset') %></a></td>
</tr>

View file

@ -3,7 +3,7 @@
class Locale < ApplicationModel
def self.load
url = 'http://localhost:3001/api/v1/locales'
url = 'https://i18n.zammad.com/api/v1/locales'
result = UserAgent.get(
url,

View file

@ -28,12 +28,20 @@ load translations from online
)
result.data.each {|translation|
#puts translation.inspect
exists = Translation.where(:locale => translation['locale'], :source => translation['source']).first
if exists
# handle case insensitive sql
exists = Translation.where(:locale => translation['locale'], :format => translation['format'], :source => translation['source'])
translaten = nil
exists.each {|item|
if item.source == translation['source']
translaten = item
end
}
if translaten
# verify if update is needed
exists.update_attributes(translation.symbolize_keys!)
exists.save
translaten.update_attributes(translation.symbolize_keys!)
translaten.save
else
Translation.create(translation.symbolize_keys!)
end
@ -51,6 +59,7 @@ push translations to online
def self.push(locale)
# only push changed translations
translations = Translation.where(:locale => locale)
translations_to_push = []
translations.each {|translation|
@ -61,7 +70,7 @@ push translations to online
return true if translations_to_push.empty?
#return translations_to_push
url = 'http://localhost:3001/api/v1/thanks_for_your_support'
url = 'https://i18n.zammad.com/api/v1/thanks_for_your_support'
result = UserAgent.post(
url,
@ -103,6 +112,7 @@ get list of translations
item.source,
item.target,
item.target_initial,
item.format,
]
list.push data
else
@ -110,6 +120,7 @@ get list of translations
item.id,
item.source,
item.target,
item.format,
]
list.push data
end
@ -121,22 +132,8 @@ get list of translations
end
end
timestamp_map_default = 'yyyy-mm-dd HH:MM'
timestamp_map = {
:de => 'dd.mm.yyyy HH:MM',
}
timestamp = timestamp_map[ locale.to_sym ] || timestamp_map_default
date_map_default = 'yyyy-mm-dd'
date_map = {
:de => 'dd.mm.yyyy',
}
date = date_map[ locale.to_sym ] || date_map_default
return {
:list => list,
:timestampFormat => timestamp,
:dateFormat => date,
:list => list,
}
end