Class: I18nema::Backend
- Inherits:
-
Object
- Object
- I18nema::Backend
- Includes:
- I18n::Backend::Base
- Defined in:
- lib/i18nema.rb,
ext/i18nema/i18nema.c
Defined Under Namespace
Classes: LoadError
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.
-
#reload! ⇒ true
Clears out all currently stored translations.
- #store_translations(locale, data, options = {}) ⇒ Object
Constructor Details
#initialize ⇒ Object
441 442 443 444 445 446 447 448 449 450 451 |
# File 'ext/i18nema/i18nema.c', line 441
static VALUE
initialize(VALUE self)
{
VALUE translations;
i_object_t *root_object = ALLOC(i_object_t);
root_object->type = i_type_hash;
root_object->data.hash = NULL;
translations = Data_Wrap_Struct(I18nemaBackend, 0, delete_object_r, root_object);
rb_iv_set(self, "@translations", translations);
return self;
}
|
Instance Method Details
#available_locales ⇒ Object
Returns the currently loaded locales. Order is not guaranteed.
backend.available_locales #=> [:en, :es]
407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'ext/i18nema/i18nema.c', line 407
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 = root_object_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"]}
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'ext/i18nema/i18nema.c', line 127
static VALUE
direct_lookup(int argc, VALUE *argv, VALUE self)
{
i_object_t *result = root_object_get(self);;
i_key_value_t *kv = NULL;
VALUE rs;
char *s;
for (int i = 0; i < argc && result != NULL && result->type == i_type_hash; i++) {
rs = rb_funcall(argv[i], s_to_s, 0);
s = StringValueCStr(rs);
HASH_FIND_STR(result->data.hash, s, kv);
result = kv == NULL ? NULL : kv->value;
}
return i_object_to_robject(result);
}
|
#init_translations ⇒ Object
16 17 18 19 |
# File 'lib/i18nema.rb', line 16 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
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 |
# File 'ext/i18nema/i18nema.c', line 370
static VALUE
load_yml_string(VALUE self, VALUE yml)
{
SYMID oid;
i_object_t *root_object = root_object_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(new_root_object, 1);
rb_raise(I18nemaBackendLoadError, "root yml node is not a hash");
}
merge_hash(root_object, new_root_object);
return INT2NUM(current_translation_count);
}
|
#reload! ⇒ true
Clears out all currently stored translations.
backend.reload! #=> true
431 432 433 434 435 436 437 438 439 |
# File 'ext/i18nema/i18nema.c', line 431
static VALUE
reload(VALUE self)
{
i_object_t *root_object = root_object_get(self);
empty_object(root_object, 1);
rb_iv_set(self, "@initialized", Qfalse);
root_object = NULL;
return Qtrue;
}
|
#store_translations(locale, data, options = {}) ⇒ Object
10 11 12 13 14 |
# File 'lib/i18nema.rb', line 10 def store_translations(locale, data, = {}) # TODO: make this moar awesome @initialized = true load_yml_string({locale => data}.deep_stringify_keys.to_yaml) end |