23 ms·
I've found that I need to support localization from the very start. I never display a quoted string. I always use Apple's tokenization (or create my own, if do
by ChrisMarshallNY 3y ago
I've found that I need to support localization from the very start.
I never display a quoted string. I always use Apple's tokenization (or create my own, if doing server code).
Apple has terrific support for localization, which puts the onus on us, to honor it. I have some basic extensions that I use to support localization in my coding[0-2], but there's also just stuff I need to keep in mind, all the time.
There has been discussion of how to deal with things like word order in different languages. For example, in Germanic languages, the modifier usually precedes the subject, while in Romance languages, it tends to be the opposite.
Thankfully, Apple supports the "$" format for sprintf strings[3], so we can do stuff like this:
import Foundation
let localizationAssets = [
(format: "The %1$@ %2$@", modifier: "white", subject: "horse"),
(format: "Le %2$@ %1$@", modifier: "blanc", subject: "cheval")
]
func localizedHorse(_ inLocalization: Int) -> String {
String(
format: localizationAssets[inLocalization].format,
localizationAssets[inLocalization].modifier,
localizationAssets[inLocalization].subject
)
}
// English (Prints "The white horse")
print(localizedHorse(0))
// French (Prints "Le cheval blanc")
print(localizedHorse(1))
[0] https://github.com/RiftValleySoftware/RVS_Generic_Swift_Toolbox/blob/master/Sources/RVS_Generic_Swift_Toolbox/RVS_Generic_Swift_Toolbox_Extensions/RVS_Foundation_Extensions.swift#L184 https://github.com/RiftValleySoftware/RVS_Generic_Swift_Tool...
[1] https://github.com/RiftValleySoftware/RVS_Generic_Swift_Toolbox/blob/master/Sources/RVS_Generic_Swift_Toolbox/RVS_Generic_Swift_Toolbox_Extensions/RVS_Foundation_Extensions.swift#L192 https://github.com/RiftValleySoftware/RVS_Generic_Swift_Tool...
[2] https://github.com/RiftValleySoftware/RVS_Generic_Swift_Toolbox/blob/master/Sources/RVS_Generic_Swift_Toolbox/RVS_Generic_Swift_Toolbox_Extensions/RVS_Foundation_Extensions.swift#L200 https://github.com/RiftValleySoftware/RVS_Generic_Swift_Tool...
[3] https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265-SW2 https://developer.apple.com/library/archive/documentation/Co...
- trinix912 3y agoThe code example should rather be an example of what not to do. This method of concatenating different parts of sentences would fall short for any language where the adjective and noun change the form depending on each other and the context (ex. cases). Most Slavic languages make heavy use of this concept. The solution is to just have the full sentence as a single string and let the translation team write it out in its entirety. Avoid interpolating nouns, adjectives, verbs etc. as much as possible.
- ChrisMarshallNY 3y agoIt was just a silly example. Of course, you are correct, but we don't always have the luxury of being able to hand an entire string to a localization team, and some programmatic interpretation often needs to be done. The best way to deal with it, is to design code that allows entire sentences to be handed to translators (I do that, most of the time). The other advantage of writing localized content, is that it can be easily modified by non-engineers, like marketing folks. Talking points and corporate glossaries are important. Even if we don't plan to localize, it doesn't hurt to use localization tokenized content.
- ChrisMarshallNY 3y agoOh, BTW. This is how not to do it: print("The white horse") I would gently suggest taking the spirit of our Ts and Cs into account, when critiquing others. I was simply sharing a quick technique that may not be known by some folks (the "$" in sprintf). It can definitely have uses. I've been writing localized software for over thirty years. I worked for a Japanese corporation that localized into more than 20 languages (including RTL and up/down). Pretty much everything I write is localized up the yin-yang. Even my test harnesses are usually localized, as I like to keep the habit of writing localized software. I've written systems that have been adopted worldwide, in many different languages, and are still in use (and expanding), many, many years later. There's a teensy little chance that I may have something valid to contribute to the topic.
- Tainnor 3y ago> There's a teensy little chance that I may have something valid to contribute to the topic. That may be true, but the criticism of GP still stands. Yes, in some special situations your approach might be the only viable one (just as how in every codebase you sometimes have to cut corners), but GP is right in pointing out that this is a problematic approach, especially without qualification ("only use this if ...").
- ChrisMarshallNY 3y agoYes, but don’t you think it might be little more diplomatic to say something like: > While this is an interesting way to allow a translator to arrange the order of terms, there’s a great deal more involved, like … , so we should probably avoid using this technique, if at all possible. It’s always a much better idea to allow translators to work on an entire sentence or paragraph. See? We get to show off our expertise, without throwing shade on others. BTW: I have learned, the hard way, that cultural and human sensitivity, as well as basic respect and kindness, are important, when localizing. But I guess those values are kinda “last century,” and don’t really have any value, in today’s hyper-competitive world.
- post-it 3y agoI don't see how this generalizes. If this is supposed to work for any "the {modifier} {subject}" phrase, it won't, because if you want to say "the white cow" you'll end up with "Le vache blanche," which is wrong. You could store `(format: "%3$@ %1$@ %2$@", modifier: "blanc", subject: "cheval", article: "Le")` but now you've got so many gender associations to keep track of, and a phrase that will still only work as a subject (because in German, all of the words will change if the white horse is the object of the sentence). EDIT: Oh you're just talking about sprintf specifically, I see what you mean. I agree with your other comment that it's ideal to pass entire sentences to the translators when possible, which is what I'm getting at here too.