How to implement language Translation in iPhone app? - ios

I need to implement language translation in my app. Could anybody tell how to do that. Actually i am not able to find APIs for it and google translation API v2 is paid.

To localize strings in your code:
-Create a new Localization.strings file
File -> New File -> iOS -> Resources -> Strings file
-Select this file and Show the file inspector
Command + Option + 1
-Click the add button under localization and add a default language (I expect English) and the other languages you want.
-You'll notice that you have a little arrow next to your Localizable.strings file, if you click it, you can see the languages that you've added.
-For each phrase you want to translate, use the following syntax:
"key" = "value";
"hello" = "bonjour";
-In your code where you want to use the translation, use NSLocalizedString(#"hello", #"hello"); (The second parameter is a comment, it doesn't matter what value you use here)
When it comes to nibs, you'll need to manually change the strings and re-arrange the text fields etc in each nib.

Related

How to insert space into a CFBundleDisplayName, IOS

I'm new to IOS and currently I'm working on Localization of Displaying Apps icon.
and I made InfoPlist.strings
and then I put the text for like this in my InfoPlist.strings(MyLanguage) file
"CFBundleDisplayName" = "sometext sometext andlasttext ";
and it literally shows every text, include &#x2007 on iphone emulators home screen.
and of course either
"CFBundleDisplayName" = "sometext sometext andlasttext";
is also not working! could you help me?
This solution works for me before iOS11  consider space as double space.
But from iOS 11 Apple has stopped truncating for longer app names. I thing if app name count is greater then 12 to 14, the spaces will be removed. Otherwise, they'll still exist.
  = 2 spaces
Localizing your app display name is actually a very simple task, so let’s just get right into it.
Set up Localization
First, make sure you set up localization for another language (Note, this is not all that is required to completely localize an app, just the display name). Select your project in the project navigator, go to Localizations, then select a language to localize your display name.
Create a Strings File
Next, you have to create a Strings file that will contain the localized name to be used in your project. Go to File > New > File, select your OS target, go to Resources, and select the Strings file template.
Select the template, enter InfoPlist into the name field, and create the file.
Adding Localized Strings
Once you have created your strings file, open it and select all of the languages you would like to localize for in the inspector. In your base language file, add the following:
/* Localized Bundle Display Name */
"CFBundleDisplayName" = "Your_Localized_Name_In_Native_Language";
Then, enter the same in every other language file, swapping in your translated name in the place of your native name as so:
/* Localized Bundle Display Name */
"CFBundleDisplayName" = "Localized_Name_In_Other_Language";
When you are done, each file should look like the following:
Add Localization Keys to Info.plist
The last thing you have to do is add the necessary keys to your Info.plist file to enable your app to read and swap in your localized display name when appropriate. The first key you will add is the “bundle display name” key, it will look like so:
Bundle display name = $(PRODUCT_NAME)
You also have to add the “application has localized display name” key and set it to yes, which will look like the following:
Application has localized display name = YES
These keys in Info.plist will look like the following:
Thats all there is to it! Your app name will now be displayed in all languages that you localize to.

XLIFF doesn't contain all forms of plurals when trying to localize a project in Xcode

I'm trying to localize a small Xcode project (Xcode 9 and Swift 4). In that project there are places where I use plurals, so I need to localize them, too. To do that, I use stringsdictfile. In code I use localizedStringWithFormat(_:,fromat,:argument) static method on a String. Here how my code for a plural looks (I just print the words for testing):
let localizedString = NSLocalizedString("%d apple(s)", comment: "The number of apples")
print(String.localizedStringWithFormat(localizedString, 0))
print (String.localizedStringWithFormat(localizedString, 1))
print(String.localizedStringWithFormat(localizedString, 2))
print(String.localizedStringWithFormat(localizedString, 10))
Then I create a stringsdict file. It looks like this:
In the video from WWDC 2017 (session 401) about Localization it is said, that when we use stringsdict file for localizing plurals into other languages (in this case, I want to localize it into Russian) we just need to give values for the cases of our development language (English), and when exporting for localization, Xcode will automatically create cases in an XLIFF file for the language into which we want to localize. So I've given values for zero, one and other (in the demo from the session values are given only to keys one and other, however, I don't thing that that's the reason of the problem).
Now, when I create an XLIFF file it looks like this (only the part of apples):
As you can see, Xcode doesn't generate cases for a particular language automatically (for Russian it should have 4). I'm using stringsdict files as well as trying to localize plurals for the first time, so I can't figure out what I'm doing wrong. If you know where is the issue, or have any suggestions, I would appreciate your help.
There is little you can do in Xcode. XLIFF just holds the segments that it's been told to hold.
A better approach would be to rephrase the strings so that plurals are less an issue, like:
not "%d apple(s)"
but "number of apples: %d"
You should file a bug at apple about that.
Unless you have a requirement to work with the XLIFF from Xcode, I suggest you don't and instead rely on the string files.

NSLocalizedString in Spritekit SKLabelNode

I've been searching all over the internet and there doesn't seem to be a clear explanation on how to localize strings using SpriteKit. Only seeing tutorials for people using the interface builder, but all I really want is, imagine this:
I have an SKLabelNode called label. And I define the text like:
labl.text = NSLocalizedString("titleOfTheScreen",nil)
So basically what I think I have to do is add the new language in the Project settings. Then, I add a new Strings file called Localized, and add it to the new folder.
But what happens to my English language? There's no file for the original one
First you have to add a Strings File:
Then open the project settings and add a new language:
Mark your added strings file as target:
Find the newly added localising file. (English is automatically added)
From your screenshots I can see that you have "File.strings" file. You should have created "Localizable.strings" file.
Also, I can see that you have the (Base), (English) and (German) strings version. Why do you think English is not there?
In each of the files you should put strings like that:
"titleOfTheScreen" = "blah-blah";
Replace "blah-blah" with the proper translation in each of the strings files. It's important to note that the semi-colon at the end of the lines in strings files are mandatory, otherwise Xcode would issue some really funny error messages. This is easy to overlook if you're programming in Swift and trailing semi-colons are not mandatory.

Specify base localization for Localized formatted APNS strings

My base localizations are in storyboards, but also in a struct like this:
struct Strings {
struct Restaurant {
static let makeCall = NSLocalizedString("Restaurant-makeCall", value: "Call %#", comment: "Restaurant-makeCall: title for button that allows you to call a place")
}
//...many more
}
This works pretty well because it keeps them in one place, defines a key that is separate from the base translation value, and gives me autocomplete when I'm using them: Strings.Restaurant.makeCall
To enter translations, I use the xliff import/export process: editor > export for localization. I found genstrings had trouble with the longhand form of NSLocalizedString.
This all works well until I get to Localized APNS Messages. Given an loc-key and optional loc-args, They search for a matching localization key.
For the translations, this works, because there's a Localizable.strings built for each translation when I import the translated xliff file.
There is no Localizable.strings file for my base translation. I attempted to make one, but the xliff export does not notice those fields to add to other translations, so I think that's not the right way to do it.
How do I add base translations in a way that will work for APNS?
One functional, but duplicative approach is to define them in both places:
Define keys (and unused values) in the Strings struct. The xliff export notices these and this allows other languages to provide translations
Define keys and values in a Localizable.strings file you create for your base translation. This allows APNS to find the values when you're in the base language, but xliff export can't see these.
This works, but if somebody ever changes one without keeping the other file up-to-date, differences in translation will occur that will be hard to detect, so I don't like it very much.

Change the encoding of a file in Visual Studio Code

Is there any way to change the encoding of a file?
For example UTF-8 to ISO 8859-1?
Setting Example Sublime Text:
"default_encoding": "UTF-8"
So here's how to do that:
In the bottom bar of VSCode, you'll see the label UTF-8. Click it. A
popup opens. Click Save with encoding. You can now pick a new
encoding for that file.
Alternatively, you can change the setting globally in Workspace/User settings using the setting "files.encoding": "utf8". If using the graphical settings page in VSCode, simply search for encoding. Do note however that this only applies to newly created files.
Apart from the settings explained in the answer by #DarkNeuron:
"files.encoding": "any encoding"
you can also specify settings for a specific language like so:
"[language id]": {
"files.encoding": "any encoding"
}
For example, I use this when I need to edit PowerShell files previously created with ISE (which are created in ANSI format):
"[powershell]": {
"files.encoding": "windows1252"
}
You can get a list of identifiers of well-known languages here.
The existing answers show a possible solution for single files or file types. However, you can define the charset standard in VS Code by following this path:
File > Preferences > Settings > Encoding > Choose your option
This will define a character set as default.
Besides that, you can always change the encoding in the lower right corner of the editor (blue symbol line) for the current project.

Resources