localized_string 0.2.0 copy "localized_string: ^0.2.0" to clipboard
localized_string: ^0.2.0 copied to clipboard

LocalizedString is an implementation of String that depends on BuildContext.

localized_string #

LocalizedString is an implementation of String that depends on BuildContext.

It can be helpful if you need to return a string which should be localized in the future but you don't have a context. Simple example is validator, it should return localized string but it can't use the context because validator is encapsulated from it.

Examples #

To create localized string use this snippet:

LocalizedString.fromFactory((context) => AppLoc.of(context).title);

To compare localized string you should specify key for each of them:

final s1 = LocalizedString.fromFactory((context) => AppLoc.of(context).title, 'title');
final s2 = LocalizedString.fromFactory((context) => AppLoc.of(context).title, 'title');
final s3 = LocalizedString.fromFactory((context) => AppLoc.of(context).desc, 'desc');

assert(s1 == s2);
assert(s1 != s3);

Also you can create LocalizedString from regular string:

LocalizedString.fromString("Hello world");

it will create LocalizedString with the same key as its value.

To localize string, just call localize method:

final s1 = LocalizedString((context) => AppLoc.of(context).title, 'title');
s1.localize(context);

You can concatenate two strings by using + operator:

final s1 = LocalizedString.fromString("Hello");
final s2 = LocalizedString.fromString("world");
final s = s1 + ' ' + s2;

assert(s.localize(context) == 'Hello world');

And join:

final hello = LocalizedString.fromFactory((_) => 'Hello');
final world = LocalizedString.fromFactory((_) => 'World!');

final concat = [hello, world].joinLocalizedS(' ');
// or
final concat = [hello, world].joinLocalized(LocalizedString.fromString(' '));

assert(concat.localize(context) == 'Hello World!');
2
likes
130
points
340
downloads

Publisher

verified publisherandrey.kabylin.ru

Weekly Downloads

LocalizedString is an implementation of String that depends on BuildContext.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

equatable, flutter, uuid

More

Packages that depend on localized_string