language_management.anubis 13.3 KB
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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
/*
 * Created by PyramIDE.
 * User: フランスのトトロ aka (David RENÉ) 
 * Date: 17/04/2020
 * Time: 15:00
 * © David RENÉ
 */

read system/logger.anubis
read locale/L3.anubis
read locale/L3LanguageInfo.anubis
read xlib/web/types/controllers_web_site.anubis
read xlib/web/web_session.anubis

public define One     set_dictionary(WEB_Session s, String n)                     = replace_String(s.fields, "AWS_CURRENT_DICTIONARY", n).
public define One     set_dictionary(Var(List(Session_Field)) fields, String n)   = replace_String(fields, "AWS_CURRENT_DICTIONARY", n).
public define String  get_dictionary(WEB_Session s)                               = get_String    (s.fields, "AWS_CURRENT_DICTIONARY", "").

public define String default_language = "en".

public define (String symbolic_name) -> String
  make_translate_function
  (
    String                      dictionary_collection_name,
    String                      lang,
    (LogLevel, String) -> One   logger
  ) =
  if l3_load_base(dictionary_collection_name) is
  {
    failure           then   
      logger(logError, "Can't load translation dictionary for '"+dictionary_collection_name+"'");
      (String symbolic_name) |-> "<" + symbolic_name + ">",

    success(L3 local) then 
      //now we set the current language
      with _l3_object = l3_set_current(local, lang),
            l3_object = l3_set_default(_l3_object, default_language),
      if lang = "symb" then
        (String symbolic_name) |-> "[" + l3_get_text(l3_object, symbolic_name) + "]"
      else
        (String symbolic_name) |-> l3_get_text(l3_object, symbolic_name)
  }
.

public define (String symbolic_name) -> String
  make_translate_function_for_lang
  (
    WEB_Session                 _session,
    String                      lang,
    (LogLevel, String) -> One   logger
  ) =
  with app_dictionary = get_String(_session.fields, "AWS_CURRENT_DICTIONARY", ""), 
  make_translate_function(app_dictionary, lang, logger)
.

public define (String symbolic_name) -> String
  make_translate_function_for_lang
  (
    WEB_Session                 _session,
    String                      lang
  ) =
  with app_dictionary = get_String(_session.fields, "AWS_CURRENT_DICTIONARY", ""), 
  make_translate_function(app_dictionary, lang, (LogLevel level, String txt) |-> println(txt))
.


public define (String symbolic_name) -> String
  make_translate_function
  (
    WEB_Session                 _session,
    (LogLevel, String) -> One   logger
  ) =
  with        lang = _session.language,
   app_dictionary = get_String(_session.fields, "AWS_CURRENT_DICTIONARY", ""),

  make_translate_function(app_dictionary, lang, logger)
.

public define (String symbolic_name) -> String
  make_translate_function
  (
    WEB_Session                 _session
  ) =
  make_translate_function(_session, (LogLevel level, String txt) |-> println(txt))
.

public define (String symbolic_name) -> String
  make_translate_function_for_dictionary
  (
    WEB_Session                 _session,
    String                      dictionary_collection_name,
    (LogLevel, String) -> One   logger
  ) =
  with        lang = _session.language,
  make_translate_function(dictionary_collection_name, lang, logger)
.

public define (String symbolic_name) -> String
  make_translate_function_for_dictionary
  (
    WEB_Session                 _session,
    String                      dictionary_collection_name,
  ) =
  with        lang = _session.language,
  make_translate_function(dictionary_collection_name, lang, (LogLevel level, String txt) |-> println(txt))
.

public define (String symbolic_name, List((String, String)) list_key_value) -> String
  make_translate_with_key_value_function
  (
    WEB_Session                 _session
  ) =
  (String symbolic_name, List((String, String)) list_key_value) |->
  with translated_sentence = make_translate_function(_session, (LogLevel level, String txt) |-> println(txt))(symbolic_name),
  find_and_replace_dict(translated_sentence, list_key_value)
.
 
public define (String symbolic_name, List((String, String)) list_key_value) -> String
  make_translate_with_key_value_function_for_lang
  (
    WEB_Session                 _session,
    String                      lang,
  ) =
  (String symbolic_name, List((String, String)) list_key_value) |->
  with translated_sentence = make_translate_function_for_lang(_session, lang)(symbolic_name),
  find_and_replace_dict(translated_sentence, list_key_value)
.

