How translate App Label

Status
Not open for further replies.

cb56

Member
Licensed User
Longtime User
Hi,
it' possible with b4a translate the App label (the label under the Icon in App drawer) based on language settings or by code?

thanks
 

cb56

Member
Licensed User
Longtime User
I did some research and found out how to change the label of the application when you change the language in the device settings.

In the manifest, insert the line:

B4X:
SetActivityAttribute("main", android:label, "@string/app_name")

SetActivityAttribute("main", android:configChanges, "locale")

Manifest:

B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14"/>
<supports-screens android:largeScreens="true" 
    android:normalScreens="true" 
    android:smallScreens="true" 
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetActivityAttribute("main", android:label, "@string/app_name")
SetActivityAttribute("main", android:configChanges, "locale")
'End of default text.

Then, in folder "Objects\res" create folders for languages, like this:

"values-de"
"values-en"
"values-fr"
"values-it"
"values-nl"

etc...

if you do not expect some languages, for default settings, insert a folder named "values".

In each folder insert an xml file, named "strings.xml" with this content:

B4X:
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">Label to display</string>
</resources>

Change the string "Label to display" with the translated one.

Make all folder and file Read Only because b4a will delete!

cb56
 
Upvote 0

lock255

Well-Known Member
Licensed User
Longtime User
I did some research and found out how to change the label of the application when you change the language in the device settings.

In the manifest, insert the line:

B4X:
SetActivityAttribute("main", android:label, "@string/app_name")

SetActivityAttribute("main", android:configChanges, "locale")

Manifest:

B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14"/>
<supports-screens android:largeScreens="true" 
    android:normalScreens="true" 
    android:smallScreens="true" 
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetActivityAttribute("main", android:label, "@string/app_name")
SetActivityAttribute("main", android:configChanges, "locale")
'End of default text.

Then, in folder "Objects\res" create folders for languages, like this:

"values-de"
"values-en"
"values-fr"
"values-it"
"values-nl"

etc...

if you do not expect some languages, for default settings, insert a folder named "values".

In each folder insert an xml file, named "strings.xml" with this content:

B4X:
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">Label to display</string>
</resources>

Change the string "Label to display" with the translated one.

Make all folder and file Read Only because b4a will delete!

cb56

E 'can also translate the text of a button, edittex, listview etc?
If the answer is yes, please make me an example for these three?
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
One short note. You don't need to use
B4X:
SetActivityAttribute("main", android:label, "@string/app_name")

You can use the application attribute:

B4X:
#ApplicationLabel: @string/app_name
 
Upvote 0

enrico

Active Member
Licensed User
Longtime User
I get this error :

Parsing code. 0.04
Compiling code. 0.05
Compiling layouts code. 0.00
Generating R file. Error
res\values-es\strings.xml:3: error: Error parsing XML: not well-formed (invalid token)
res\values-fr\strings.xml:3: error: Error parsing XML: not well-formed (invalid token)

This is my Manifest :

B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="4"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
SetActivityAttribute("Main", android:label, "@string/app_name")
SetActivityAttribute("Main", android:configChanges, "locale")
 
Upvote 0

enrico

Active Member
Licensed User
Longtime User
Unfortunately my problem still remains.
This time I will bypass it not using those characters, but I would like to understand how to solve it.
 
Upvote 0

Jack Cole

Well-Known Member
Licensed User
Longtime User
Using either method (specifying through #ApplicationLabel: @string/app_name or through the manifest file), I get the following error when trying to compile:

B4A version: 5.80
Parsing code. (0.00s)
Running custom action. (0.32s)
Compiling code. (0.04s)
Compiling layouts code. (0.00s)
Generating R file. Error
AndroidManifest.xml:14: error: Error: No resource found that matches the given name (at 'label' with value '@strings/app_name').
AndroidManifest.xml:17: error: Error: No resource found that matches the given name (at 'label' with value '@strings/app_name').

Under res/xml/values/strings.xml I have:

B4X:
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">Test App</string>
</resources>

I have also tried naming the folder values-en.

An example project is attached. Any ideas about what I'm doing wrong?
 

Attachments

  • localized.zip
    6.9 KB · Views: 501
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There are several mistakes in this project.

With v5.80 there is a simpler way to use resources. Delete the xml files and the custom build action. Use this manifest editor code instead:
B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="19"/>
<supports-screens android:largeScreens="true"
  android:normalScreens="true"
  android:smallScreens="true"
  android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "@string/app_name")
'End of default text.

CreateResource(values, strings.xml,
<resources>
  <string name="app_name">Test App</string>
</resources>
)

You can create multiple resources if you need more languages.
 
Upvote 0

Jack Cole

Well-Known Member
Licensed User
Longtime User
Thanks Erel. I had not thought to use that new feature. Unfortunately though, it still is not working. Using the above manifest code in a brand new project gives the results of the following. The #ApplicationLabel attribute somehow overrides what is specified in the manifest editor above. If I delete the #ApplicationLabel line in the project, the app label changes back the B4A Example.
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Thanks Erel. I had not thought to use that new feature. Unfortunately though, it still is not working. Using the above manifest code in a brand new project gives the results of the following. The #ApplicationLabel attribute somehow overrides what is specified in the manifest editor above. If I delete the #ApplicationLabel line in the project, the app label changes back the B4A Example.
Is your #ApplicationLabel set to @string/app_name ?
 
Upvote 0
Status
Not open for further replies.
Top