I18N-Portable icon indicating copy to clipboard operation
I18N-Portable copied to clipboard

FEATURE: Section are supported

Open kvandake opened this issue 6 years ago • 8 comments

Hello! I'm a xamarin developer. I thought it would be nice to add namespaces. This will make it impossible to write long keys in the format "MainViewModel.Title" or "BananasContainer.Title". I selected the entity Section and with its help I call Section["Title"]. I also corrected README.md and Unit Tests.

kvandake avatar Feb 06 '18 14:02 kvandake

Your concept of "Sections" looks nice, but

This will make it impossible to write long keys in the format "MainViewModel.Title" or "BananasContainer.Title"

Does that means that it will break current implementations using dots?

xleon avatar Feb 06 '18 14:02 xleon

No, you can access all the keys via dots. The section only allows you to reduce the duplicate prefix and to uniquely duplicate the names. For example, instead of

Main.Title = MainTitle
Company.Name = Company Name
...
FirstViewModel.Title = Title
FirstViewModel.Description = Description
...
SecondViewModelTitle.Title = Title
SecondViewModelTitle.Description = Description
...

can be written

Main.Title = MainTitle
Company.Name = Company Name
...
FirstViewModel = {
   Title = Title
   Description = Description
   ...
}

SecondViewModel = {
   Title = Title
   Description = Description
   ...
}

Maybe I did not understand the question?

kvandake avatar Feb 06 '18 14:02 kvandake

And also you can get easy access to the values through the sections. For example, instead of

var testViewModelTitle = I18N.Current.Translate("FirstViewModel.Title");
var testViewModelDescription = I18N.Current.Translate("FirstViewModel.Description");

can be written

var section = I18N.Current.GetSection("TestViewModel");
var testViewModelTitle = section.Translate("Title");
var testViewModelDescription = section.Translate("Description");

kvandake avatar Feb 06 '18 14:02 kvandake

Great, so it won´t break current implementations.

What I´m missing here is binding support. Many applications should be able to translate anything with a simple string and a single statement.

I´m thinking that you could introduce some kind of special notation for sections, so instead of:

var section = I18N.Current.GetSection("TestViewModel");
var testViewModelTitle = section.Translate("Title");

You would do something like:

var testViewModelTitle = I18N.Current.Translate("TestViewModel=>Title");

or:

var testViewModelTitle = "TestViewModel=>Title".Translate();

=> would be the special notation for sections. You would check that notation on every translation.

That way we could simplify it and bindings would work without any additional code.

xleon avatar Feb 06 '18 15:02 xleon

If you add the form "TestViewModel=>Title", how should JsonKvpReader and other parsers react? If you do a unique separator for the section, then you need to change the contract for ILocaleReader on, for example

public interface ILocaleReader
{
    string GetValueOrNull(string key, string section = null);
    // or
    T GetValueOrNull<T>(string key, string section = null);
}

I propose to change the contract so that the custom readers react to the current section and on the basis of the section give specific value.

kvandake avatar Feb 06 '18 15:02 kvandake

it looks good to me.

I don´t think many people are using the readers but the contract change is needed. This should be something to document on readme though

xleon avatar Feb 06 '18 15:02 xleon

Could be a change in the contract to submit another pull request?

kvandake avatar Feb 06 '18 16:02 kvandake

I´ll wait for you to implement => Then I´ll make sure everything works as expected and then merge your PR.

Thank you!

xleon avatar Feb 06 '18 16:02 xleon