-
Notifications
You must be signed in to change notification settings - Fork 3
/
lokaliz.m
216 lines (201 loc) · 7.9 KB
/
lokaliz.m
1
2
3
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
37
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
% lokaliz()
%
% Naudojimas:
% lokalizuotas_tekstas = localiz(originalus_tekstas);
%
% Usage:
% lokalizd_text = lokaliz(original_text);
%
% (C) 2014, 2023, 2024 Mindaugas Baranauskas
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Ši programa yra laisva. Jūs galite ją platinti ir/arba modifikuoti
% remdamiesi Free Software Foundation paskelbtomis GNU Bendrosios
% Viešosios licencijos sąlygomis: 3 licencijos versija, arba (savo
% nuožiūra) bet kuria vėlesne versija.
%
% Ši programa platinama su viltimi, kad ji bus naudinga, bet BE JOKIOS
% GARANTIJOS; taip pat nesuteikiama jokia numanoma garantija dėl TINKAMUMO
% PARDUOTI ar PANAUDOTI TAM TIKRAM TIKSLU. Daugiau informacijos galite
% rasti pačioje GNU Bendrojoje Viešojoje licencijoje.
%
% Jūs kartu su šia programa turėjote gauti ir GNU Bendrosios Viešosios
% licencijos kopiją; jei ne - žr. <https://www.gnu.org/licenses/>.
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
%
%%
function txt=lokaliz(orig_txt)
persistent LC_TXT LC_info LC_current_locale warning_no_translation function_dir;
msg='';
txt='';
function_dir=regexprep(mfilename('fullpath'),[ mfilename '$'], '' );
i=[];
LANG_id_fallback=2;
data_file='lokaliz.mat';
try
enc=feature('DefaultCharacterSet');
catch
enc=get(0,'language');
end;
% create example of locale_txt.mat :
% LC_info=struct('LANG', {'--' 'en' 'lt'}, 'COUNTRY', {'' 'US' 'LT'}, 'VARIANT', {'' '' ''});
% LC_TXT={ ...
% 'OK_button' 'OK' 'Gerai' ; ...
% 'Cancel_button' 'Cancel' 'Atšaukti' ; ...
% 'Close_button' 'Close' 'Užverti' ; ...
% 'Copy_button' 'Copy' 'Kopijuoti'; ...
% } ;
% save('locale_txt.mat', LC_info, LC_TXT ) ;
try
if isempty(LC_current_locale);
if usejava('awt');
LC=javaObject ('java.util.Locale','');
LC_current_locale=LC.getDefault();
else
try load('Darbeliai_config.mat','-mat');
lnt=[{'getLanguage' 'getCountry' 'getVariant'}; Darbeliai.nuostatos.lokale' ];
LC_current_locale=struct(lnt{:});
catch
LC_current_locale=struct('getLanguage','--','getCountry','','getVariant','');
end;
end;
end;
if or(isempty(LC_TXT),isempty(LC_info)) ;
%disp('load locale');
try
load(fullfile(function_dir,data_file));
catch err;
LC_info=struct('LANG', {'--'}, 'COUNTRY', {''}, 'VARIANT', {''});
LC_TXT={''};
end;
end ;
LANG_id=find(ismember({LC_info.LANG},char(LC_current_locale.getLanguage())));
%disp(LANG_id);
if length(LANG_id) > 1;
LANG_id=intersect(LANG_id,find(ismember({LC_info.COUNTRY},char(LC_current_locale.getCountry()))));
end;
if length(LANG_id) > 1;
LANG_id=intersect(LANG_id,find(ismember({LC_info.VARIANT},char(LC_current_locale.getVariant()))));
end;
if isempty(LANG_id);
LANG_id=LANG_id_fallback;
if isempty(warning_no_translation);
warning_no_translation=1;
n=num2str(1+length(LC_info));
msg=sprintf( [ '\nCurrently there is no translation for %s_%s locale.\n' ...
'Your can lokaliz it youself! \n' ...
'1) import localization file by executing:' ...
'\n load('' %s%s '');\n' ...
'2) create info about new language by executing: ' ...
'\n LC_info(' n ').LANG=''%s'';' ...
'\n LC_info(' n ').COUNTRY=''%s'';' ...
'\n LC_info(' n ').VARIANT=''%s'';\n' ...
'3) edit ' n ' column in LC_TXT variable;\n' ...
'4) write your localization to disk by executing:' ...
'\n save(''%s%s'',... \n ''LC_info'', ''LC_TXT'') ;\n' ...
'5) clear catch by executing:' ...
'\n clear(''lokaliz'');\n' ] , ...
char(LC_current_locale.getLanguage()), ...
char(LC_current_locale.getCountry()), ...
function_dir, data_file,...
char(LC_current_locale.getLanguage()), ...
char(LC_current_locale.getCountry()), ...
char(LC_current_locale.getVariant()), ...
function_dir, data_file);
%error(msg);
end;
end;
i=find(ismember(LC_TXT(:,1),orig_txt));
if length(i) > 1 ;
i=i(1);
end;
%if isempty(i) ;
%load(data_file);
%i=find(ismember(LC_TXT(:,1),orig_txt),1);
if isempty(i) ;
n=num2str(1+size(LC_TXT,1));
l=cellfun(@(x) ([LC_info(x).LANG '_' LC_info(x).COUNTRY]), num2cell(2:length(LC_info)), 'UniformOutput',false) ;
k=[ num2cell(2:size(LC_TXT,2)) ; l ];
if isempty(k);
k={2 ; [char(LC_current_locale.getLanguage()) '_' char(LC_current_locale.getCountry())] };
end;
msg=sprintf([ '%s\n' ...
'String ''%s'' is not included for translation.\n' ...
'Your can improve localization! \n' ...
'1) import localization file by executing:' ...
'\n load(''%s%s'');\n' ...
'2) add keyword to 1st column of LC_TXT variable by executing:' ...
'\n LC_TXT{' n ',1}=''%s'';\n' ...
'3) add translations for ' ...
sprintf('%s, ', l{:}) 'by executing:\n' ...
sprintf([' LC_TXT{' n ',%d}=''translation to %s'';\n' ], k{:}) ...
'4) write your localization to disk by executing:' ...
'\n save(''%s%s'',... \n ''LC_info'', ''LC_TXT'') ;\n' ...
'5) clear catch by executing:' ...
'\n clear(''lokaliz'');\n' ], ...
msg, orig_txt, ...
function_dir, data_file, orig_txt, ...
function_dir, data_file);
end;
%end;
%disp(i);
txt=[LC_TXT{i,LANG_id}];
if isempty(txt);
if and(~isempty(i),~isempty(LANG_id));
msg=sprintf([ '\n' ...
'String ''%s'' is not translated to ' char(LC_current_locale) '.\n' ...
'Your can improve localization! \n' ...
'1) import localization file by executing:' ...
'\n load(''%s%s'');\n' ...
'2) add translation for ' char(LC_current_locale) ' by executing:\n' ...
sprintf([' LC_TXT{%d,%d}=''translation to %s'';\n' ], i, LANG_id, char(LC_current_locale)) ...
'3) write your localization to disk by executing:' ...
'\n save(''%s%s'',... \n ''LC_info'', ''LC_TXT'') ;\n' ...
'4) clear catch by executing:' ...
'\n clear(''lokaliz'');\n' ], ...
orig_txt, ...
function_dir, data_file, ...
function_dir, data_file);
warning(msg);
msg='';
end;
txt=[LC_TXT{i,LANG_id_fallback}];
end;
if ~strcmp(enc,'UTF-8');
bytes=unicode2native(txt,enc);
%disp(bytes);
txt=native2unicode(bytes,enc);
end;
if ~isempty(msg);
% if ~strcmp(enc,'UTF-8');
% msg=sprintf([ msg '\n' ...
% 'NOTE: all data in ' function_dir data_file ' should be in unicode (UTF-8) encoding!\n' ...
% 'Try switch to unicode by executing\n' ...
% ' feature(''DefaultCharacterSet'',''UTF-8'');\n' ...
% 'Return to current encoding by executing:\n' ...
% ' feature(''DefaultCharacterSet'',''' enc ''');\n' ]);
% end;
msg=sprintf(['\n' msg ]);
error(msg);
end;
catch err ;
warning(err.message);
if isempty(txt);
txt=orig_txt;
end;
end;