×
Namespaces

Variants
Actions
Revision as of 04:58, 1 July 2012 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Archived:Example Localization in WidSets

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

The article is believed to be still valid for the original topic scope.

Article Metadata
Article
Created: User:Mirsilla (26 May 2008)
Last edited: hamishwillee (01 Jul 2012)
FNWID.gif

This example shows how to localize strings used in widget.

Do the following:

  • Change all "Text string" notations to @text.id notations.
  • Create a [iso2code].properties file for each language, save them in ASCII format, and if possible escape unicode by using the \u1234 notation (use the native2ascii program that comes with JDK).
  • List all properties files in your widget.xml as language resources, with value as "iso2code" and text as the language name.

Known problems

  • Strings defined in widget.xml itself cannot be localized. This includes parameter descriptions and values, info-metadata, and library descriptions.

See also

localization.he

class
{
  const int CMD_BACK    = 1;
  
  /** Not like this 1
   *
   * MenuItems need to be created and used dynamically to
   * get the current language in use.
   *
   * MenuItem BACK        = new MenuItem(CMD_BACK, @option.back);
   * MenuItem LANGUAGE    = new MenuItem(OPEN_SETTINGS, @option.language);
   */
  
  /** Not like this 2
   *
   * If you declare this method, the widget is not automatically restarted
   * when parameters (language parameter) are changed and the language
   * of the widget will not change before the widget is reloaded.
   *
   * void parametersChanged()
   * {
   * }
   */
  
  void startWidget()
  {
    printf("* Staring localization widget: "+getParameter("language"));
    setMinimizedView(createMinimizedView("viewMini", getStyle("default")));
  }


  Shell openWidget()
  {
    Flow flow = new Flow(getStyle("maxi"));
    flow.setPreferredSize(-100, -100);
    flow.add(createHeader(@content.header));
    flow.add(createText(@content.text));
    
    Scrollable scroll = new Scrollable(getStyle("maxi"), flow);
    
    return new Shell(scroll);
  }


  Component createElement(String viewName,
                          String elementId,
                          Style style,
                          Object context)
  {
    //the label used on minimized view
    if ("name".equals(elementId)) {
      return new Label(style, @content.header);
    }
    return null;
  }
  
  
  Label createHeader(String text)
  {
    Label l = new Label(getStyle("header"), text);
    l.setFlags(VISIBLE|LINEFEED|HEXPAND);
    return l;
  }
  
  
  Text createText(String text)
  {
    Text t = new Text(getStyle("text"), text);
    t.setFlags(VISIBLE|LINEFEED|HEXPAND);
    return t;
  }
  
  
  MenuItem getSoftKey(Shell shell, Component focused, int key)
  {
    if (key == SOFTKEY_OK) {
      return new MenuItem(OPEN_SETTINGS, @option.language);
    
    } else if (key == SOFTKEY_BACK) {
      return new MenuItem(CMD_BACK, @option.back);
    }
    return null;
  }
  
  
  void actionPerformed(Shell shell, Component source, int action)
  {
    if (action == CMD_BACK) {
      popShell(shell);
    }
  }
}

widget.xml

<?xml version="1.0" encoding="utf-8"?>
 
<widget spec_version="2.0">
  <info>
    <name>example_localization</name>
    <version>1.0</version>
    <author>example</author>
    <clientversion>1.0</clientversion>
    <shortdescription>Localization Example</shortdescription>
    <longdescription>Localization Example</longdescription>
    <tags>example localization</tags>
  </info>
   
  <parameters>
    <parameter name="widgetname" visible="false">Localization Example</parameter>
    <parameter name="language" description="Language" editable="true" visible="true">
      <option selected="true" value="en">English</option>
      <option value="fi">Finnish</option>
      <option value="sv">Swedish</option>
    </parameter>
  </parameters>
  
  <resources>
    <code src="localization.he"/>

    <lang id="en" src="en.properties"/>
    <lang id="fi" src="fi.properties"/>
    <lang id="sv" src="sv.properties"/>
    
    <stylesheet>
      mini {
        background: solid white;
        color: black;
        align: vcenter hcenter;
      }

      maxi {
        background: solid white;
        color: black;
        align: top hcenter;
      }

      header {
        color: black;
        align: vcenter hcenter;
        font: medium plain;
      }

      text {
        color: black;
        align: top left;
        font: small plain;
      }
    </stylesheet>
  </resources>
    
  <layout minimizedheight="3em">
    <view id="viewMini" class="mini">
      <script id="name" class="mini" top="0%" bottom="50%"/>
      <label class="mini" top="50%" bottom="100%">[${language}]</label>
    </view>
	</layout>
  
</widget>

en.properties

option.back = Back
option.language = Language

content.header = Localization example
content.text = English content in English language

fi.properties

option.back = Takaisin
option.language = Kieli

content.header = Lokalisointiesimerkki
content.text = Suomen kielellä olevaa kontenttia suomeksi

sv.properties

option.back = Tillbaka
option.language = Språk

content.header = Localization exämppel
content.text = Svenska text
This page was last modified on 1 July 2012, at 04:58.
37 page views in the last 30 days.
×