public define (String symbolic_name, List((String, String)) list_key_value) -> String
  make_translate_with_key_value_function_for_dictionary
  (
    WEB_Session                 _session,
    String                      dictionary_collection_name,
  ) =
  (String symbolic_name, List((String, String)) list_key_value) |->
  with translated_sentence = make_translate_function_for_dictionary(_session, dictionary_collection_name)(symbolic_name),
  find_and_replace_dict(translated_sentence, list_key_value)
.

public define List((List(CoreAttrs),WebArgValue, String))
  language_list_for_combo
  (
    String dictionary_collection_name   //means application name
  )
  =
  map((L3LangDicProperty dico_prop)
      |->
      since dico_prop is l3LangDicProperty( _, _, lang_code, _, _, _),
      with lang_info = l3_get_language_info(lang_code),
      ((List(CoreAttrs))[], wav(lang_code), self_name(lang_info))
      , 
      l3_get_all_dicos(dictionary_collection_name)
  )
.

define HTML_Off_Form
  language_line
  (
    String language_code,
    String language
  )=
  li([style("background-image: url(/xlib/flags/lang/24/"+language_code+".png);"), 
      event(onclick, "language_changed('"+language_code+"');")], text(language))
.

define HTML_Off_Form
  language_line
  (
    String language_code,
    String language,
    String -> String action
  )=
  div(id(generate_random_string(15)+"_"+language_code), [
    img([class("clickable"), event(onclick, action(language_code))],"/xlib/flags/lang/24/"+language_code+".png"),
    text([class("clickable"), event(onclick, action(language_code))],language)
  ])
.

public define HTML_Partial_Content 
  flags_menu
  (
    String                      dictionary_collection_name,
    String                      lang,
    Bool                        show_symbolic
  ) =
  with make_line = (L3LangDicProperty dico_prop) |-> 
                    if dico_prop is l3LangDicProperty( _, sha1, lang_code, user_def, creat, update) then
                    with lang_info = l3_get_language_info(lang_code),
                    if lang_code = lang then
                      empty
                    else
                      language_line(lang_code, self_name(lang_info)),
  partial_content([
      css(css_file("/xlib/css/flags_menu.css")),
      js(js_file("/xlib/js/xlib.js"))
    ],
    [
      dl([
      //literal("<dt style=\"background-image: url(../flags/"+lang+".png);\" onmouseover=\"javascript:montre('drapo1');\" onmouseout=\"javascript:montre();\">&nbsp;</dt>\n"),
      //literal("<img src=\"/xlib/flags/24x24/"+lang+".png\" onmouseover=\"javascript:montre('drapo1');\" onmouseout=\"javascript:montre();\">"),

      img([event(onmouseover, "showObject('l3-flag');"), event(onmouseout, "hideObject('l3-flag');")], "/xlib/flags/lang/24/"+lang+".png" ),
      dd([style("display: none"),id("l3-flag"), event(onmouseover, "showObject('l3-flag');"), event(onmouseout, "hideObject('l3-flag');")],
      ul(
        map(make_line, l3_get_all_dicos(dictionary_collection_name))+
        [
          if show_symbolic then
            language_line("symb", "symbolic")
          else
            empty
        ]
      )),
      ])  //!dl
    ]
  )
.

public define HTML_Partial_Content 
  flags_menu
  (
    WEB_Session                 _session,
    Bool                        show_symbolic,
    (LogLevel, String) -> One   logger
  ) =
  with        lang = _session.language,
   app_dictionnary = get_dictionary(_session),
   
  flags_menu(app_dictionnary, lang, show_symbolic)
.

 
public define HTML_Partial_Content
  choose_lang_dialog
  (
    String            dictionary_collection_name,
    String -> String  action,
    Bool              show_symbolic,
    List(String)      exclude_lang
  )=
    with make_line = (L3LangDicProperty dico_prop) |-> 
                    if dico_prop is l3LangDicProperty( _, sha1, lang_code, user_def, creat, update) then
                    with lang_info = l3_get_language_info(lang_code),
                    //if language belong to exclude list, return an empty line
                    if lang_code:exclude_lang then
                      empty
                    else
                      language_line(lang_code, self_name(lang_info), action),
                      
   partial_content([
      css(css_file("/xlib/css/flags_menu.css")),
      js(js_file("/xlib/js/xlib.js"))
    ],
    [
      dl([
      //img([event(onmouseover, "showObject('l3-flag');"), event(onmouseout, "hideObject('l3-flag');")], "/xlib/flags/lang/24/"+lang+".png" ),
      dd([style("display: block"),id("l3-flag")],
      ul(
        map(make_line, l3_get_all_dicos(dictionary_collection_name))+
        [
          if show_symbolic then
            language_line("symb", "symbolic", action)
          else
            empty
        ]
      )),
      ])  //!dl
    ]
  )
