Class: LesliBabel::TranslationService

Inherits:
Lesli::ApplicationLesliService
  • Object
show all
Defined in:
app/services/lesli_babel/translation_service.rb

Instance Method Summary collapse

Instance Method Details

#deployObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/services/lesli_babel/translation_service.rb', line 38

def deploy

    LesliSystem.engines.each do |engine, engine_info|

        next if engine == "Root"

        # get all rails engines to buil
        engine_id = Module
        .where("platform in ('lesli_core', 'lesli_engine')")
        .where(:code => engine_info[:code])
        .pluck(:id)

        engine_id_lesli = Module
        .where("platform in ('lesli_core', 'lesli_engine')")
        .where(:code => "lesli")
        .pluck(:id)

        bucket_id_shared = Bucket
        .where(:code => "shared")
        .where(:module_id => engine_id_lesli)
        .pluck(:id)

        bucket_id_application = Bucket
        .where(:code => "application")
        .where(:module_id => engine_id_lesli)
        .pluck(:id)

        # get labels filtered by module (only rails translations)
        labels = LabelService.new(current_user, query).builder(modules_id:engine_id).order(created_at: :asc)
        labels_shared = LabelService.new(current_user, query).builder(modules_id:engine_id_lesli, buckets_id:bucket_id_shared).order(created_at: :asc)
        labels_application = LabelService.new(current_user, query).builder(modules_id:engine_id_lesli, buckets_id:bucket_id_application).order(created_at: :asc)

        translations = {}

        available_locales = Lesli.config.locales.keys

        # add key for every available language (required by i18n Rails gem)
        available_locales.each do |lang|
            translations[lang] = { 
                :file => "",
                :labels => {}
            }
        end

        engine_code = engine_info[:code]

        available_locales.each do |lang|

            # translations path for lesli core
            translations[lang][:file] = "#{engine}::Engine".constantize.root.join(
                "config", "locales", "translations.#{lang}.yml"
            ).to_s

            # Create a collection of labels for the current module
            labels.each do |label|
                
                bucket_code = label[:bucket_code]

                unless translations[lang][:labels].has_key? engine_code
                    translations[lang][:labels][engine_code] = { }
                end

                unless translations[lang][:labels][engine_code].has_key? bucket_code
                    translations[lang][:labels][engine_code][bucket_code] = { }
                end

                # # send debug message for missing translations
                label[lang] = ":" + label.path + ":" if label[lang].blank?

                translations[lang][:labels][engine_code][bucket_code][label.text] = label[lang]

            end

            # Create a collection of labels for the shared labels of Lesli
            # labels_shared.each do |label|

            #     unless translations[lang][:labels].has_key? "lesli"
            #         translations[lang][:labels]["lesli"] = { }
            #     end

            #     unless translations[lang][:labels]["lesli"].has_key? "shared"
            #         translations[lang][:labels]["lesli"]["shared"] = { }
            #     end

            #     # # send debug message for missing translations
            #     label[lang] = ":" + label.path + ":" if label[lang].blank?

            #     translations[lang][:labels]["lesli"]["shared"][label.text] = label[lang]
            # end

            # # Create a collection of labels for the application labels of Lesli
            # labels_application.each do |label|

            #     unless translations[lang][:labels].has_key? "lesli"
            #         translations[lang][:labels]["lesli"] = { }
            #     end

            #     unless translations[lang][:labels]["lesli"].has_key? "application"
            #         translations[lang][:labels]["lesli"]["application"] = { }
            #     end

            #     # # send debug message for missing translations
            #     label[lang] = ":" + label.path + ":" if label[lang].blank?

            #     translations[lang][:labels]["lesli"]["application"][label.text] = label[lang]
            # end

        end

        translations.each do |lang, translations|

            # creates folder and subfolders
            FileUtils.makedirs(File.dirname(translations[:file]))

            # creates translation file for every available language
            translation_file = File.new(translations[:file], "w+")

            translation_file.write({ "#{lang}": translations[:labels] }.to_yaml)

            translation_file.close

            #L2.msg "file added: #{ translations[:file] }"
        end
    end
end

#statsObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/services/lesli_babel/translation_service.rb', line 4

def stats
    Rails.cache.fetch((__method__), expires_in: 1.hour) do

        label_service = LabelService.new(current_user, query)

        # total translations registered in babel
        total_strings = label_service.builder.reselect(:id).count
        
        # total translations by language
        total_strings_translations = []

        Lesli.config.locales.each do |locale|
            total_strings_translations.push({
                code: locale[0],
                name: locale[1],
                total: label_service.builder.reselect(:id).where("#{locale[0]} is not null").where("#{locale[0]} != ''").count
            })
        end

        # total translations that needs help
        total_strings_waiting_for_help = 0#self.list.reselect(:id).where(:status => 1).count

        # total translations that needs translation
        total_strings_waiting_for_translation = 0 #self.list.reselect(:id).where(:status => 2).count

        {
            total_strings: total_strings,
            total_strings_translations: total_strings_translations,
            total_strings_waiting_for_help: total_strings_waiting_for_help,
            total_strings_waiting_for_translation: total_strings_waiting_for_translation
        }
    end
end