开发者

How can you localize a sentence with a dynamic number of conjunctions?

开发者 https://www.devze.com 2023-04-05 21:58 出处:网络
Suppose I\'ve got a sentence tha开发者_如何学Pythont I\'m forming dynamically based on data passed in from someone else.Let\'s say it\'s a list of food items, like: [\"apples\", \"pears\", \"oranges\"

Suppose I've got a sentence tha开发者_如何学Pythont I'm forming dynamically based on data passed in from someone else. Let's say it's a list of food items, like: ["apples", "pears", "oranges"] or ["bread", "meat"]. I want to take these sentences and form a sentence: "I like to eat apples, pears and oranges" and "I like to eat bread and meat."

It's easy to do this for just English, but I'm fairly sure that conjunctions don't work the same way in all languages (in fact I bet some of them are wildly different). How do you localize a sentence with a dynamic number of items, joined in some manner?

If it helps, I'm working on this for Android, so you will be able to use any libraries that it provides (or one for Java).


I would ask native speakers of the languages you want to target. If all the languages use the same construct (a comma between all the elements except for the last one), then simply build a comma separated list of the n - 1 elements, and use an externalized pattern and use java.util.MessageFormat to build the whole sentence with the n - 1 elements string as first argument and the nth element as second argument. You might also externalize the separator (comma in English) to make it more flexible if needed.

If some languages use another construct, then define several Strategy implementations, externalize the name of the strategy in order to know which strategy to use for a given locale, and then ask the appropriate strategy to format the list for you. The strategy for English would use the algorithm described above. The strategy for another language would use a different way of joining the elements together, but it could be reused for several languages using externalized patterns if those languages use the same construct but with different words.

Note that it's because it's so difficult that most of the programs simply do List of what I like: bread, meat.


The problem, of course, is knowing the other languages. I don't know how you'd do it accurately without finding native speakers for your target languages. Google Translate is an option, but you should still have someone who speaks the language proofread it.

Android has this built-in functionality in the Plurals string resources. Their documentation provides examples in English (res/valules/strings.xml) and also in another language (placed in res/values-pl/strings.xml)

Valid quantities: {zero, one, two, few, many, other}

Their example:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <plurals name="numberOfSongsAvailable">
        <item quantity="one">One song found.</item>
        <item quantity="other">%d songs found.</item>
    </plurals>
</resources>

So it seems like their approach is to use longer sentence fragments than just the individual words and plurals.


Very difficult. Take french for example: it uses articles in front of each noun. Your example would be:

J'aime manger des pommes, des poires et des oranges.

Or better:

J'aime les pommes, les poires et les oranges.

(Literally: I like the apples, the pears and the oranges).

So it's no longer only a problem of conjunctions. It's a matter of different grammatical rules that goes beyond conjunctions. Needless to say that other languages may raise totally different issues.


This is unfortunately nearly impossible to do. That is because of conjugation on one side and different forms depending on gender on the other. English doesn't have that distinction but many (most of?) other languages do. To make the matter worse, verb form might depend on the following noun (both gender-dependent form and conjugation).

Instead of such nice sentences, we tend to use lists of things, for example:

The list of things I would like to eat:
apples
pears
oranges

I am afraid that this is the only reasonable thing to do (variable, modifiable list).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号