.

public define HTML_Partial_Content 
  choose_lang_dialog
  (
    WEB_Session       _session,
    String -> String  action,
    Bool              show_symbolic
  ) =
  with        lang = _session.language,
   app_dictionnary = get_dictionary(_session),
   
  choose_lang_dialog(app_dictionnary, action, show_symbolic, [])
.

public define HTML_Partial_Content 
  choose_lang_dialog
  (
    WEB_Session       _session,
    String -> String  action,
    List(String)      exclude_lang
  ) =
  with        lang = _session.language,
   app_dictionnary = get_dictionary(_session),
   
  choose_lang_dialog(app_dictionnary, action, false, exclude_lang)
.

public define HTML_Partial_Content 
  choose_lang_dialog
  (
    WEB_Session       _session,
    String -> String  action
  ) =
  with        lang = _session.language,
   app_dictionnary = get_dictionary(_session),
   
  choose_lang_dialog(app_dictionnary, action, false, [])
.

 /* DROPDOWN part */
 
define HTML_Off_Form
  dd_language_line
  (
    String language_code,
    String language,
    String -> String action
  )=
  li(event(onclick, action(language_code)), [
    img("/xlib/flags/lang/24/"+language_code+".png"),
    text(language)
  ])
.

public define HTML_Partial_Content
  choose_lang_dropdown
  (
    String            dictionary_collection_name,
    String -> String  action,
    String            image,   //Web element on which is sticked the dropdown
    Bool              on_click,
    Bool              show_symbolic,
    List(String)      exclude_lang
  )=
    with rand_id   = generate_random_string(15)+"_flag",
    with make_line = (L3LangDicProperty dico_prop) |-> 
                    if dico_prop is l3LangDicProperty( _, sha1, lang_code, user_def, creat, update) then
                    with lang_info = l3_get_language_info(lang_code),
                    //if language belong to exclude list, return an empty line
                    if lang_code:exclude_lang then
                      empty
                    else
                      dd_language_line(lang_code, self_name(lang_info), action),
                      
   partial_content([
      css(css_file("/xlib/css/flags_menu.css")),
      js(js_file("/xlib/js/xlib.js"))
    ],
    [
      img([class("icon_in_text clickable"), event(if on_click then onclick else onmouseover, "showObject('"+rand_id+"');"), event(onmouseout, "hideObject('"+rand_id+"');")], image ),
      ul([id(rand_id), class("drop-flag-body"), event(onmouseover, "showObject('"+rand_id+"');"), event(onmouseout, "hideObject('"+rand_id+"');"), style("display: none"), ],
        map(make_line, l3_get_all_dicos(dictionary_collection_name))+
        [
          if show_symbolic then
            dd_language_line("symb", "symbolic", action)
          else
            empty
        ]
      )
    ]
  )
.

public define HTML_Partial_Content 
  choose_lang_dropdown
  (
    WEB_Session           _session,
    String -> String      action,
    String  image,   //Web element on which is sticked the dropdown
    Bool                  on_click,
    Bool                  show_symbolic
  ) =
  with        lang = _session.language,
   app_dictionnary = get_dictionary(_session),
   
  choose_lang_dropdown(app_dictionnary, action, image, on_click, show_symbolic, [])
.

public define HTML_Partial_Content 
  choose_lang_dropdown
  (
    WEB_Session           _session,
    String -> String      action,
    String  image,      //Web element on which is sticked the dropdown
    Bool                  on_click,
    List(String)          exclude_lang
  ) =
  with        lang = _session.language,
   app_dictionnary = get_dictionary(_session),
   
  choose_lang_dropdown(app_dictionnary, action, image, on_click, false, exclude_lang)
.

public define HTML_Partial_Content 
  choose_lang_dropdown
  (
    WEB_Session       _session, //
    String -> String  action,   //web action function to call when language is choosen
    String            image,    //Web element on which is sticked the dropdown
    Bool              on_click  //true if the dropdown shown on click event else it's on mouseover

  ) =
  with        lang = _session.language,
   app_dictionnary = get_dictionary(_session),
   
  choose_lang_dropdown(app_dictionnary, action, image, on_click, false, [])
.