Class: I18nema::Backend
- Inherits:
-
Object
- Object
- I18nema::Backend
- Includes:
- I18n::Backend::Base, CoreMethods
- Defined in:
- lib/i18nema.rb,
ext/i18nema/i18nema.c
Defined Under Namespace
Classes: LoadError
Constant Summary
Constants included from CoreMethods
Instance Method Summary collapse
-
#available_locales ⇒ Object
Returns the currently loaded locales.
-
#direct_lookup([part]) ⇒ Object
Returns the translation(s) found under the specified key.
- #init_translations ⇒ Object
- #initialize ⇒ Object constructor
-
#load_yaml_string(yaml_str) ⇒ Object
Loads translations from the specified yaml string, and returns the number of (new) translations stored.
-
#normalize_key(key, separator) ⇒ Object
Normalizes and splits a key based on the separator.
-
#reload! ⇒ true
Clears out all currently stored translations.
- #store_translations(locale, data, options = {}) ⇒ Object
Methods included from CoreMethods
Constructor Details
#initialize ⇒ Object
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 |
# File 'ext/i18nema/i18nema.c', line 563
static VALUE
initialize(VALUE self)
{
VALUE translations, key_cache;
i_object_t *root_object = new_hash_object();
translations = Data_Wrap_Struct(I18nemaBackend, 0, delete_object_r, root_object);
rb_iv_set(self, "@translations", translations);
i_object_t *key_map = new_hash_object();
key_cache = Data_Wrap_Struct(I18nemaBackend, 0, delete_object_r, key_map);
rb_iv_set(self, "@normalized_key_cache", key_cache);
return self;
}
|
Instance Method Details
#available_locales ⇒ Object
Returns the currently loaded locales. Order is not guaranteed.
backend.available_locales #=> [:en, :es]
459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
# File 'ext/i18nema/i18nema.c', line 459
static VALUE
available_locales(VALUE self)
{
if (!RTEST(rb_iv_get(self, "@initialized")))
rb_funcall(self, s_init_translations, 0);
i_object_t *root_object = translations_get(self);
i_key_value_t *current = root_object->data.hash;
VALUE ary = rb_ary_new2(0);
for (; current != NULL; current = current->hh.next)
rb_ary_push(ary, rb_str_intern(rb_str_new2(current->key)));
return ary;
}
|
#direct_lookup([part]) ⇒ Object
Returns the translation(s) found under the specified key.
backend.direct_lookup("en", "foo", "bar") #=> "lol"
backend.direct_lookup("en", "foo") #=> {"bar"=>"lol", "baz"=>["asdf", "qwerty"]}
155 156 157 158 159 160 |
# File 'ext/i18nema/i18nema.c', line 155
static VALUE
direct_lookup(int argc, VALUE *argv, VALUE self)
{
i_object_t *translations = translations_get(self);
return i_object_to_robject(hash_get(translations, argv, argc));
}
|
#init_translations ⇒ Object
44 45 46 47 |
# File 'lib/i18nema.rb', line 44 def init_translations load_translations @initialized = true end |
#load_yaml_string(yaml_str) ⇒ Object
Loads translations from the specified yaml string, and returns the number of (new) translations stored.
backend.load_yaml_string("en:\n foo: bar") #=> 1
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 |
# File 'ext/i18nema/i18nema.c', line 422
static VALUE
load_yml_string(VALUE self, VALUE yml)
{
SYMID oid;
i_object_t *root_object = translations_get(self);
i_object_t *new_root_object = NULL;
current_translation_count = 0;
SyckParser* parser = syck_new_parser();
syck_parser_handler(parser, handle_syck_node);
StringValue(yml);
syck_parser_str(parser, RSTRING_PTR(yml), RSTRING_LEN(yml), NULL);
syck_parser_bad_anchor_handler(parser, handle_syck_badanchor);
syck_parser_error_handler(parser, handle_syck_error);
oid = syck_parse(parser);
syck_lookup_sym(parser, oid, (void **)&new_root_object);
if (parser->syms)
st_foreach(parser->syms, delete_syck_st_entry, 0);
syck_free_parser(parser);
if (new_root_object == NULL || new_root_object->type != i_type_hash) {
delete_object_r(new_root_object);
rb_raise(I18nemaBackendLoadError, "root yml node is not a hash");
}
merge_hash(root_object, new_root_object);
return INT2NUM(current_translation_count);
}
|
#normalize_key(key, separator) ⇒ Object
Normalizes and splits a key based on the separator.
backend.normalize_key "asdf", "." #=> ["asdf"]
backend.normalize_key "a.b.c", "." #=> ["a", "b", "c"]
backend.normalize_key "a.b.c", ":" #=> ["a.b.c"]
backend.normalize_key %{a b.c}, "." #=> ["a", "b", "c"]
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 |
# File 'ext/i18nema/i18nema.c', line 519
static VALUE
normalize_key(VALUE self, VALUE key, VALUE separator)
{
Check_Type(separator, T_STRING);
i_object_t *key_map = normalized_key_cache_get(self),
*sub_map = hash_get(key_map, &separator, 1);
if (sub_map == NULL) {
sub_map = new_hash_object();
char *key = new_string(RSTRING_PTR(separator), RSTRING_LEN(separator));
i_key_value_t *kv = new_key_value(key, sub_map);
add_key_value(&key_map->data.hash, kv);
}
if (TYPE(key) == T_ARRAY)
key = join_array_key(self, key, separator);
else if (TYPE(key) != T_STRING)
key = rb_funcall(key, s_to_s, 0);
i_object_t *key_frd = hash_get(sub_map, &key, 1);
if (key_frd == NULL) {
char *sep = StringValueCStr(separator);
VALUE parts = rb_str_split(key, sep);
long parts_len = RARRAY_LEN(parts),
skipped = 0;
key_frd = new_array_object(parts_len);
for (long i = 0; i < parts_len; i++) {
VALUE part = RARRAY_PTR(parts)[i];
// TODO: don't alloc for empty strings, since we discard them
if (RSTRING_LEN(part) == 0)
skipped++;
else
set_string_object(&key_frd->data.array[i - skipped], RSTRING_PTR(part), RSTRING_LEN(part));
}
key_frd->size -= skipped;
char *key_orig = new_string(RSTRING_PTR(key), RSTRING_LEN(key));
i_key_value_t *kv = new_key_value(key_orig, key_frd);
add_key_value(&sub_map->data.hash, kv);
}
return i_object_to_robject(key_frd);
}
|
#reload! ⇒ true
Clears out all currently stored translations.
backend.reload! #=> true
483 484 485 486 487 488 489 490 |
# File 'ext/i18nema/i18nema.c', line 483
static VALUE
reload(VALUE self)
{
i_object_t *root_object = translations_get(self);
empty_object(root_object, 1);
rb_iv_set(self, "@initialized", Qfalse);
return Qtrue;
}
|
#store_translations(locale, data, options = {}) ⇒ Object
38 39 40 41 42 |
# File 'lib/i18nema.rb', line 38 def store_translations(locale, data, = {}) # TODO: make this moar awesome @initialized = true load_yml_string({locale => data}.deep_stringify_keys.to_yaml) end |