turquoiseowl / i18n

Smart internationalization for ASP.NET
Other
556 stars 156 forks source link

Conditionals #323

Open Drizin opened 7 years ago

Drizin commented 7 years ago

With conditionals we can use a single nugget tag and have different translations according to the value of parameters.:

[[[%0_PRODUCTS_ADDED_TO_ORDER]]] -> 
"%0{0:No products were added|1:1 product was added|%0 products were added} to your order" 
turquoiseowl commented 7 years ago

If I understand this code correctly, you want to move some conditional logic from outside the nugget into the nugget? Can you elaborate on the benefit of doing that? Thanks.

turquoiseowl commented 7 years ago

It's Monday morning and I'm struggling to understand what you mean in your examples, for instance:

[[[%0_PRODUCTS_ADDED_TO_ORDER]]] -> 
 "%0{0:No products were added|1:1 product was added|%0 products were added} to your order" 

Can you help?

Drizin commented 7 years ago

No problem, actually I didn't make myself clear on this. These conditional patterns are based on Ruby i18n inflector (http://www.rubydoc.info/gems/i18n-inflector/2.6.6). Quoting from their documentation:

can be used if there is a need to be strict and/or to use the same token names but assigned to different kinds. Example: welcome: "Dear @gender{f:Lady|m:Sir|n:You|All}"

It follows a simple pattern: @variable { possiblevalue1:text | possiblevalue2: text | .... | default text) Something like:

switch(@gender)
{
   case "f": return "Lady";
   case "m": return "Sir";
   case "n": return "You";
   default: return "All";
}

This example is very simple, and one could just have different nuggets for each possible text. However, when it comes to inflections that do not exist in in your base language (e.g. gender inflection does not exist in English) then conditionals are necessary (#323) which allied to extension attributes (#324) allow you to make whatever flections are needed.

In other words, one may always have a nugget for every possible sentence: [[[Your order has been saved]]] [[[Your invoice has been saved]]] Translations would be specific for each different nugget, and although cumbersome it works.

However, if someone has a more reusable structure like [[[Your %0 has been saved|||(((invoice)))]]] some languages couldn't translate that, because depending on the argument %0 the translation should be different. That's what inflectors are for - they are used to choose the correct inflection when verbs/adjetives/articles/etc depend on a dynamic argument.

In portuguese language for example, invoice (nota fiscal) is a feminine noun, and order (pedido) is a masculine noun. So the translations depend on that attribute (the gender of the argument): "Seu %0 foi salvo" (masculine arguments) "Sua %0 foi salva" (feminine arguments).

Does that make sense?

Many languages have even more complex inflections (multiple types of plural, more than 2 genders), and I think #324 could cover all that. In other words #323 alone could be described as you did (moving conditional logic from outside the nugget to inside it), but #324 covers a real word requirement for inflections.

Hope it's more clear now.